0

I'm trying to read everything from this text file into my heap in java.


    Achernar       537 272
    Alcor          970  25
    Aldebaran      737 356
    Algol          900 325
    Almagest       990 235
    Alpha-Centauri 155  99
    Altair         450 650
    Antares        647 754
    Arcturus       964 579
    Belltrix       545 192
    Beta-Cassiopia 378 525
    Betelgeuse     412 377
    Canopus        218 150
    Capella        424   9
    Delta-Cygnus    42 472
    Deneb          218   8
    Epsilon-Ceti   271 471
    Epsilon-Cygnus  25 458
    Fomalhaut      437 856
    Gamma-Centauri 158 112
    Gamma-Cygnus    56 471
    Groombridge    996 904
    Hyperion       656 127
    Menkar         481 927
    Metis          570 460
    Microscopii    439 998
    Mizar           41 130
    Mu-Draconis    231 870
    Organon        685  58
    Pollux         565   9
    Procyon         74 227
    Regulus        373 106
    Rigel          210 208
    Siris          180 151
    Sol            175 145
    Theta-Persei   937 241
    Vega            33 975
    Vela           334 193
    Zeeman         335 194
    Zeta-Ptolemae  179  34

The regex that I typically use for CSV files (",[ ]*") doesn't work, I've been trying to modify it a little but I'm not sure where to go with it. Using the first line of the .txt as an example it should read Achernar as a String, 537 as a integer (x coordinate), and 272 as an integer (y coordinate).

I tried creating a String 'record', and splitting it into a String array 'tokens' with the using regex's like (""), (" "), and ("[ ]"), but when I try to read the the x coordinate after reading the string, I get an exception because the token array seems to be getting filled with spaces (instead of [Achernar, 537, 272] its something like [Achernar, , , , ... 537, 272].

I tried abandoning the token array and simply reading the inFile.hasNextLine() (which is what I'm using now) and there are no exceptions.. except it skips ever other entry in the text file because my parameter for the while loop is (inFile.hasNextLine()). I understand why this doesn't work, though, because its not just checking the nextLine, it checking if it exists and skipping it entire if it is there.

^^ My current code using that technique

while (inFile.hasNextLine()) {
    entName = inFile.next();
    entX = inFile.nextInt();
    entY = inFile.nextInt();
    Astro temp = new Astro(entName, entX, entY);
    entDistance = temp.calculateDistance(x, y);
    heap.enqueue(new Astro(entName, entX, entY, entDistance));
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Related: [Parsing a fixed-width formatted file in Java](https://stackoverflow.com/q/1609807/12567365), with links to more answers. Especially if you have data where one field can legitimately contain spaces as part of the data. – andrewJames Dec 06 '22 at 20:57
  • 1
    What I would do personally is combine these two approaches. Read each line one at a time, then use `String.split()` and pass in a regular expression that matches whitespace (`\W+`) so that you simultaneously split everything and get rid of the white space. Then you can do any processing you need to such as converting some of them to integers. – Jesse Dec 06 '22 at 21:06

0 Answers0