2013-12-07 1 views
0

여기까지의 작업은 다음과 같습니다. 내가 무엇을해야 기본적으로TimeSorter : 스캐너를 사용하여 시간 (hh : mm a.m.) 형식을 읽습니다.

try 
    { 
     Scanner keyb = new Scanner(System.in); 
     System.out.print("Enter the name of the input file-> "); 
     String inFileName = keyb.next(); 
     System.out.print("Enter the name of the output file-> "); 
     String outFileName = keyb.next();    
     ArrayList<Time> roster = new ArrayList<Time>(); 
     Scanner fileIn = new Scanner(new File(inFileName)); 
     while (fileIn.hasNext()) 
     { 
     int hours = fileIn.nextInt(); 
     int minutes = fileIn.nextInt(); 
     String meridian = fileIn.next(); 
     roster.add(new Time(hours,minutes,meridian)); 
     } 
     fileIn.close(); 

은 다른 파일 이름을 순서대로 정렬하고 저장하기 위해 오전 11시 반 형태로 모든 다른 시간이 'appointment.txt'파일을 읽습니다. 하지만 시간과 분 사이의 콜론 (:) 때문에 my while 루프가 시간을 올바르게 읽고 오류를 만들 수 없습니다. while 루프는 어떻게 작동합니까? 당신이 다른 방법으로 fileIn.nextInt()fileIn.next()를 사용 이후 fileIn.hasNext()를 확인하지만, 때문에

+0

도움이 필요하시면 : 입력의 예를 적어주십시오.이 오류에 대한 세부 정보를 출력하십시오 (stacktrace). – reto

답변

0

귀하의 while -loop가 제대로 작동하지 않습니다.

은 당신이 아마 사용하려는 것은 :

while (fileIn.hasNextLine()) { 
    String line = fileIn.nextLine(); 
    String[] bigParts = line.split(" "); 
    String[] timeParts = bigParts[0].split(":"); 
    roster.add(new Time(
     Integer.parseInt(timeParts[0]), 
     Integer.parseInt(timeParts[1]), 
     bigParts[1] 
    )); 
} 

이것은 다음 라인으로 파일 라인을 읽고 그 읽은 라인을합니다. 다음에 텍스트를 세 부분으로 나누어서 먼저 (공백), : (콜론) 순으로 나눕니다.

업데이트 :Integer.parseInt() 원래 추가되었습니다.

관련 문제