2013-01-21 1 views
1

간단한 질문이 있습니다. 온라인 게임을 만들고 있습니다. (GUI로 이동하기 전에 기초를 배우는 중입니다.) 예를 들어 사용자가 콘솔에 'q'를 입력하면 프로그램이 종료됩니다. 꽤),하지만 코드에서 다른 곳에서 오류가 발생하면 (즉, 서버를 찾을 수 없습니다) 모든 연결/시도 된 연결을 닫습니다. 또한 스캐너 개체를 닫고 싶지만 스캐너 개체는 ".next()"메서드를 사용하고 있습니다. ".close()"및 ".reset()"등의 호출을 시도했지만 스캐너 객체를 중지시키지 않습니다. 프로그램을 완전히 닫을 수 있도록 스캐너 개체를 중지하려면 어떻게해야합니까?.next() 메서드를 실행하는 동안 스캐너 개체를 닫으시겠습니까? Java

-Dan

public void commands(){ 
    char[] charArray; 
    while(running){ 
     String input = scanCommands.next(); 
     if(input.length() > 1){ 
      command = 'x'; 
     }else{ 
      charArray = input.toCharArray(); 
      command = charArray[0]; 
     } 
     switch(command){ 
      case 'h': 
       System.out.println("Server Commands: "); 
       System.out.println("Command:  Function:"); 
       System.out.println(" q  - to exit the server."); 
       System.out.println("done."); 
       break; 
      case 'q': 
       System.out.println("Quiting..."); 
       quite(); 
       System.out.println("done."); 
       return; 
      default: 
       System.out.println("Invalid server command"); 
       break; 
     } 
    } 
} 

//the quite method is the one that I call and should close everything, including the 
//scanner object. 

public void quite(){ 
    running = false; 
    try{ 
     IS.close(); 
     OS.close(); 
     send.close(); 
     recieve.close(); 
    }catch(Exception e){ 
     IS = null; 
     OS = null; 
     send = null; 
     recieve = null; 
    } 
    String[] nullNames = new String[7]; 
    name.setArray(nullNames); 
} 

업데이트 : 내가 가진 데 사용 ".close()는"꽤 방법하지만 일을 일부러. 나는 그것을 다시 시도하고 디버그 모드 모드로 들어가고 단지 ".close()"를 유지한다. 'q'또는 무엇이든 입력하면 코드가 계속되고 ".close()"가 실행됩니다. 아무 것도 입력하지 않고 어떻게 닫을 수 있습니까?

+0

여기에 답변을 드릴만한 정보가 충분하지 않습니다. 이 모든 것들은 무엇입니까? 피어 코드는 어떻게 생겼습니까? 그리고 당신이 찾고있는 단어는 '꽤'가 아니라 '그만'합니다. – EJP

답변

0

try 블록에 없으므로 Scanner 객체를 닫지 않습니다. 이 예외가 발생하는 경우

try{ 
    IS.close(); 
    OS.close(); 
    send.close(); 
    recieve.close(); 
} 

scanCommands.close(); 

을 포함해야합니다 포함하여 질문이.

0

빠른 해결 방법으로 다음 메소드를 호출하기 전에 hasNext를 검사 해 볼 수 있습니다.

관련 문제