1

I'm trying to read a file called "CityData.txt" that just has a list of city names in it, one on each line. I've been using scanner in the past to read Strings from a file, and am using it to read ints from another file in this same program, however it doesn't seem to be reading anything from the file.

int counter2 = 0;
File strFile = new File("CityData.txt");
Scanner strScanner = new Scanner(strFile);
Scanner strCountScanner = new Scanner(strFile);

while ((strScanner.hasNext() == true)) {
    System.out.println(strScanner.nextLine());
    counter2++;
}

System.out.println("This is counter2: " + counter2);
String[] array2 = new String[counter2];

while ((strCountScanner.hasNext() == true)) {
    for (int i = 0; i < counter2; i++) {
        array2[i] = strCountScanner.nextLine();
    }
}

Ideally, counter2 will tell me how many cities are in the file, and I'll then populate array2 with them. However, counter2 remains at 0 after the program has been run. I've been fiddling with this for a while, and am hoping that maybe I've just missed something silly.

Thanks

4
  • 2
    == true is redundant. Remove it. Commented Dec 14, 2017 at 0:27
  • 3
    Don't mix hasNext() with nextLine(). Use hasNextLine() with nextLine(), or use hasNext() with next(). Commented Dec 14, 2017 at 0:30
  • 1
    Works fine for me. Are you sure you have the file in the right place? Check if the file exists in the path printed with this: System.out.println("Working Directory = " + System.getProperty("user.dir")); Commented Dec 14, 2017 at 0:33
  • 1
    By the way, only one Scanner is needed. There are other ways to read the number of lines in a file Commented Dec 14, 2017 at 0:58

3 Answers 3

1

You are trying to add cities to an array?

public static void readText throws FileNotFoundException {
    ArrayList lines = new ArrayList();          
    Scanner scan = new Scanner(new File("CityData.txt"));
    while(scan.hasNextLine()){
        String line = scan.nextLine();
        lines.add(line);
    }
}

or a stream in 8

 Stream <String> lines = Files.lines(Paths.get("c:\\demo.txt"));
            lines.forEach(System.out::println);
            lines.close();
Sign up to request clarification or add additional context in comments.

1 Comment

you can cast it back then array2 = lines.stream().toArray(String[]::new);
1

Ideally I would avoid two loops and just use an ArrayList for this purpose. This can give you count as well as the flexibility to make the array more dynamic. Also I would enclose Scanner in try with resources block as it closes the resource itself. Here is the code for reference.

    File strFile = new File("CityData.txt");
    try (Scanner strScanner = new Scanner(strFile)) {

        ArrayList<String> arrayList = new ArrayList<>();

        while (strScanner.hasNext()) {
            arrayList.add(strScanner.nextLine());
        }

        System.out.println("number of cities is " + arrayList.size());
        System.out.println("cities are " + arrayList);
    }catch (Exception e){
        e.printStackTrace();
    }

Comments

1

Since you are reading in string, using hasNextLine() will be more appropriate. You can try the code below, it should work as intended. HTH.

int counter2 = 0;
File strFile = new File("CityData.txt");
Scanner strScanner = new Scanner(strFile);
Scanner strCountScanner = new Scanner(strFile);

while((strScanner.hasNextLine() == true)) {
    System.out.println(strScanner.nextLine());
    counter2++;
}

System.out.println("This is counter2: " + counter2);
String[] array2 = new String[counter2];

while((strCountScanner.hasNextLine() == true)) {
    for (int i = 0; i < counter2; i++) {
        array2[i] = strCountScanner.nextLine();
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.