Arco3
Arco3

Reputation: 343

Using Scanner to read Strings in Java

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

Upvotes: 1

Views: 120

Answers (3)

Akjun
Akjun

Reputation: 370

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();
    }
}

Upvotes: 1

Aggy
Aggy

Reputation: 11

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();
    }

Upvotes: 1

Kevin
Kevin

Reputation: 80

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();

Upvotes: 1

Related Questions