gpt4 book ai didi

java - 尝试检查猜​​测是否为空时收到异常

转载 作者:行者123 更新时间:2023-12-02 02:09:22 28 4
gpt4 key购买 nike

我开发了一款刽子手游戏,已完成 99%。我只有一个小故障,这个小故障是当用户没有输入猜测并按 Enter 时。

我知道我们无法检查字符是否为空,因此我使用 scanner.nextLine().isEmpty() 检查字符串是否为空。现在,如果我不输入任何内容并按 Enter 键,那么它可以正常工作并输出打印行消息。

但是,如果我输入一个字母并按 Enter 键,那么它会转到下一行,并要求我输入一些内容。如果我输入另一个字母,那么它工作正常,如果我什么都不输入并按 Enter 键,那么我会得到一个异常

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

所以我相信我的问题是 if 语句,我检查字符串是否为空,如果不是,则继续检查字符:

            if (scanner.nextLine().isEmpty()) {
System.out.println("Your guess seems empty. Please enter in a letter or number");
} else {

char input = scanner.nextLine().charAt(0);

if (input == '-' || Character.isLetter(input) || Character.isDigit(input)) {

if (input == '-') {
weArePlaying = false;
wordCompleted = true;
}
....
}

我如何调整它,以便如果为空字符串,则要求用户再次猜测,否则如果不为空,则继续猜测?

下面是更多代码:

public static void main(String[] args) throws FileNotFoundException {

ArrayList<String> movieList = new ArrayList<>();
File file = new File("C:\\Users\\mmkp1\\Documents\\listofmovies.txt");
Scanner fileScanner = new Scanner(file);
int attempts = 10;
Scanner scanner = new Scanner(System.in);
Random random = new Random();
ArrayList<Character> guessedLetters = new ArrayList<>();

while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
// Reads the whole file
movieList.add(line);
}

boolean weArePlaying = true;
while (weArePlaying) {
System.out.println("Welcome to Guess The Movie Game");

int index = random.nextInt(movieList.size());
String getMovies = movieList.get(index);
char[] randomWordToGuess = getMovies.toLowerCase().toCharArray();
int wordLength = randomWordToGuess.length;
char[] playerGuess = new char[wordLength];
boolean wordCompleted = false;

for (int i = 0; i < playerGuess.length; i++) {
playerGuess[i] = '_';
}

for (int i = 0; i < randomWordToGuess.length; i++) {
if (randomWordToGuess[i] == ' ') {
playerGuess[i] = ' ';
}
}


while (!wordCompleted && attempts != 0) {

printArray(playerGuess);
System.out.println("Number of attempts left: " + attempts);
System.out.println("Your previous guesses were:" + guessedLetters);
System.out.println("Enter a letter or number");

if (scanner.nextLine().isEmpty()) {
System.out.println("Your guess seems empty. Please enter in a letter or number");
} else {

char input = scanner.nextLine().charAt(0);

if (input == '-' || Character.isLetter(input) || Character.isDigit(input)) {

if (input == '-') {
weArePlaying = false;
wordCompleted = true;
}
....
}

最佳答案

您错误地使用了扫描仪并调用了 nextLine() 两次。

if (scanner.nextLine().isEmpty()) {            // First call.
System.out.println("empy");
}
else {
char input = scanner.nextLine().charAt(0); // Second call.
}

假设扫描仪有一条对我们来说很有趣的线。第一次调用将获取它,发现它不为空并将其丢弃。第二次调用不会得到我们感兴趣的行,但会尝试获取(可能是空的)后续行。

尝试访问空字符串的第一个字符会导致您发布的 StringIndexOutOfBoundsException

您可以借助局部变量轻松修复代码,如下所示:

String line;
char input;

line = scanner.nextLine();
if (line.isEmpty()) {
System.out.println("empy");
return;
}

input = line.charAt(0);
// ...

关于java - 尝试检查猜​​测是否为空时收到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50253950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com