言语猜测游戏错误StringIndexOutOfbound错误
我正在使用java swing制作一个言语类型的猜测游戏,试图匹配两个单词(一个来自用户输入,第二个来自txt文件),并在GUI标签上显示它,如果我使用check_word作为预定的check_word,例如字符串check_word = = “ Moizz”,但是如果我试图从TXT文件中获取错误,我的两个单词都相同,但是
java.lang.StringIndexOutOfBoundsException: String index out of range: 5
在此处抛出了一个错误,这是我在提交按钮中的代码
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);
}
可以帮助我,我尝试了不同的事情,但是这个错误是t解决了
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论