2013-08-13 5 views
2

항상 n00b 질문을하고 있기 때문에 사과드립니다.하지만 실제로 도움을받을 수 있습니다. 어쨌든 사전에서 특정 길이의 단어 만 해시 집합 인 변수 단어로 가져 오려고합니다. 프로그램을 실행하고 내 단어를 일컬어 문자열의 해시 세트를 인쇄하려고 할 때. 나는 콘솔에서 아무 것도 얻지 못하고 프로그램이 멈추지 않는다. 이 문제를 어떻게 해결할 수 있습니까? 추신 또한 JOptionPane 코드의 일부분이 충분히 잘려 졌음을 알고 있지만 오류가 없으므로 그 요점을 알 수 있습니다. 감사! 알렉스텍스트에서 단어 가져 오기

public void inputWords() 
    { 
     try 
     { 
     frame = new JFrame("Hangman"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(300,300); 
     frame.setVisible(true); 
     input = new Scanner(new FileInputStream("dictionary.txt")); 
     wordLength = Integer.parseInt( JOptionPane.showInputDialog(null,                   
     String importedWords = input.nextLine(); 
     while(stillHasWords==true) 
     { 
      if(importedWords.length()==wordLength) 
      { 
       words.add(importedWords); 
      } 

      else 
      { 

      } 

     } 

    } 

    catch(FileNotFoundException f) 
    { 
     System.out.println("File does not exist."); 
     System.exit(0); 
    } 

    catch(NoSuchElementException q) 
    { 
     stillHasWords=false; 
    } 


    public static void main(String[] args) 
     { 
    EvilHangman j = new EvilHangman(); 
    System.out.println(stillHasWords); 
    j.inputWords(); 
    System.out.println(words + " "); 

     } 

} 

답변

3

에 관한 :

while(stillHasWords==true) 
    { 
     if(importedWords.length()==wordLength) 
     { 
      words.add(importedWords); 
     } 

     else 
     { 

     } 

    } 

내가 않습니다 (importedWords를) words.add 무엇인지, 그러나 당신이 겪고있는 문제에 대한 가장 중요한 아니에요,

질문 : 어디 루프 내부에서 stillHasWords를 변경합니까?
답변 : 그렇지 않으므로 루프가 끝나지 않습니다.

내가 처음이 while 루프 여담으로

를 해결하는 것이 좋습니다, 그것은 == 사용하여 while 루프에서 진정한 않도록하는 것이 좋습니다 대신 단순히 부울 테스트 :

while (stillHasWords) { 
    // add a word 
    // change stillHasWords to false if we've run out of words 
} 

편집
당신의 상태 :

아직도 캐치 단어 변경 (예외 : NoSuchElementException q를)

이 while 루프의 내부 을 기록에는 catch 블록이 없으며, 그래서 나는 여전히 stillHasWords 값 기반 while 루프 의 내부 변경할 수 없습니다 제출이있다 지금까지 게시 한 코드를. 더 많은 관련 코드가 있다면 물론 보여 주어야합니다. 그렇지 않으면 이 표시되지 않은 코드가 잘못되었을 수도 있습니다. 최고 게시글 SSCCE

+0

catch (NoSuchElementException q)의 단어가 여전히 변경되었습니다. – Amloelxer

+0

@Amloelxer : 편집을 참조하십시오. –

+0

정말 고마워요! 죄송합니다. "나는 당신이 무엇이 될지 추측하게 만들었습니다." 다음 번에 더 좋은 질문을 올리겠습니다. – Amloelxer

관련 문제