猿问

Java 程序永远不会完成打印计数

我正在尝试读取文件并计算文件中的行数。每行都有一个电影标题。我注释掉了增加计数的行。如果我取消注释计数的递增,程序就会挂起并且永远不会打印计数。我究竟做错了什么?如果我只打印 while 循环中的每一行,它就可以正常工作。但如果我尝试增加计数则不会。谢谢。


import java.io.FileNotFoundException;

import java.util.Scanner;

import java.io.File;


public class GuessTheMovie {


    public static void main(String[] args) throws Exception {

        try {

            File file = new File("movies.txt");

            Scanner scanner = new Scanner(file);


            // open movies file and count the number of titles in the file

            int count = 0;

            while (scanner.hasNextLine()) {

                //count += 1;

                System.out.println(scanner.nextLine());

            }

            System.out.println(count);

            // create String array of movies such that the size is equal to the number of movies in file.

            //String [] movies = []

        } catch (Exception e) {

            System.out.println("Could not find file.");

        }

    }

}


蝴蝶不菲
浏览 161回答 4
4回答

万千封印

假设您不知道格式如何,movies.txt我建议您执行以下操作:将内容附加到StringBuilderString用的内容做一个StringBuilder得到想要的数据这是一个小演示,对我有用。//make sure you are using the "relative path" to find the movies.txt file(as follows) try (BufferedReader br = new BufferedReader(new FileReader("./movies.txt"))) {       StringBuilder sb = new StringBuilder();       String responseLine = null;       while ((responseLine = br.readLine()) != null) {                     sb.append(responseLine.trim());        }        System.out.println(sb);       //By now you should have all movies & titles to your StringBuilder instance then        String temp = sb.toString();        String movies[] = temp.split(" ");//split the string at "spaces"        System.out.println("First Element: "+movies[0]);        System.out.println("Second Element: "+movies[1]);        System.out.println("Third Element: "+movies[2]);        } catch (Exception e) {        System.out.println("Could not find file."); }这是我的内容movies.txt注意:String按照您想要的方式拆分内容如果你正确分割它movies.length会给你电影的数量输出

手掌心

在这里,我为您准备了工作代码。它正在从 filereader 读取文件,而 st 是等于每一行的参数。在循环中,它将遍历每一行并计算行数。import java.io.FileNotFoundException;import java.io.FileReader;import java.util.Scanner;import java.io.BufferedReader;import java.io.File;public class FileRead {    public static void main(String[] args) throws Exception {         File file = new File("C:\\Users\\Name\\Desktop\\test.txt");         String st;          BufferedReader br = new BufferedReader(new FileReader(file));          int count = 0;         while ((st = br.readLine()) != null) {             count++;         }         System.out.println(count);    }}

弑天下

试试下面的代码import java.io.FileNotFoundException;import java.util.Scanner;import java.io.File;public class GuessTheMovie {    public static void main(String[] args) throws Exception {        try {            File file = new File("movies.txt");            Scanner scanner = new Scanner(file);            // open movies file and count the number of titles in the file            int count = 0;            while (scanner.hasNextLine()) {                count += 1;                scanner.nextLine();            }            System.out.println(count);            // create String array of movies such that the size is equal to the number of movies in file.            //String [] movies = []        } catch (Exception e) {            System.out.println("Could not find file.");        }    }}

肥皂起泡泡

如果您想尝试,可以使用更简单的方式与文件交互:List<String>&nbsp;lines&nbsp;=&nbsp;Files.readAllLines(Paths.get(path),&nbsp;Charset.defaultCharset());之后,您可以与准备好的所有行列表进行交互,或者使用lines.size().
随时随地看视频慕课网APP

相关分类

Java
我要回答