Java程序从不完成打印计数

Java程序从不完成打印计数,java,Java,我试图读取一个文件并计算文件中的行数。每行都有一个电影标题。我注释掉了递增计数的行。如果我取消对计数递增的注释,程序将挂起,并且从不打印计数。我做错了什么?如果我只打印while循环中的每一行,效果很好。但如果我尝试增加计数,则不会。多谢各位 import java.io.FileNotFoundException; import java.util.Scanner; import java.io.File; public class GuessTheMovie { public s

我试图读取一个文件并计算文件中的行数。每行都有一个电影标题。我注释掉了递增计数的行。如果我取消对计数递增的注释,程序将挂起,并且从不打印计数。我做错了什么?如果我只打印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.");
        }
    }
}
试试下面的代码

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> lines = Files.readAllLines(Paths.get(path), Charset.defaultCharset());
List lines=Files.readAllLines(path.get(path),Charset.defaultCharset());

之后,您可以与所有行的准备好的列表进行交互,或者使用
lines.size()

获取计数。在这里,我为您准备了工作代码。它从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);
    }

}

假设您不知道如何格式化
movies.txt
,我建议您执行以下操作:

  • 将内容附加到
    StringBuilder
  • 使用
    StringBuilder
  • 获取所需的数据
这是一个小的演示,它为我工作

//确保使用“相对路径”查找movies.txt文件(如下所示)
try(BufferedReader br=newbufferedreader(newfilereader(“./movies.txt”)){
StringBuilder sb=新的StringBuilder();
字符串响应线=null;
而((responseLine=br.readLine())!=null){
某人附加(应答行trim());
}
系统输出打印LN(sb);
//现在,您应该已经将所有电影和标题添加到StringBuilder实例中了
String temp=sb.toString();
字符串电影[]=临时拆分(“”;//在“空格”处拆分字符串
System.out.println(“第一个元素:+movies[0]);
System.out.println(“第二个元素:+movies[1]);
System.out.println(“第三个元素:+电影[2]);
}捕获(例外e){
System.out.println(“找不到文件”);
}
这是我的
movies.txt的内容

注意:拆分
字符串
,以获得所需内容

  • 如果您将其正确拆分,
    movies.length
    将为您提供电影的数量
按照此链接查找有关使用
split()
方法的更多信息

输出


祝你好运:)

取消对行的注释
//count+=1不能对
while
循环产生任何影响。这根本不可能。您是否向我们展示了所有相关代码?能否提供movies.txt文件内容
count+=1
在这里不应该有任何影响。我猜您删除了调用线路的scanner.nextLine()。如果这样做,就永远不会转到下一行,因此它会生成一个无限循环。您不必打印下一行。但是您需要调用nextLine()方法。您的示例实际上可以工作,即使
count+=1
未注释:这在两种情况下都可以工作。可能您不会进入scanner.nextLine()中的下一行。你能分享这两段代码吗?一段有效,另一段无效