저는 (scanner.hasNext()) {} 메소드를 으로 읽은 다음 항상이 프로그램을 실행하는 동안 계속 사용해 왔습니다. 그러나 프로그램 (Eclipse)이 프로세스를 완료하더라도 CPU 사용량이 급격히 증가했음을 알았습니다.이 버튼을 누르기 전까지는 완전히 멈추지 않을 것입니다. buttonImage코드를 사용하여 이클립스 종료 (자바로)
이 기능 (System.exit (0))을 사용하여 프로그램을 중지했지만 버튼을 계속 눌러야합니다.
내 코드에서 프로그램을 중단시키지 않는 결함이 있습니까?
public class HW3
{
/*
Description
*/
public static void main(String[] args) throws FileNotFoundException
{
final File file1 = new File(args[0]);
final File file2 = new File(args[1]);
final Scanner sc1 = new Scanner(file1);
HW3 instance = new HW3();
while (sc1.hasNext()) {
print(instance.splitSentence(sc1.nextLine()));
}
sc1.close();
final Scanner sc2 = new Scanner(file2);
while (sc2.hasNext()) {
}
System.exit(1);
}
public void constructTreeNode(String[] stringArray) {
}
private String[] splitSentence (String sentence) {
int wordCount = 1;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ' ') {
wordCount++;
}
}
String[] arr = new String[wordCount];
wordCount = 0;
String temp = "";
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ' ' || i == sentence.length() - 1) {
arr[wordCount] = temp;
wordCount++;
temp = "";
} else {
temp += sentence.charAt(i);
}
}
return arr;
}
private static void print (String[] arr) {
for(String e : arr) {
System.out.println(e);
}
}
}
동안 (sc2.hasNext())없이 신체에 확실 무한 루프에있을 것인가? 스캐너에서 정보를 읽지 않습니다. Scanner에서 데이터를 실제로 읽어야합니다 (next() 또는 nextLine()이 원하는 것일 것입니다). 그렇지 않으면 파일에서 행을 읽지 않으므로 항상 hasNext()에서 true를 반환합니다. – Zachary
hasNext() 자체가 다음 줄로 건너 뛸 수있는 함수를 가지고 있다고 생각했습니다. 이후로이 문제가 발생하지 않았기 때문에 이것은 전례가없는 오류였습니다. (어쨌든 고마워요 – sbot
이전에 비슷한 실수를 저질렀습니다. 다음 줄로 건너 뛰면 읽지 못할 것입니다. 현재 줄 (또는 줄을 읽으려고하면 다른 줄을 건너 뛸 수 있습니다.) – Zachary