2014-12-10 1 views
2

파일에서 읽고 모든 내용을 저장하고 있습니다. 전체 프로그램은 정상적으로 실행되었지만 파일의 마지막 줄을 읽지 않았으며 일을 시도 할 때 이상한 상황이 발생했을 때 파일의 마지막 줄은 "END_OF_FILE"입니다. 문자열로 저장하려고했는데 오류 메시지가 나타나면 작동하지 않습니다. 그러나 이제는 모든 코드를 코드에서 제거하고 프로그램을 완전히 실행하지 않습니다. 마지막으로 출력되는 것은 작업 예입니다. 내 마지막 루프에있다. 여기java.util.NoSuchElement 파일을 검색하는 중 오류가 발생했습니다.

import java.io.*; 

import java.util.Scanner; 
public class hurry2 { 

    public static void main (String [] args) throws IOException 
    { 
     File candidates = new File("cipcs115.txt"); 
     Scanner scan = new Scanner(candidates); 
     Scanner user = new Scanner(System.in); 
     String state = scan.next(); 
     int numOfCan = scan.nextInt(); 
     String what = scan.nextLine(); 

     String stateWi ; 
     String stateAl ; 
     String stateDe ; 

     int numOfCanWi ; 
     int numOfCanAl ; 
     int numOfCanDe ; 

     Candidate [] candiIl ; 
     Candidate [] candiWi; 
     Candidate [] candiAl ; 
     Candidate [] candiDe; 
     hurry2 cill = new hurry2(); 

     candiIl = new Candidate[numOfCan]; 
     for (int i=0; i<numOfCan; i++) 
     { 
      String three = scan.next(); 
      String four = scan.next(); 
      String five = scan.next(); 
      int six = scan.nextInt(); 
      double seven = scan.nextDouble(); 
      String eight = scan.nextLine(); 

      Candidate night = new Candidate (three,four,five,six,seven,eight); 
      candiIl [i] = night ; 
      System.out.println(night.getName() + " " + night.getMotto()); 
     } 

     stateWi = scan.next(); 
     numOfCanWi = scan.nextInt(); 

     candiWi = new Candidate[numOfCanWi]; 
     for (int i=0; i<numOfCanWi; i++) 
     { 
      String three = scan.next(); 
      String four = scan.next(); 
      String five = scan.next(); 
      int six = scan.nextInt(); 
      double seven = scan.nextDouble(); 
      String eight = scan.nextLine(); 

      Candidate night = new Candidate (three,four,five,six,seven,eight); 
      candiWi [i] = night ; 
      System.out.println(night.getName() + " " + night.getMotto()); 
     } 

     stateAl = scan.next(); 
     numOfCanAl = scan.nextInt(); 

     candiAl = new Candidate[numOfCanAl]; 
     for (int i=0; i<numOfCanAl; i++) 
     { 
      String three = scan.next(); 
      String four = scan.next(); 
      String five = scan.next(); 
      int six = scan.nextInt(); 
      double seven = scan.nextDouble(); 
      String eight = scan.nextLine(); 

      Candidate night = new Candidate (three,four,five,six,seven,eight); 
      candiAl [i] = night ; 
      System.out.println(night.getName() + " " + night.getMotto()); 
     } 

     stateDe = scan.next(); 
     numOfCanDe = scan.nextInt(); 

     candiDe = new Candidate[numOfCanDe]; 
     for (int i=0; i<numOfCanAl; i++) 
     { 
      String three = scan.next(); 
      String four = scan.next(); 
      String five = scan.next(); 
      int six = scan.nextInt(); 
      double seven = scan.nextDouble(); 
      String eight = scan.nextLine(); 

      Candidate night = new Candidate (three,four,five,six,seven,eight); 
      candiDe [i] = night ; 
      System.out.println(night.getName() + " " + night.getMotto()); 
      System.out.println("yes"); 
     } 

     System.out.println(" what would you like to do? >"); 
     String option = user.next(); 
     switch (option) 
     { 
      case "m": 
      case "M": 
       cill.menu(candiIl); 
       break; 
     } 
     scan.close();  
    } 

    public static void menu(Candidate [] rod) 
    { 
     hurry2 cill1 = new hurry2(); 
     Scanner menus = new Scanner(System.in); 
     System.out.print("What would you like to do next >"); 
     String option1 = menus.next() ; 
     switch (option1) 
     { 
      case "l" : 
      case "L" : 
       cill1.listall(rod); 
       break ; 
     } 
     return ; 
    } 
    public static void listall(Candidate [] yes) 
    { 
     int numOfCan = 3; 
     for (int s =0; s<numOfCan;s++) 
      System.out.println(yes[s].getName() + yes[s].getParty() + yes[s].getMotto()); 
     return ; 
    } 
} 

내 전체 오류 메시지가 다시이 프로그램은 내가 배열 캉디드에 대한 루프 마지막에 포함 된 단어 예,까지 모든 출력은 다음과 같습니다

여기 내 코드입니다. 도움을 주신 분들께 미리 감사드립니다.

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Scanner.java:907) 
    at java.util.Scanner.next(Scanner.java:1416) 
    at hurry2.main(hurry2.java:91) 
+0

문장이 완전히 섞여 있고 코드가 너무 커서 직접 이해할 수 없습니다. MCVE로 변경하십시오. http://stackoverflow.com/help/mcve 들여 쓰기를 사용하여 포맷 된 텍스트를 복사하여 IDE에서 붙여 넣기 만하면 대부분의 IDE가 올바르게 포맷팅되고 들여 쓰여집니다. – specializt

답변

2

당신이 scan.next() 방법은 scan.hasNext() 방법으로 확인하지 않고 호출하는 복수를 위해 시도하는 것 같다.

next() 방법으로 전화하기 전에 scan.hasNext()을 시도하십시오.

+0

글쎄, 그건 안전을위한 것이지만, 파일이 어떻게 설정되었는지 알았고, 모든 것을 정확하게 저장하고있다. 각 모양의 끝에서 전달 된 값의 각 하나를 출력하여 모든 것이 올바르게 저장되어 있음을 볼 수 있습니다. 문제는 내 첫 번째 스위치 명령문 바로 전에 발생합니다. System.out.println ("다음에서 무엇을 하시겠습니까?")은 절대로 출력되지 않습니다. @hemanth – mrod2893

+0

왜 numOfCanAl을위한 두 개의 for 루프가 있습니까? – hemanth

0

가 출력 마지막 네

루프가 입력하는 것

, 인쇄 yes하고 다음 반복에서 예외를 던지는 작품이다. 스캐너에 들어가기 위해 토큰이 나오는 순간도 있습니다.

관련 문제