Java 方法无效,导致程序挂起
Java method not working, causing program to hang
您好,我正在编写一个程序来分析一段文本,特别是读取一个文件并计算该文件中的行数、单词数和数字数。
我尝试在方法中的单独 class 中计算行数,然后尝试在主 class 中调用该方法以打印文件中的总行数,然而,这并没有像我预期的那样工作,并且在我尝试调用行计数方法时导致程序挂起。非常感谢任何帮助!
主要Class:
package cw;
import java.util.Scanner;
public class TextAnalyser {
public static void main(String[] args) {
LineCounter object = new LineCounter();
Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
int totalNumbers = 0;
int totalDigits = 0;
int totalWordCount = 0;
int totalLines = 0;
while (sc.hasNext()) {
totalWordCount++;
if (sc.hasNextInt()) {
totalDigits += sc.nextInt();
totalNumbers++;
}
else {
sc.next();
}
}
System.out.println("The total of all digits added = " + totalDigits);
System.out.println("The total number of digits in the file = " + totalNumbers);
System.out.println("The total number of words in the file = " + totalWordCount);
object.TotalLines();
}
}
行计数class:
package cw;
import java.util.Scanner;
//Class that counts the lines in a file.
public class LineCounter {
public static void TotalLines() {
Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
System.out.println("hi");
int linetotal = 0;
while (sc.hasNextLine()) {
linetotal++;
}
System.out.println(linetotal);
}
}
hasNextLine() 只会告诉您是否有下一行 - 它不会读取它。所以你站在开头,一次又一次地问 "is there a next line?" ...你应该尝试通过 nextLine();
实际阅读它
just add
这个 line of code
:
while(sc.hasNextLine()){
String sentence = sc.nextLine();
linetotal++;
}
在您的 public static void TotalLines(){
方法中...
这样你actually get the next line
,not only
询问你的while
中是否存在另一行(always
returns true
然后你 never
退出 while 循环!)
您好,我正在编写一个程序来分析一段文本,特别是读取一个文件并计算该文件中的行数、单词数和数字数。
我尝试在方法中的单独 class 中计算行数,然后尝试在主 class 中调用该方法以打印文件中的总行数,然而,这并没有像我预期的那样工作,并且在我尝试调用行计数方法时导致程序挂起。非常感谢任何帮助!
主要Class:
package cw;
import java.util.Scanner;
public class TextAnalyser {
public static void main(String[] args) {
LineCounter object = new LineCounter();
Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
int totalNumbers = 0;
int totalDigits = 0;
int totalWordCount = 0;
int totalLines = 0;
while (sc.hasNext()) {
totalWordCount++;
if (sc.hasNextInt()) {
totalDigits += sc.nextInt();
totalNumbers++;
}
else {
sc.next();
}
}
System.out.println("The total of all digits added = " + totalDigits);
System.out.println("The total number of digits in the file = " + totalNumbers);
System.out.println("The total number of words in the file = " + totalWordCount);
object.TotalLines();
}
}
行计数class:
package cw;
import java.util.Scanner;
//Class that counts the lines in a file.
public class LineCounter {
public static void TotalLines() {
Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
System.out.println("hi");
int linetotal = 0;
while (sc.hasNextLine()) {
linetotal++;
}
System.out.println(linetotal);
}
}
hasNextLine() 只会告诉您是否有下一行 - 它不会读取它。所以你站在开头,一次又一次地问 "is there a next line?" ...你应该尝试通过 nextLine();
实际阅读它just add
这个 line of code
:
while(sc.hasNextLine()){
String sentence = sc.nextLine();
linetotal++;
}
在您的 public static void TotalLines(){
方法中...
这样你actually get the next line
,not only
询问你的while
中是否存在另一行(always
returns true
然后你 never
退出 while 循环!)