2012-11-04 3 views
0

나는 클래스를위한 작은 프로젝트를 해왔다. 문제없이 완벽하게 실행되지만, 클래스의 자동 테스터들과 비교해 보면, 2 라인은 오류를 찾을 수 없다. 과정의 직원에게 질문하는 것은 아무도 없을 때 선을 검사하려고하기 때문일 수 있다고 말하지만 모든 스캔을 인쇄하려고 시도했지만 그와 같은 것을 발견하지는 못했습니다. 그건 내 코드에서 내가 가진 모든 검사의 :스캔 라인이 이탈하고 있습니까?

Scanner sc = new Scanner(System.in); 
    String sentence; 
    int choice; 

    System.out.println("Please enter a sentence:"); 
    sentence = sc.nextLine(); 

    printMenu(); // calls a function to print the menu. 

    // gets the require action 
    System.out.println("Choose option to execute:"); 
    choice = sc.nextInt(); 
    sc.nextLine(); 

(나는 마지막 sc.nextLine로 및없이 시도)

static void replaceStr(String str) 
{ 
    String oldWord, newWord; 
    Scanner in = new Scanner(System.in); 

    // get the strings 
    System.out.println("String to replace: "); 
    oldWord = in.nextLine(); 
    System.out.println("New String: "); 
    newWord = in.nextLine(); 

    // replace 
    str = str.replace(oldWord, newWord); 
    System.out.println("The result is: " + str); 
    in.close(); 
} 
static void removeNextChars(String str) 
{ 
    Scanner in = new Scanner(System.in); 
    String remStr; // to store the string to replace 
    String tmpStr = ""; //the string we are going to change. 
    int i; // to store the location of indexStr 

    // gets the index 
    System.out.println("Enter a string: "); 
    remStr = in.nextLine(); 
    i=str.indexOf(remStr); 

    in.close(); // bye bye 

    if (i < 0) 
    { 
     System.out.println("The result is: "+str); 
     return; 
    } 

    // Build the new string without the unwanted chars. 
    /* code that builds new string */ 

    str = tmpStr; 
    System.out.println("The result is: "+str); 
} 

라인이 여기에 누설 할 수있는 방법 어떤 생각?

+0

정확한 오류 란 무엇인가요? –

+0

[스캐너 No 라인 발견 예외] (http://stackoverflow.com/questions/7688710/scanner-no-line-found-exception) –

+0

Richard - 코드가 오류를 제공하지 않으며, 예상대로 작동하지 않습니다. . 이 클래스의 자동 테스터는 "No line found"라고 말하면서 더 이상의 세부 사항을 언급하지 않습니다. 그리고 그래, 나는 이미 그것을 보았다. 하지만 도와 줘서 고마워. – Nescio

답변

4

여기에 문제가 있습니다. 여러 위치에서 in.close();을 사용하고 있습니다 (마지막 문은 replaceStr 방법이고 중간은 removeNextChars 방법입니다). close() 메서드를 사용하여 Scnaner를 닫으면 InputStream (System.in)도 닫힙니다. InputStream은 프로그램에서 다시 열 수 없습니다. public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**

모든

예외는 NoSuchElementException에 발생합니다 가까운 스캐너 후 시도를 참조하십시오.

프로그램이 완료되면 스캐너를 한 번만 닫으십시오.

편집 : 스캐너 닫기/사용 :이 행해져 Yout 주요 기능에

는 :

Scanner sc = new Scanner(System.in); 
    .... 
    ..... 
    replaceStr(Scanner sc, String str); 
    ..... 
    .... 
    removeNextChars(Scanner sc ,String str); 
    .... 
    .... 
    //In the end 
    sc.close(); 


static void replaceStr(Scanner in, String str){ 
    //All the code without scanner instantiation and closing 
    ... 
} 

static void removeNextChars(Scanner in, String str){ 
    //All the code without scanner instantiation and closing 
    ... 
} 

당신은 모두 잘되어야합니다.

+0

아, 그렇다면 스캐너를 함수 내부에서 어떻게 사용합니까? – Nescio

+0

@Nescio 그냥 닫지 마십시오 –

+0

스캐너를 함수의 인수로 전달하십시오. 대답을 업데이트하겠습니다. –