Search & display results [java] - java

I run a small online gaming community and deal with a database of accounts.
The setup is this:
Folder named Accounts
Inside the Accounts directory, there is 200,000+ text files organized by player name. Access to this folder manually is a pain because of the needed RAM to get in and search files. I find this very inconvenient.
I access this directory to send password reminders or for highscores on who has been playing the longest.
Here is an example of an account file. This file is named Falcon.txt
[ACCOUNT]
character-username = Falcon
character-password = falconpassword
[INFO]
character-coordx = 3252
character-coordy = 3432
character-active = yes
character-ismember = 1
character-messages = 5
character-lastconnection = [removed]
character-lastlogin = 2009-11-29
character-energy = 100
character-gametime = 193
character-gamecount = 183
[EQUIPMENT]
character-equip = 0 4724 0
character-equip = 1 1052 0
character-equip = 2 6585 0
character-equip = 3 4151 0
character-equip = 4 4720 0
character-equip = 5 1215 0
character-equip = 6 -1 0
character-equip = 7 4722 0
character-equip = 8 -1 0
character-equip = 9 775 0
character-equip = 10 1837 0
character-equip = 11 -1 0
character-equip = 12 6735 0
character-equip = 13 -1 0
[APPEARANCE]
character-look = 0 1
character-look = 1 1
character-look = 2 2
character-look = 3 3
character-look = 4 5
character-look = 5 2
[STATS]
character-skill = 0 1 0
character-skill = 1 1 0
character-skill = 2 1 0
character-skill = 3 1 0
character-skill = 4 1 0
character-skill = 5 1 0
character-skill = 6 1 0
character-skill = 7 1 0
character-skill = 8 1 0
character-skill = 9 1 0
character-skill = 10 1 0
character-skill = 11 1 0
character-skill = 12 1 0
character-skill = 13 1 0
character-skill = 14 1 0
character-skill = 15 1 0
character-skill = 16 1 0
character-skill = 17 1 0
character-skill = 18 1 0
character-skill = 19 1 0
character-skill = 20 1 0
[ITEMS]
[BANK]
[FRIENDS]
[IGNORES]
[END]
There is a huge database of these and search through the directory in the files for values.
Values I mean by item ID's or IP addresses to find and track other accounts.
However I have a new problem and my development for this is crashing.
As you can see in the file the lines are organized by tabs.
character-equip = 0 4724 1
If I put the value 4724 in my search application, I want it to print out the value 1 tab to the right of the found search result. I want it to print out the value for the found results only, not extra results.
So the search could look like this:
1 "Enter item to find:"
2 "Enter item to find: 4724"
3 "Account Falcon.txt has 1!"
press any key to continue...
Or if there was more quantity of that equipped item
character-equip = 5 1239 102
1. "Enter item to find:"
2. "Enter item to find: 1239"
3. "Account Falcon2.txt has 102!"
press any key to continue...
I simply want to input an item ID, and have it display the value after the found value. The white space is a tab. I have tried doing this and the only successful way of getting any result is to put a tab in between the search term. So if I want to find item 1239 id type this in the cmd line:
Enter item to find:<tab>1239<tab>
It would then search for that and will display the accounts with that item in it. However I still have to individually open up the accounts to find out the quantity of that item. I want the search results to display the quantity of the item if the value is found. However if the value is a quantity and it trys to search one tab over, I want it to either skip it or say zero.
This is what I mean.
character-equip = 0 1024 1239
Enter item to find: 1239
If it hits this account I want to make the search results display a zero if it cannot tab over or view any values in the white space. So it will display as null or zero
Account Falcon3.txt has null!
or
Account Falcon3.txt has 0!
I've attempted to do this but I am unsure how to achieve this.
Here is my code.
import java.io.*;
import java.util.*;
public class ItemDatabase {
public static void main(String args[]) {
System.out.print("Enter item to find: ");
Scanner sc = new Scanner(System.in);
find(sc.nextLine());
}
public static void find(String delim) {
File dir = new File("accounts");
if (dir.exists()) {
String read;
try {
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File loaded = files[i];
if (loaded.getName().endsWith(".txt")) {
BufferedReader in = new BufferedReader(new FileReader(loaded));
StringBuffer load = new StringBuffer();
while ((read = in.readLine()) != null) {
load.append(read + "\n");
}
String delimiter[] = new String(load).split(delim);
if(delimiter.length > 1) {
System.out.println("Account " + loaded.getName() + "has " + delimiter[1] + "!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("error: dir wasn't found!");
}
}
}
Thanks guys I hope you can help me.

This is simply crying out for a database. If your entire back end is running in a single java process I'd recommend going with something like Apache Derby or H2.
If you'd rather not move to a database, and the main problem is the act of listing all the entries in a single directory, could you split it into a heirarchy. Your Falcon account could then be located in F/FA/Falcon.txt - each directory would then contain a more manageable number of files.

Aside from the need for a database, you could probably implement your solution more intuitively and easily using commandline utilities such as find, grep, etc. or a text-processing language such as perl.
Maybe
grep '[0-9]+\t[0-9]+\t1239' Account_Falcon3.txt
Would return
character-equip = 0 1024 1239
You could then easily see that the value is 0 for that item.
I cannot emphasize enough the need to not write a Java program to do this - you won't do as good a job as the authors of the standard shell utilities. Let's face it, the fact that you are asking this question indicates that you are a newb! :) (We are all newbs depending on the topic).

Related

Java: Looping issue within my file reading program

I've only been coding for a couple months so I'm still fairly new.
I'm writing a program using Java in Eclipse IDE that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name (which is entered via keyboard by the user). Each line of the file stores a name followed by integers representing the name's popularity in each decade: 1900, 1910, 1920, and so on. My program prompts the user for a name, searchs the file for that name, and displays the data for that name from the file to the screen.
Example of data from txt file: Sam 58 69 99 131 168 236 278 380 467 408 466
Example of what should be displayed on screen:
Statistics on name "Sam"
1900: 58
1910: 69
1920: 99
1930: 131
...
This is my program:
public class fileAndExceptionHanding {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
FileInputStream fileByteStream = null;
Scanner inFS = null;
String userName = "";
String fileName = "";
File file = new File("names.txt");
List<Integer> numNamesOfYear = new ArrayList<Integer>();
boolean repeatSearch = true;
boolean repeatNameSearch = true;
int startingYear = 1900;
int yearCounter = startingYear;
char userKey;
fileByteStream = new FileInputStream("names.txt");
inFS = new Scanner(fileByteStream);
if (!file.exists()) {
file.createNewFile();
}
System.out.println("This program allows you to search through the data from the Social
Security Administration to see how popular a particular name has been since 1900.");
do {
System.out.println("Name? ");
userName = keyboard.next();
do {
while (!(fileName.equals(userName)) && inFS.hasNextLine()) {
fileName = inFS.next();
}
if (!(fileName.equals(userName))) {
System.out.println("Error! Name was not found. Please enter another name for our
system to search.");
userName = keyboard.next();
repeatNameSearch = true;
}
else {
repeatNameSearch = false;
}
} while (repeatNameSearch);
while (inFS.hasNextInt()) {
numNamesOfYear.add(inFS.nextInt());
}
System.out.println("Statistics on name " + "\"" + userName + "\"");
yearCounter = startingYear;
for (int i = 0; i < numNamesOfYear.size(); ++i) {
System.out.println(" " + yearCounter + ": " + numNamesOfYear.get(i));
yearCounter += 10;
}
numNamesOfYear.clear();
System.out.println("If you would like to search another name for popularity data,
please enter \"y\" to continue.");
System.out.println("If you wish to exit, please press any other key.");
userKey = keyboard.next().charAt(0);
if (!(userKey == 'y' || userKey == 'Y')) {
repeatSearch = false;
}
}while (repeatSearch);
fileByteStream.close(); // Closing file and input stream.
keyboard.close(); // Closing keyboard scanner.
inFS.close(); // Closing file scanner.
}
}
My program is running perfectly at first until the prompt for the user to enter "y" to continue or any other key to exit. When I enter "Y/y" and enter another name to search, my program doesn't repeat the name search and instead displays my "error invalid name, please enter a different name to search" message even though the name I entered is valid and inside the txt file.
Example of another name and data within the txt file that I tried to repeat search for:
Abbey 0 0 0 0 0 0 0 0 537 451 428
If you would like to search another name for popularity data, please enter "y"
to continue.
If you wish to exit, please press any other key.
y
Name?
Abbey
Error! Name was not found. Please enter another name for our system to search.
Entering "Sam" gives me correct data but if I press y to repeat loop and enter "Sam" again, it prints the following (Skips year data output):
This program allows you to search through the data from the Social Security Administration to see how popular a particular name has been since 1900.
Name?
Sam
Statistics on name "Sam"
1900: 58
1910: 69
1920: 99
1930: 131
1940: 168
1950: 236
1960: 278
1970: 380
1980: 467
1990: 408
2000: 466
If you would like to search another name for popularity data, please enter "y"
to continue.
If you wish to exit, please press any other key.
y
Name?
Sam
Statistics on name "Sam"
If you would like to search another name for popularity data, please enter "y"
to continue.
If you wish to exit, please press any other key.
I assume the problem lies somewhere within my loops, however it's proving difficult for me to find.
Any help or advice on where my looping problem lies would be greatly appreciated!
Thank you in advance.

Get all perfect squares in a given range

I have 2 numbers M and N of range, I want to count all perfect squares in this range and that perfect square should have below property.
lets say the perfect square has digits n1,n2,n3,n4, n5 etc then the digits should be like this:
n1 < n2 > n3 < n4 > n5 etc
Example:
Input : M = 40, N = 70
Output : 1
Explanation:
The perfect squares in the range 40 to 70 are 49 and 64. But only 49 has the proerty where 4 < 9. But for 64, 6 < 4 is false. So output is 1.
Constraints:
1 <= M <= N <= 1011.
This was asked during an interview and I tried the below code that works for some input and fails for some hidden test cases which I don't know.
I have taken code references from this link to find out perfect squares first.
static int perfectSquares(long M, long N) {
int total = 0;
// Getting the very first number
long number = (long) Math.ceil(Math.sqrt(M));
// First number's square
long n2 = number * number;
// Next number is at the difference of
number = (number * 2) + 1;
// While the perfect squares
// are from the range
while ((n2 >= M && n2 <= N)) {
/** Logic to check if digits of form d1 < d2 > d3 < d4 > d5 etc **/
long n = n2;
String s = n + "";
int a = s.charAt(0) - '0';
boolean isless = true;
boolean valid = true;
for (int i = 1; i < s.length(); i++) {
int b = s.charAt(i) - '0';
if (isless && a > b) {
valid = false;
break;
}
if (!isless && a < b) {
valid = false;
break;
}
isless = !isless;
a = b;
}
// Count the valid the perfect square
if (valid) {
System.out.println("sq:" + n2);
total++;
}
// Get the next perfect square
n2 = n2 + number;
// Next odd number to be added
number += 2;
}
return total;
}
Is there any bug in my code or otherwise can we reduce the time complexity of this code?
Issue is with the checks for "valid" where simply using >= and <= will fix it.
I ran your code against the full range (1..100000000000) and found an issue early with the series result (your code prints the valid numbers (i got rid of the newline)):
1 4 9 16 25 36 49 121 144 196 361 441 484 576 676 784 1225 1444 1600 1849 1936 2209 2304 2401 2500 2601 2704 2809 2916 3600 3844 3969 4624 4900 5625 5929 6724 6889 7744 8836 14161 15376 16384 17161 18496 19044 19881 25281 26244 28561 29241 29584 35344 36481 49284 57121 58081 58564 66564 67081 68121 69696 77284 110224 110889 111556 120409 121104 121801 122500 131769 133956 140625 144400 150544 154449 160000 160801 161604 162409 164836 165649 172225 173889 174724 180625 182329 184900 186624 190969 191844 193600 198916 220900 230400 232324 240100 250000 260100 270400 273529 275625 277729 280900 291600 294849 295936 330625 332929 342225 351649 352836 360000 361201 362404 363609 364816 374544 375769 381924 384400 390625 396900 442225 443556 444889 452929 455625 462400 471969 481636 484416 485809 490000 491401 492804 495616 553536 562500 565504 571536 580644 586756 592900 660969 665856 672400 680625 683929 688900 692224 693889 695556 774400 777924 786769 883600 894916 896809 1119364 1227664 1229881 1304164 1406596 1418481 1503076 1515361 1527696 1535121 1615441 1623076 1628176 1633284 1666681 1669264 1726596 1745041 1747684 1758276 1766241 1844164 1855044 1907161 1937664 1999396 2208196 2214144 2217121 2223081 2226064 2307361 2316484 2328676 2427364 2446096 2515396 2534464 2637376 2768896 2802276 2819041 2849344 2859481 2917264 2937796 2979076 2989441 3316041 3319684 3334276 3504384 3519376 3538161 3644281 3709476 3717184 3748096 3767481 3837681 3849444 3857296 3888784 3916441 3924361 3944196 3956121 3968064 4528384 4713241 4717584 4726276 4748041 4778596 4879681 4937284 4946176 4955076 4968441 4977361 4999696 5612161 5626384 5645376 5669161 5803281 5827396 5866084 5900041 5934096 5958481 6615184 6625476 6646084 6713281 6718464 6859161 6906384 6922161 6948496 6959044 7706176 7717284 7907344 7918596 7958041 8856576 8868484 8916196 8928144 8934121 8946081 9979281 11022400 11042329 11055625 11088900 11122225 11155600 11175649 11182336 11195716 12040900 12061729 12075625 12110400 12180100 12250000 13010449 13053769 13140625 13176900 13191424 13271449 13293316 13344409 13351716 13373649 13395600 14032516 14040009 14062500 14070001 14092516 14152644 14182756 14197824 14250625 14265729 14386849 14394436 14440000 14462809 14470416 14485636 15054400 15186609 15194404 15233409 15241216 15264649 15342889 15350724 15444900 15460624 15499969 15570916 15586704 15594601 16000000 16080100 16120225 16160400 16184529 16240900 16281225 16353936 16370116 16394401 16483600 16564900 16662724 16670889 17073424 17164449 17172736 17222500 17230801 17255716 17272336 17280649 17355556 17363889 17372224 17388900 17472400 17673616 17690436 17698849 17774656 17791524 18011536 18062500 18164644 18190225 18232900 18275625 18292729 18352656 18361225 18386944 18455616 18472804 18481401 18490000 18662400 18774889 18783556 18792225 18887716 19061956 19096900 19140625 19184400 19254544 19333609 19342404 19351201 19360000 19465744 19492225 19562929 19580625 19686969 19695844 19775809 19784704 19793601 19891600 22033636 22052416 22061809 22071204 22080601 22090000 22155849 22231225 22240656 23011209 23020804 23030401 23040000 23164969 23193856 23232400 23261329 23280625 23299929 24000201 24010000 24176889 24186724 24255625 24373969 24383844 24462916 24472809 24482704 24492601 25000000 25010001 25020004 25030009 25150225 25180324 25230529 25250625 25270729 25351225 25381444 25441936 25472209 25482304 25492401 26010000 26020201 26030404 26040609 26050816 26081449 26132544 26142769 26193924 26265625 26450449 26460736 26481316 26491609 26553409 26563716 26594649 26697889 27040000 27050401 27060804 27071209 27081616 27164944 27352900 27373824 27394756 27562500 27583504 27772900 28090000 28111204 28121809 28132416 28153636 28355625 28376929 28440889 28451556 28462225 28590409 28686736 28793956 28890625 29030544 29084449 29160000 29170801 29181604 29192409 29343889 29354724 29452329 29484900 29560969 29571844 29593600 29680704 29691601 33062500 33085504 33131536 33292900 33350625 33373729 34140649 34152336 34175716 34222500 34292736 34374769 35010889 35022724 35081929 35164900 35283600 35354916 35366809 35390601 35450116 35473936 35485849 35581225 36000000 36120100 36180225 36240400 36360900 36481600 36590401 37063744 37161216 37173409 37185604 37197801 37222201 37454400 37576900 37662769 37773316 37785609 37797904 38031889 38130625 38192400 38241856 38365636 38390416 38440000 38452401 38464804 38588944 38663524 38775529 39050001 39062500 39250225 39262756 39287824 39350529 39375625 39463524 39664804 39690000 39891856 44142736 44182609 44195904 44222500 44262409 44275716 44355600 44462224 44488900 45050944 45091225 45131524 45252529 45292900 45360225 45481536 45562500 46022656 46076944 46144849 46185616 46240000 46253601 46280809 46294416 46553329 46580625 47141956 47196900 47265625 47361924 47444544 47554816 47582404 47692836 47775744 48052624 48121969 48163600 48260809 48274704 48330304 48385936 48441600 48580900 48790225 49000000 49140100 49252324 49280400 49350625 49491225 49561600 49660209 49885969 55011889 55130625 55160329 55353600 55442916 55472704 55591936 56220004 56250000 56280004 56550400 56595529 57032704 57062916 57153600 57183844 57198969 57274624 57350329 57380625 57486724 57577744 57790404 58033924 58064400 58140625 58277956 58461316 58476609 58491904 58583716 58675600 58782889 59043856 59243809 59274601 59290000 59351616 59382436 59397849 59474944 59675625 59783824 66096900 66161956 66373609 66471409 66487716 66585600 66683556 67141636 67174416 67190809 67240000 67272804 67354849 67683529 68062500 68095504 68161536 68260644 68392900 68442529 68475625 68591524 68690944 68773849 68790436 68890000 69155856 69222400 69272329 69388900 69455556 69472225 69555600 69672409 69772609 77000625 77440000 77492809 77792400 78021889 78251716 78340201 78375609 78393316 78552769 78676900 79174404 79192201 79263409 79281216 79691329 79780624 79887844 88021924 88284816 88341201 88360000 88472836 88491649 88585744 88792929 89075844 89264704 89283601 89340304 89491600 89680900 89775625 99022401 99042304 99062209 99121936 99141849 99241444 99460729 110418064 110439081 110502144 110544196 110649361 110838784 111746041 111767184 111809476 111915241 111957561 120209296 120428676 120516484 120538441 120736144 120758121 120802081 120824064 121308196 121418361 121528576 121837444 122412096 122722084 122744241 130439241 130507776 130622041 130919364 131308681 131423296 131744484 131767441 131813361 131928196 132434064 132503121 132526144 132756484 132848676 133726096 133888041 133911184 133957476 140209281 140612164 140707044 140944384 141419664 141824281 142229476 142301041 143328784 143544361 143712144 143736121 143808064 144504441 144528484 150528361 150749284 151807041 151979584 152226244 152819044 152868496 153958464 154405476 154778481 154803364 155725441 160326244 160757041 161518681 161849284 162333081 162766564 162919696 163609681 163635264 164506276 164557584 164814244 165611161 166616464 166978084 170537481 170668096 170877184 170929476 171819664 172423161 172607044 172712164 173659684 173923344 174609796 174927076 175801081 177715561 177848896 177902244 180311184 180338041 180526096 181225444 181548676 181656484 181737361 181926144 182547121 182628196 182817441 182844484 183819364 183846481 184715281 185558884 185613376 185804161 186978276 187909264 187936681 188815081 190219264 190826596 192959881 193738561 193822084 193877776 193989184 194937444 195608196 195748081 196616484 196728676 220879044 220938496 221444161 222427396 222636241 222666084 222845184 230402041 230766481 231709284 232227121 232318564 232806564 232837081 232989696 233417284 233845264 240002064 240529081 240622144 240839361 241118784 241429444 242549476 242705241 242767561 242923396 243609664 244547044 250304041 250525584 250557241 250778896 250937281 251603044 251666496 251825161 252206161 253637476 253701184 253733041 253956096 254849296 255616144 255648121 255712081 255744064 260209161 260435044 261113281 261436561 261727684 263445361 263607696 263705121 264517696 264745441 264908176 266636241 270306481 270339364 271524484 271557441 271623361 272514064 272547081 272613121 272646144 272712196 272877361 273439296 273505444 274929561 275858881 276756496 276823044 276956164 277855561 277922241 277955584 281434176 281769796 281937681 282946041 282979684 283518244 283619281 284968161 285914281 286828096 287709444 287777296 290225296 291419041 291658084 293711044 293848164 294534244 294877584 294946276 296666176 296735076 296838441 296907361 297838564 298978681 330439684 330803344 331749796 332734081 333756361 333829441 333939076 340107364 340845444 340919296 341436484 341547361 341806144 341917081 342546064 342657121 342768196 343657444 343768681 344436481 344622096 344919184 350513284 351225081 351900081 352538176 352613284 353778481 353966596 354418276 354606561 355624164 360544144 360658081 361304064 361418121 361836484 361988676 362445444 362559681 363436096 363703041 363817476 364504464 365536161 365727376 370216081 370909081 371949796 372837481 373301041 373339684 373958244 375623161 376709281 376903396 377758096 380601081 380718144 381108484 381655296 382515364 383337241 383415561 383807281 383846464 384905161 385768881 385808164 386869561 386948241 387735481 388957284 390418081 390813361 391723264 392515344 393546244 394777161 396766561 396846241 440118441 440412196 440538121 440622081 441336064 441504144 441924484 442513296 443439364 444408561 444619396 450755361 450967696 451945081 452668176 453306681 454627684 454926241 460617444 460703296 461218576 461304484 461433361 461648196 461734144 461777121 461906064 462637081 462723121 462766144 464747364 465739561 465955396 466948881 471845284 473889361 474629796 474847681 477947044 480004281 480223396 480749476 480837184 481407481 482329444 482417296 482768784 482856676 483516121 483604081 483648064 484528144 484616196 484836361 484924441 484968484 491908041 492218596 492307344 493817284 493906176 494439696 494706564 495908361 496888681 496933264 497825344 550559296 551216484 551733121 551827081 552626064 552767121 552814144 552908196 554979364 555639184 555827776 555922084 555969241 560316241 560647684 562733284 562828176 562923076 563635081 564727696 566868481 570302161 570779881 570827664 571879396 572214241 572549184 574848576 574944484 575616064 577729296 577825444 577969681 580424464 581726161 581967376 582305161 582546496 582836164 583657281 583802244 584624041 586705284 586802176 590927481 591511041 591559684 592338244 593312164 593507044 593604496 593848161 594433161 594969664 596629476 596727184 598878784 661209796 661724176 661827076 663526081 663629121 664402176 664505284 664917796 666724041 671224464 671535396 672313041 672935481 673869681 674648676 674856484 674908441 676624144 676728196 676988361 677977444 680114241 680218561 681627664 682202161 682829161 683404164 684816561 685706596 685968481 686859264 687803076 690323076 690428176 690533284 691216681 692426596 692847684 693848281 694744164 694955044 695957161 770506564 770839696 771117361 771228441 771506176 771617284 772339681 773507344 773618596 776848384 780755364 782656576 782768484 782824441 782936361 783328144 784448064 784504081 784616121 788823396 790959376 791634496 791747044 792929281 793605241 793999684 795747681 796707076 796989361 880427584 880546276 881555481 883516176 883635076 884408121 884527081 885538564 885717121 886729284 890306244 891559881 891739044 891858496 892336384 894548281 894847396 895745041 990738576 990927441 991557121 991746064 992817081 993447361 993636484 993888676 994519296 994645444 995907364 996728041 996917476
You should see it straight away: 144.
So making the conditional changes as described yields:
1 4 9 16 25 36 49 121 196 361 484 576 676 784 1849 1936 2304 2401 2601 2704 2809 2916 3969 4624 5625 5929 6724 14161 15376 16384 17161 18496 25281 28561 29241 29584 36481 49284 57121 58081 58564 67081 68121 69696 120409 121801 131769 140625 160801 161604 162409 164836 165649 174724 180625 182329 190969 198916 232324 273529 275625 294849 295936 351649 352836 361201 362404 363609 364816 375769 381924 390625 452929 471969 481636 485809 491401 492804 495616 571536 586756 680625 683929 786769 894916 896809 1304164 1406596 1418481 1503076 1515361 1527696 1535121 1623076 1628176 1726596 1745041 1747684 1758276 1907161 2307361 2316484 2328676 2427364 2515396 2637376 2819041 2859481 2917264 2979076 3504384 3519376 3538161 3709476 3717184 3748096 3767481 3837681 3857296 3924361 3956121 3968064 4528384 4713241 4717584 4726276 4748041 4879681 4937284 4946176 5612161 5626384 5645376 5803281 5827396 5934096 5958481 6713281 6718464 6859161 6906384 6948496 7918596 7958041 8916196 8934121 8946081 12061729 12075625 13053769 13140625 13191424 14032516 14092516 14182756 14197824 14250625 14265729 14386849 15241216 15264649 15350724 15460624 16184529 16353936 17073424 17172736 17230801 17280649 17673616 17690436 18275625 18292729 18352656 18472804 18481401 19061956 19140625 19342404 19351201 19562929 19580625 19686969 19784704 19793601 23020804 23030401 23164969 23193856 23261329 23280625 24186724 24373969 25180324 25230529 25250625 25270729 25482304 25492401 26020201 26030404 26040609 26050816 26142769 26193924 26265625 26460736 26481316 26491609 26563716 26594649 27050401 27060804 27071209 27081616 27373824 27394756 27583504 28121809 28132416 28153636 28376929 28590409 28686736 28793956 29170801 29181604 29192409 29354724 29452329 29560969 29680704 29691601 34140649 34175716 34292736 34374769 35081929 35354916 35390601 35473936 35485849 36590401 37161216 37173409 37185604 37197801 38130625 38241856 38365636 38390416 38452401 38464804 39262756 39287824 39350529 39375625 39463524 39891856 45131524 45252529 45481536 46185616 46253601 46280809 46580625 47141956 47265625 47361924 47582404 47692836 48052624 48121969 48260809 48274704 48385936 49252324 49350625 57032704 57062916 57198969 57274624 57350329 57380625 57486724 58140625 58461316 58491904 58583716 59043856 59243809 59274601 59351616 59382436 59397849 59675625 59783824 67141636 67190809 67272804 67354849 67683529 68161536 68475625 68591524 68790436 69272329 69672409 78251716 78340201 78375609 79263409 79281216 79691329 79780624 89264704 89283601 89340304 120209296 120428676 120516484 120758121 120802081 120824064 121308196 121418361 121528576 130439241 130919364 131308681 131423296 131928196 132434064 132503121 132756484 132848676 140209281 140612164 141824281 142301041 143736121 143808064 150528361 150749284 151807041 151979584 152868496 153958464 160757041 161518681 161849284 162919696 163609681 163635264 164506276 170537481 170929476 172423161 172712164 173659684 174609796 174927076 175801081 180526096 181548676 181656484 181737361 182547121 182628196 183819364 183846481 184715281 185804161 186978276 187909264 190219264 190826596 193738561 193989184 195608196 195748081 196728676 230402041 231709284 232318564 232806564 232837081 232989696 240529081 240839361 242549476 242705241 242767561 250304041 250937281 251825161 253637476 253956096 254849296 260209161 261436561 261727684 263607696 263705121 264517696 264908176 270306481 272514064 272547081 272613121 272712196 273439296 274929561 276756496 276956164 281434176 281769796 281937681 282946041 282979684 283619281 284968161 285914281 286828096 291419041 291658084 293848164 294946276 296735076 296907361 297838564 298978681 340107364 340919296 341436484 341547361 341917081 342546064 342657121 342768196 343768681 350513284 352538176 352613284 354606561 360658081 361304064 361418121 361836484 363436096 363703041 363817476 365727376 370216081 370909081 371949796 372837481 375623161 376709281 380601081 382515364 383807281 383846464 384905161 385808164 386869561 386948241 390418081 391723264 396846241 450967696 451945081 454627684 454926241 460703296 461218576 461648196 461906064 462637081 462723121 464747364 465739561 471845284 474629796 474847681 480749476 480837184 481407481 482417296 482768784 483516121 483604081 483648064 484616196 484836361 484968484 491908041 493817284 493906176 494706564 495908361 560316241 560647684 562828176 562923076 563635081 564727696 570302161 571879396 572549184 574848576 575616064 581726161 581967376 582305161 582546496 582836164 583657281 584624041 586705284 586802176 590927481 593848161 596727184 671535396 672313041 672935481 673869681 674648676 674856484 676728196 680218561 682829161 683404164 684816561 685706596 685968481 686859264 687803076 690323076 690428176 692426596 692847684 693848281 695957161 782656576 782768484 782936361 784504081 784616121 790959376 792929281 793605241 795747681 796707076 796989361 891858496 894548281 894847396 895745041
Here's that section of code with the change - I suspect it doesn't need explaining ... but for n1 < n2 then the negation of that check is n1 >= n2 and similarly with n2 > n3 then the negation is n2 <= n3...
if (isless && a >= b) {
valid = false;
break;
}
if (!isless && a <= b) {
valid = false;
break;
}
Well, I didn't check every number but this correction seems promising, agreed?
Personally, I think you may have over-complicated how to get the squares. Consider the following:
// square root of lowest perfect square in range
int n = (int)(Math.ceil(Math.sqrt(N)));
// square root of highest perfect square in range
int m = (int)(Math.floor(Math.sqrt(M)));
for (int i=n;i<=m;i++)
{
// Calculate the number to test
long j = i * i;
etc...
For more readability, you might also split the order test into its own function.
Here is a different implementation that uses math, rather than using strings.
/** Test to see if the given number has all digits in increasing order
when read from Most Significant Digit to Least Significant Digit */
private boolean isOrdered(long n)
{
// Preset our current Least Significant Digit so that the first test
// won't fail.
long last = 10;
boolean isLess = false;
while (n > 0)
{
// Get the least significant digit
long digit = n % 10;
// If the digit is not in order compared to the previous LS digit, then return.
// then return false.
if (isLess ? digit <= last : digit >= last)
return false;
isLess = !isLess;
// Remember the current LS Digit.
last = digit;
// Move to the next digit.
n = n / 10;
}
return true;
}

java.lang.ArrayIndexOutOfBoundsException: 4

I am working on extracting values from a tab separated text file into a list in groovy. But am running into the ArrayIndexOutOfBoundsException.
Code
println("Reading File Contents")
def fullArray = new String[31721][4]
def availableArray = new String[1386][2]
def filteredFullArray = new String[1386][5]
String fileContents = new File('beliefs.txt').text
String availableContents = new File('available.txt').text
def count = 0
fileContents.eachLine { line ->
String[] str
str = line.split('\t')
def subCount = 0
for (subCount; subCount < str.length; subCount++) {
fullArray[count][subCount] = str[subCount]
}
count++
}
beliefs.txt
1 Azerbaijan hasOfficialLanguage Azerbaijani_language
2 Augustus hasChild Julia_the_Elder
3 Arthur_Aikin isCitizenOf England
4 Arthur_Aikin diedIn London
5 Alexander_III_of_Russia isMarriedTo Maria_Feodorovna__Dagmar_of_Denmark_
6 Alexander_III_of_Russia hasChild Nicholas_II_of_Russia
7 Alexander_III_of_Russia hasChild Grand_Duke_Michael_Alexandrovich_of_Russia
8 Alexander_III_of_Russia hasChild Grand_Duchess_Olga_Alexandrovna_of_Russia
9 Alexander_III_of_Russia hasChild Grand_Duke_Alexander_Alexandrovich_of_Russia
10 Alexander_III_of_Russia hasChild Grand_Duke_George_Alexandrovich_of_Russia
...
...
...
31719 Minqi_Li isKnownFor Chinese_New_Left
31720 Henry_Bates_Grubb isKnownFor Mount_Hope_Estate
31721 Thomas_Kuhn isKnownFor Paradigm_shift
Running this gives me the following error.
Caught: java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 4
at extractBeliefs$_run_closure1.doCall(extractBeliefs.groovy:19)
at extractBeliefs.run(extractBeliefs.groovy:12)
I am aware of the reason why the above error could occur. But since my array does not exceed the last index and since the error is shown to be at the line fileContents.eachLine { line ->, I am unable to find where this is going wrong.
Any suggestions in this regard will be highly appreciated.
Your initial error is coming from this line (19):
fullArray[count][subCount] = str[subCount]
Line 12 is just elevating the exception as it exits the closure. This definitely indicates you have an extra tab on one line... for debugging purposes, try printing the line to the console before you attempt to load it into the array. That'll help you identify which line has the error.
Try splitting with space
str = line.split('\s+')
instead of
str = line.split('\t')
Better way would be to replace all Multispaces or tabs with the single space first and then split by single space.
line = line.replace("\\s+/g", " ")
str = line.split('\\s+')

searching and replacing content from one file to another file using a keyword using python or java

Locked. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
I have 2 files which contains some data like this!!
File 1 contains:
/begin MENT AE0DAQ0O41 ""
ECU_ADDRESS 0x8111DSCC
ECU_ADDRESS_EXTENSION 0x0
/begin IF_DATA CAN_EXT
120
LINK_MAP "AE0DAQ0O41" 0x8111DSCC 0x0 0 0x2 1 0x2F 0x1
DISPLAY 0 0 655
/end IF_DATA
SYMBOL_LINK "AE0DAQ0O41" 0
/end MENT
File 2 contains:
name value line keyword
.data 80008114+000005 AE0DAQ0O43
.data 80008116+000005 AE0DAQ0O41
.data 80008118+000005 EA0DAQ0O45
.data 8000811a+000005 AE0DAF0O89
Now what we need to do is take a keyword AE0DAQ0O41 and need to search in the next file.
It has some value before the keyword, so we need to take that value 80008116 and need to replace it in
ECU_ADDRESS 0x8111DSCC and also LINK_MAP AE0DAQ0O41 0x8111DSCC 0x0 0 0x2 1 0x2F 0x1 for (0x8111DSCC it needs to be 0x80008116) and save it to FILE 1.
FILE 1 is to be saved like this :
/begin MENT AE0DAQ0O41 ""
ECU_ADDRESS 0x80008116
ECU_ADDRESS_EXTENSION 0x0
/begin IF_DATA CAN_EXT
120
LINK_MAP "AE0DAQ0O41" 0x80008116 0x0 0 0x2 1 0x2F 0x1
DISPLAY 0 0 655
/end IF_DATA
SYMBOL_LINK "AE0DAQ0O41" 0
/end MENT
How do we do that ??? because it has multiple lines like this ????
Thanks in advance!!!!!!!
If you consider your File 2 as a tab seperated value file then you could read the File 1 line by line and then compare the keyword in the file1 with each line in file2.
When you get a match then write another file with the new inputs
Quick and dirty solution:
(Assuming that the inputs are both text files...)
The code creates a dictionary by mining the second file.
The first file is processed line by line and written to the output file after the required modifications.
This is certainly not the best way to go about it.
If you know the exact format of the files, you can optimize the code to run a lot faster.
fout = open('output.txt' , 'w')
beg, ecu, lnk = '/begin','ECU_ADDRESS', 'LINK_MAP'
keyVal = dict()
with open('file2.txt') as f2:
for line in f2:
b = line.split(' ')
newK, newV = b[-1].replace('\n','') , b[-2].split('+')[0]
keyVal[newK] = newV
with open('file1.txt') as f1:
value,keyword = '', ''
for line in f1:
a = line.split(' ')
loc = 0
if beg in a and 'MENT' in a:
keyword = a[a.index(beg)+2]
value = '0x'+keyVal.get(keyword,keyword)
elif ecu in a:
loc = a.index(ecu) + 1
elif lnk in a: loc = a.index(lnk) + 2
else : loc = 0
if loc != 0:
a[loc] = value
a = ' '.join(a)
fout.writelines(a)
fout.close()

Writing a .pdf to sd card in android

When I have a PDF formatted like so (abbreviated)
"JVBERi0xLjQKJf////8KMzIgMCBvYmoKPDwvTGVuZ3RoIDI0ODIKL1N1YnR5cGUgL1hNTAovVHlwZSAvTWV0YWRhdGEKPj4Kc3RyZWFtCjw/eHBhY2tldCBiZWdpbj0n77u/JyBpZD0nVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkJz8+Cjx4OnhtcG1ldGEgeDp4bXB0az0iMy4xLTcwMSIgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iPgogIDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+CiAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iPgogICAgICA8eG1wOkNyZWF0ZURhdGU+MjAxMi0wOS0yNlQxNzoxOTo0OVo8L3htcDpDcmVhdGVEYXRlPgogICAgICA8eG1wOkNyZWF0b3JUb29sPk5pdHJvIFJlYWRlciAyICAoMi4gNS4gMC4gNDApPC94bXA6Q3JlYXRvclRvb2w+CiAgICAgIDx4bXA6TW9kaWZ5RGF0ZT4yMDEyLTA5LTI2VDE3OjE5OjQ5WjwveG1wOk1vZGlmeURhdGU+CiAgICAgIDx4bXA6TWV0YWRhdGFEYXRlPjIwMTItMDktMjZUMTc6MTk6NDlaPC94bXA6TWV0YWRhdGFEYXRlPgogICAgPC9yZGY6RGVzY3JpcHRpb24+CiAg
I am able to write it to the sd card with this code, and then open it.
File file = new File(eqnxPath.getAbsolutePath(), FileId + FileName);
Log.d(TAG, "looking for file...");
if (file.exists())
{
Log.d(TAG, "fiel exists, returning file");
return file;
}
else
{
Log.d(TAG, "file does not exist, making file");
byte[] pdfAsBytes = Base64.decode(this.FileContent, Base64.DEFAULT);
try
{
FileOutputStream os = new FileOutputStream(file);
try
{
os.write(pdfAsBytes);
os.flush();
os.close();
Log.d(TAG, "returning file");
return file;
} catch (IOException e)
{
Log.d(TAG, "IOException...");
e.printStackTrace();
}
} catch (FileNotFoundException e)
{
Log.d(TAG, "FileNotFoundException...");
e.printStackTrace();
}
However when I get a pdf that looks like this (again abbreviated)
%PDF-1.3
%����
1 0 obj
<undefined</Metadata 35 0 R/Pages 2 0 R/Type/Catalog>>
endobj
2 0 obj
<</MediaBox[0.0 0.0 609.12 788.88]/Count 1/Type/Pages/Kids[4 0 R]>>
endobj
4 0 obj
<undefined</Parent 2 0 R/Contents 29 0 R/PieceInfo<undefined</MRC<undefined</Private<undefined</B[1 1 3 1]/I[3 8 300 300]/P[2 0.45]/Q 0.1/S[2 0]/U 0/Y 15/b 20/v 2.31>>/LastModified(D:20121102151622-06'00')>>/PSL<undefined</Private<undefined</V(3.1.902)>>/LastModified(D:20121102211626-00'00')>>>>/MediaBox[0 0.0 609.12 788.88]/Resources<undefined</XObject<</4 28 0 R/5 27 0 R/6 26 0 R/7 23 0 R/8 22 0 R/9 21 0 R/A 20 0 R/B 19 0 R/C 18 0 R/D 17 0 R/E 16 0 R/F 15 0 R/G 14 0 R/H 13 0 R/I 12 0 R/J 11 0 R/K 10 0 R/L 9 0 R/M 8 0 R/N 7 0 R/O 6 0 R/P 5 0 R>>/ProcSet[/PDF/ImageC/ImageI/ImageB]>>/Type/Page/LastModified(D:20121102151622-06'00')>>
endobj
5 0 obj
<undefined</Subtype/Image/Length 20602/Filter/CCITTFaxDecode/ImageMask true/BitsPerComponent 1/Width 2320/DecodeParms<undefined</K -1/Columns 2320>>/Height 2960/Type/XObject>>stream
����KH�r�f(A�:FD���;!�(CǕ��:!������v�p���������������?w�A�?I<��
#� b��r�P�����������O��̓�Gcb��ɘ�("El%A�#���r��S����3����h� �� �Ȑc"#�.�0DE��2PA�.(A�a�8k�)%���L��#A�c'd�Ȑh*f� `�M �(�f�C6���� ��xN��� �0 xA��� `�A�h�2�&�y�TA�x'A��zp����4L a��� 駬?OO�S�������xA���~��N,'��AƐN-;���i����[�[OD~�Zo��q�"2-��*v��[���m�q�!֓rM�'��g8rC�1�`�����%�G� :!r��"�4fB� G��h�)2c&�tpO��O ��W��GSAA���:�O!�ɧ�`��5��n#�'���zM�ap���_�&�I�J �n��L8OA�����&�=W ��xN���H=7 �=7
�!xV��n����n�]7���I�n��.�����&�[���O[���z�Kn~���_�������I�����V��������Z�������������������������"A����������
���ܫ��������������e��� a������/����H3?���������_�ߐq������������ �7�������O�����/��/�!�E���������"�u���������� D���� ���肋��������� ������� ������U����D����Ai��������������_�����\6i������ݿ�K����V�/� c������_���m���0K���"�zo��������c���������������_�������������_���]��������k����_�~�����������������������D������������w��k�������]���������o�J������E����/ۯ����
�_�Z�BA/��^�Jյ���KJ��/���k��[�յm+Xui��������i{a[K�:lڤ�IZ���Z�����k%
v�
��^�0}���$}��cRCRCH�4�l+iCY
m/�a~
)6� L�� ޖ%�lW��3��ȑ�+ �m)�Hb�a/�?��b�67د
5�?�*����**(1_��5��^�[�2
/����5U_쁈�S[�
.�^ �����P��/��A���
B
n������/�va�Bk
���/�
E�����a4"9���"+�
0� �"�&���Xk�aa��`�!� ���2�=��j� ��A��� ����l��e�A������SǨ�����/�_�������:���������
���a!VSZ��������!*�H7龓��W��̓�
��"Ő1��)%
#�;D��)9!� #AAld �`�>�P�u�"D�
9|�rundefined<azv�z�0����zi����za;���L'�קj�A��OT�7^-�'���4�M��\�� G���v�c�<�P��O �o���I�t��M� ������ �zN�y�0�>�:]=����2�5�:O�Ӿ��ivAA?[�ut���S��7a�^��׽�^���P�N����n��Upa�_X�����
���_ޗ�<����Zo��o�[��U�e�X�����_��7��D�]m������#ٷ��_\��������������/u����������������SP+��������#ڧ���������������{��_����^��]�;���^����h�������W����������������د�n����������i[_��]�ڵ�[]�
����K��J�J����m-���յ��]�xk��J�X/�����1V����0�����V!�����_�h5
o�����P�/��O����P����_R�13��RBL���G"Cn�l���K���d���}kV������eP�����Z��� _�e�*Bl� 0ed�3;Z`��9i*��5%����d�iw�z���u����$�~4�߅��o�Q���������ܧ2H.x�X�џ)��Ρ�p�8S�4g'� 0hGS'�<� �=B`�;��u=< �4�?M4� �S��^�ń���M>/�N���ӽPh�v��� ��#�t�܉�Dt!9���O��"��#�����=$��龃A�n�#�7M�o����I=>��t��U:N����T���}7��[WT�+������ .�_�C�=>��=p����������6c6zVf�ޥ�mB� ��?����D^�j��G������������?������-�_������W������$�$c��L� �ד�����FGJFG���鷷��o���u���u��u_^�/����_��}-���ki}�w�u.���Z�v�����Z�]�~��mv�V��Nڶ��k�Z���I��&����L5`�]�}�`˃�lSTW��LS�HM�����
��ڦ�� �i����a���
a��k�����4���+a�A�^��ӴװX0D�&A�M!iq��D�"#������Sz�~�_j�#���������������s\�
٪3��B��)_Q���-Of��^��������?���������_�������_�%����̔����}��EW���TY�~.�5�q�������������������l����:���#��ddj��\φ%�(RFvjɺ� �Oe;j��Yrundefined<W4���j�ddP3S7�`�DNN!K�����0��H!���j�i����!�:3XR��Ä �0���Au�M���M=>&��MQ>!H�\�At�O��8w
{��Z�
$�%N����7�����6r�7�����u��ץ�5�o#��y tO�6 M6��s1�� ���D��v�".9�k&�O*'�d�e�_�0��t�J��U����Fe'ۗ6�ӧk߄����q�+��i����.�w��_�xA��:�}��&��q��oҽ/�Y��!�ҿ_�?�_��1�����M�?�/�ӏ}�_~�_���T����==iid�������W�׭��7�WO�������������_�o����:�����R�_���!����Azվ���z�ȉ�W���Z�]b����W����o���-k�d?�_������~���M����p�_�$��uMR�&-��)����.��o�.�Ip��������$�����������k_��/y/�W���#�~��M�+�,�7��^��5�� �"_ҧ�����qqZ�ﭨ5#��g�{��|A�Σ���J�/�I/��zm'�un�}��������II�)�����_�t�Hw��}���%_�W���K�'�������
�u|^���'�+��/�v�ߧ聟���9�~�Z���w��Z�/�K�\?��^����}-t����ץ��u���v6֟��������]h/^�ƕ{���]��^�R��mwUV�R�Y�%�yѯ�����ۮ�Z���N����}p�5�]+K����U�����y�vҤ
*�����&��I1\���:!���f�0�|V�/��I5���[ˋ�`��SH��a%�b!�
�Ay����aWZU�`�rG�����A��+��E!q��
/����-��X`�0��\�V��E'���[�_��I?���-yᅿ�_�0[�L,4�Imt��zB"8��������0�
;P���)�,��lE�f4�qZ��^��MUZhDb?����j|��"�\���TY*jFǟ�"!D�b���!.�Fdv��*�NdX!6(�����
0����Y�A���gSDԇ��Û�V�A�x �ja��=5��T���C�����ih4�a=4Ӫ����D��|��[�Ӈpk�޿wڧ��:Dp��O(��¥c׾�?֗OY(|�Q�r;�` �������t6��M���K0�����'ϒƲ��'��"%ԏ��(+��n�+������u���
��A�e֛�oKH_��۽������OW��������[_���RYC��޵kh����I�o&�G�ѪZ����w~�-��?���U/���������i/��W߮��[_�����������|$��.��խ������JA6������^D �ҥ����A��J��*���O���5���~�������o�[�T����m/��_����]r �����R���I%����/��lz/M�����0�����W���\��Pa���O��^G��6��%�����z��a�u���U�D0�9��
���Ҧ�K�p�έ����[����Z��?�3������߂��<��XJ�����Km/�����:�#��I~�R�I�]���^���#������ץ���/����ޕ����A'^�.�/�k~��Z �k��դ��-źJ���~�v����k��AWK�������֯�_��^��U�0�Natˉ��%�ܘ�3�V��0_����y��xk��!���d��)#�undefined<BJ-��1QE��(����A��`�QR�H���jCG
4E�i�װ�
}K1(>�1\;�*��������8[�׿k
��n�[ }�%�
�h��7]���Xk�i�ͅ�+�DG�CB
�pf�����E��d*c��4z�I*���J��P�!��
My method does not work. How would I write this type of .pdf to the sd card? I dont even know what type of data I am looking at...
A PDF "formatted" like in your first example is base64 encoded (an encoding used to transport binary data via a channel that can only handle text properly).
Before writing to card, therefore, it has to be decoded which your method does by means of a ' Base64.decode' call.
A PDF "formatted" like in your second example is already in its unencoded state. Thus, no further decoding is necessary and the data has to be written to card as is.
I hope your member variable ' this.FileContent' is not a string but a byte array, or that its byte array representation is present in yet another member. Otherwise the second, unencoded form surely already is broken.
For this kind of data your method shall not call the base64 decode method before saving but instead save the data as is.

Categories