简体   繁体   English

使用扫描仪读取Java中的字符串

[英]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. 我正在尝试读取一个名为“ CityData.txt”的文件,其中仅包含一个城市名称列表,每行一个。 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. 理想情况下,counter2会告诉我文件中有多少个城市,然后用它们填充array2。 However, counter2 remains at 0 after the program has been run. 但是,程序运行后,counter2仍为0。 I've been fiddling with this for a while, and am hoping that maybe I've just missed something silly. 我已经摆弄了一段时间,并希望也许我只是错过了一些愚蠢的东西。

Thanks 谢谢

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 或8中的视频流

 Stream <String> lines = Files.lines(Paths.get("c:\\demo.txt"));
            lines.forEach(System.out::println);
            lines.close();

Since you are reading in string, using hasNextLine() will be more appropriate. 由于您正在读取字符串,因此使用hasNextLine()更合适。 You can try the code below, it should work as intended. 您可以尝试下面的代码,它应该可以正常工作。 HTH. 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();
    }
}

Ideally I would avoid two loops and just use an ArrayList for this purpose. 理想情况下,我将避免两个循环,而仅使用ArrayList即可。 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. 另外,我会在尝试使用资源块的同时附上Scanner,因为它会关闭资源本身。 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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM