2014-08-30 3 views
-2

너무 간단하다면 사과하지만 현재 Java를 배우고 있는데 막혔습니다. 저는 현재 온라인 스쿨링을하고 있으며 데이터 분석과 관련된 질문에 매달 렸습니다. Java에서 파일의 데이터를 읽는 데 문제가 있음

import java.util.Scanner; 
import java.io.*; 
class PrintnumbertoFile 
{ 
public static void main (String[] args) throws IOException 
{ 
    Scanner scan = new Scanner(System.in); 
    int age=0, IQ, Gender, height; 
    File file = new File("data.txt"); 
    PrintStream print = new PrintStream(file); 
    while (age !=-1) 
    { 
     System.out.print("Age(-1 to exit): "); 
     age= scan.nextInt(); 
     print.println(age); 
     System.out.print("IQ: "); 
     IQ= scan.nextInt(); 
     print.println(IQ); 
     System.out.print("Gender(1 for male, 0 for female): "); 
     Gender= scan.nextInt(); 
     print.println(Gender); 
     System.out.print("Height (Inches): "); 
     height= scan.nextInt(); 
     print.println(height); 
     } 
    print.close(); 
    } 
} 

가 여기에 입력있어 데이터의 : 비록

17 
120 
1 
71 
20 
183 
0 
63 
15 
100 
1 
61 
31 
165 
0 
73 
20 
190 
1 
62 
50 
167 
0 
59 
36 
295 
0 
79 
76 
173 
1 
58 
12 
97 
1 
48 
27 
115 
0 
72 
-1 

자체가 입력 -1 슬프게도 후 닫히지 않습니다 데이터 나 코드의 조각 파일을 만들었습니다. 하지만 그건 문제가 아닙니다.

import java.util.Scanner; 
import java.io.*; 
class getnumbersfromFile 
{ 
public static void main (String[] args) throws IOException 
{ 
    File file = new File("data.txt"); 
    Scanner scan = new Scanner(file); 
    int age=0, IQ, Gender, height, AmountOfPeople = 0; 
    while (age != -1) 
    { 
     System.out.print("Age(-1 to exit): "); 
     age= scan.nextInt(); 
     System.out.println(age); 
     System.out.print("IQ: "); 
     IQ= scan.nextInt(); 
     System.out.println(IQ); 
     System.out.print("Gender(1 for male, 0 for female): "); 
     Gender= scan.nextInt(); 
     System.out.println(Gender); 
     AmountOfPeople++; 
     System.out.print("Height (Inches): "); 
     height= scan.nextInt(); 
     System.out.println(height); 
     } 
    scan.close(); 
    System.out.println("Number of people in the file: " +AmountOfPeople); 
    } 
} 

는 -1을 읽은 후 중지하지 않기 때문에 나는 항상 마지막에 오류가 발생 : 문제는이 코드와 데이터를 읽을 때이다. 나는 또한 퍼팅을 시도했다 :

while (scan.hasNextInt()) 

하지만 아무것도하지 않는다. 이것이 정말로 바보 같으면 다시 사과하지만 선생님이 저에게 준 가이드를 대부분 따르고 있습니다. 도움이되지 않습니다. 여름에도 일하지 않기 때문에 선생님에게 도움을 요청할 수도 없습니다. 어떤 도움을 주시면 감사하겠습니다!

P. 누군가 가장 오래된 사람과 가장 젊은 사람을 파일에서 가져 오는 방법을 알고 있다면 정말 도움이 될 것입니다!

편집 : 죄송합니다. 오류 코드를 추가하는 것을 잊었습니다. 이것은 내가 점점 계속 것입니다 :

Exception in thread "main" java.util.NoSuchElementException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at getnumbersfromFile.main(getnumbersfromFile.java:17) 
+1

"나는 항상 오류 _ 발생합니다."어떤 오류가 있습니까? 또한 모든 변수를 소문자로 시작하는 명명 규칙을 일관되게 유지하십시오. – csmckelvey

+1

문제는 -1을 읽은 후에도 여전히 나머지 변수를 읽으려고한다는 것입니다. 'return'과 같이 루프를 종료 할 필요가 있습니다. 또한 예외가 발생할 경우 스캐너를 건너 뛰지 말고'try' 블록이나'try-with-resources '를 사용하여 스캐너를 닫아야합니다. 마지막으로 디버거를 사용하는 법을 배웁니다. 루프를 밟아 보면 문제가있는 것으로 나타났습니다. – chrylis

답변

0
age= scan.nextInt(); 
if(age==-1){ 
    break; 
} 

한 일식 디버거를 사용하는 학습.

관련 문제