-1

I'm building a graph that lists ranking, name, an old statistic, a new statistic, and the change in them ( in that order). I have so far used the code

for (int rank = 0; rank < 5; rank++) {
    String name = scnr.next();
    int pop16 = scnr.nextInt();
    int pop11 = scnr.nextInt();
    canada.add(new Jurisdiction (name, pop16, pop11));
    System.out.printf(format1, rank+1, canada.get(rank).getName(), canada.get(rank).getPop16(), canada.get(rank).getPop16(), canada.get(rank).calculateChange());
}

to find the first 5 instances however when it tries to read a name with more than one word for example "West End" or "New England" it stops. I'm trying to figure out just exactly how I can get past this issue using different variations of the next() method or if theres something else I can do to solve my problem.

AND before you ask the text file im using is formatted specifically so it goes

stringName   intOldStat   intNewStat

so using nextLine() isnt really an option.

I need 13 instances of the different names and stats for reference.

Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • Please show us an example input file. – Stephen C Nov 16 '20 at 14:10
  • Use `scnr.nextLine()` instead of `scnr.next()` because `scnr.next()` will read only `West` from `West End`. – Arvind Kumar Avinash Nov 16 '20 at 14:11
  • @ArvindKumarAvinash - He already said he can't ... but we need to see some example lines to understand why. – Stephen C Nov 16 '20 at 14:12
  • 2
    Why is this duplicate linked? It does not really represent the question. @ArvindKumarAvinash – maloomeister Nov 16 '20 at 14:15
  • Are all values from different categories/columns (`stringName intOldStat intNewStat`) always separated by 3 spaces? Are those 3 spaces only between those categories or can they appear inside `stringName`? – Pshemo Nov 16 '20 at 14:20
  • @Pshemo Where did the 3 spaces come from? When looking at the edit, didn't you add those? – maloomeister Nov 16 '20 at 14:23
  • @maloomeister I added code formatting to example from OP's original post (without it all continues spaces ware printed as one space by browser). – Pshemo Nov 16 '20 at 14:26
  • 1
    `Scanner` is a terrible API that looks simple but using it is never simple. Just avoid it. Use `BufferedReader` and `readLine` and then methods like `String.split()` to split the line into the desired parts. – Joachim Sauer Nov 16 '20 at 14:34

1 Answers1

1

OK, so your input file syntax is ... unfortunate ... because there no obvious way to know when the stringName finishes and the intOldStat starts. If have the option, the best solution is (IMO) to change the file syntax; e.g. to use a field delimiter other than a space ... which is ambiguous.

(And it is worse that you think. There are genuine recognized placenames that have numbers in their names; e.g. "Town of 1770").)

So how to deal with this? Here are some ideas:

  • Use Scanner.hasInt() and a loop and consume / reassemble the words of a place name until you get a number. (But the fails for 1770!)

  • Read a complete line and then use String.split to split into words. Then treat the last two words in the array as the "stat" values and turn them into numbers using Integer.parseInt.

  • Read a complete line and parse it using a regex with groups to match the 3 components.

Or go crazy and write a parser ...

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096