user16870886
user16870886

Reputation:

wordly Guess game Error StringIndexOutofBound error

I am making a wordly type guess game using java swing ,trying to match two word (one from user input and second from txt file) and display it on gui label everything is working perfectly if i used the check_word as pre defined like String check_word = "moizz" but if i try to get the word from txt file a error come , my both word are even same but it throws an error

java.lang.StringIndexOutOfBoundsException: String index out of range: 5

here is my code in submit button

try{
    BufferedReader reader = new BufferedReader(new FileReader("E:\\WordyGame\\src\\wordygame\\targetWords.txt"));
    String line = reader.readLine();
    List<String> words = new ArrayList<String>();
    while(line != null) {
        String[] wordsLine = line.split(" ");
        for(String word : wordsLine) {
            words.add(word);
        }
        line = reader.readLine();
    }

    Random rand = new Random(System.currentTimeMillis());
    //getting random word from txt file
    String randomWord = words.get(rand.nextInt(words.size()));
    System.out.println(randomWord);
    String check_word = randomWord.trim().toLowerCase().toString();
    
    
    
    //getting user input
     String take_word = type_word.getText().toLowerCase().toString();
     System.out.println(take_word);
     
     //dividing word into char and then each char to strings
     //user input word 
      char first = take_word.charAt(1);
      String first1 =  String.valueOf(first);
      char second = take_word.charAt(2);
      String second1 =  String.valueOf(second);
      char third = take_word.charAt(3);
      String third1 =  String.valueOf(third);
      char fourth = take_word.charAt(4);
      String fourth1 =  String.valueOf(fourth);
      char fifth = take_word.charAt(5);
      String fifth1 =  String.valueOf(fifth);
      
      //random word from file
      
      char first2 = check_word.charAt(1);
      String first22 =  String.valueOf(first);
      char second2 = check_word.charAt(2);
      String second22 =  String.valueOf(second);
      char third2 = check_word.charAt(3);
      String third22 =  String.valueOf(third);
      char fourth2 = check_word.charAt(4);
      String fourth22 =  String.valueOf(fourth);
      char fifth2 = check_word.charAt(5);
      String fifth22 =  String.valueOf(fifth);
      
      
      
      
     
    if(take_word.length() < 7) 
    {
        if(first1.trim().equals(first22))
        {
                          word_1.setText(first1);
                          word_1.setBackground(Color.yellow);
        }
        else if(second1.trim().equals(second22))
        {

                          word_2.setText(second1);
                          
        }
        if(third1.trim().equals(third22))
        {
                          
                          word_3.setText(third1);
        }
        if(fourth1.trim().equals(fourth22))                
        {
                         
                          word_4.setText(fourth1);
                          
                          
        }
        if(fifth1.trim().equals(fifth22))
         {
                         
                          word_5.setText(fifth1);
                          //displaying charchter on GUI
        }                                  
    }
     else
     {
         JOptionPane.showMessageDialog(this,"only 5 char word are allowed Number");
     }
} catch (Exception e) {
        System.out.println(e);
}

can someone help me out i tried different things but this error isn't solved

Upvotes: 0

Views: 61

Answers (0)

Related Questions