gpt4 book ai didi

java - 程序无法运行,Java、StringBuilder、Array

转载 作者:行者123 更新时间:2023-12-01 18:34:59 26 4
gpt4 key购买 nike

我正在尝试让我的程序运行。但是,我收到了 NullPointerException

如有任何帮助,我们将不胜感激。我不想包含太多代码;所以,我只包含了 eclipse 说错误的 block 。

这是拼图类的构造函数。 (java.lang.NullPointerException)

我纠正了第一个问题并添加了“StringBuilder puzzle = new StringBuilder();”。我现在在同一行收到这些错误。

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.AbstractStringBuilder.setCharAt(Unknown Source)
at java.lang.StringBuilder.setCharAt(Unknown Source)
at Puzzle.<init>(Puzzle.java:55)
at PuzzleGameConsole.main(PuzzleGameConsole.java:22)

第 55 行是原始问题行。它标有“//eclipse 在这一行有错误”

代码:

public class Puzzle
{

private StringBuilder puzzle;

public Puzzle(String theSolution)
{
for (int index = 0; index < theSolution.length(); index++)
{
if (theSolution.charAt(index) != ' ')
{
puzzle.setCharAt(index, '-'); //eclipse had an error on this line
}
else if (theSolution.charAt(index) == ' ')
{
puzzle.setCharAt(index, ' ');
}
}
}
}


public int guessLetter(char letter)
{
int count = 0;

for(int index = 0; index < solution.length(); index++)
{
if(Character.toUpperCase(letter) == solution.toUpperCase().charAt(index))
{
if (puzzle.charAt(index) == '-')
{
puzzle.setCharAt(index, letter);
}
else
{
count = 0;
break;
}
count++;
}
}
return count;
}

public String getPuzzle()
{
return "";
}


public boolean solvePuzzle(String guess)
{
if(guess.toUpperCase().equals(solution.toUpperCase()))
{
return true;
}
else
{
return false;
}
}

}

测试发布在下面。没有任何问题。

import java.util.Arrays;
import java.util.Scanner;


public class PuzzleGameConsole
{
private static final String NEWLINE = "\n";
private static final String MENU = "(1) GUESS A LETTER (2) SOLVE THE PUZZLE (3) QUIT >> ";
private static final String PROMPT_ENTER_LETTER = "\nENTER A LETTER> ";
private static final String PROMPT_SOLVE_PUZZLE = "\nSOLVE THE PUZZLE> ";

private static final int SELCTION_ENTER_LETTER = 1;
private static final int SELCTION_SOLVE_PUZZLE = 2;
private static final int SELCTION_QUIT = 3;


public static void main(String[] args)
{

Scanner in = new Scanner(System.in);

Puzzle puzzle = new Puzzle("JAVA IS FUN");


int selection = 0;

while(selection != SELCTION_QUIT)
{
System.out.printf("PUZZLE: %s\n\n", puzzle.getPuzzle());

System.out.print(MENU);
selection = in.nextInt();
in.nextLine();

if(selection == SELCTION_ENTER_LETTER)
{
System.out.print(PROMPT_ENTER_LETTER);
char letter = in.next().charAt(0);

int letterCount = puzzle.guessLetter(letter);
if(letterCount > 0)
{
printMessageBox(String.format("YES! The letter %s was found %d time(s).", letter, letterCount));
}
else
{
printMessageBox(String.format("Sorry, the letter %s is not available in the puzzle.", letter));
}
}
else if(selection == SELCTION_SOLVE_PUZZLE)
{
System.out.print(PROMPT_SOLVE_PUZZLE);
String guess = in.nextLine();

if(puzzle.solvePuzzle(guess))
{
printMessageBox("Congratulations, you solved the puzzle!");
selection = 3;
}
else
{
printMessageBox(String.format("Sorry, '%s' is not the puzzle. Keep trying!", guess));
}
}
else if (selection != SELCTION_QUIT)
{
System.out.println("INVALID MENU OPTION");
}
}

System.out.println("\nGood Bye!");

in.close();
}

public static void printMessageBox(String message)
{
char [] border = new char[message.length() + 4];
Arrays.fill(border, '-');

StringBuilder result = new StringBuilder();
result.append(NEWLINE);
result.append(border).append(NEWLINE);
result.append("| ").append(message).append(" |").append(NEWLINE);
result.append(border).append(NEWLINE);

System.out.println(result);
}

}

最佳答案

您的问题可能是您从未初始化拼图,也从未为其分配new StringBuilder()。即,

StringBuilder puzzle = new StringBuilder();  

例如,

public class Puzzle
{
private StringBuilder puzzle = new StringBuilder(); // **** add this!

public Puzzle(String theSolution)
{
for (int index = 0; index < theSolution.length(); index++)
{
if (theSolution.charAt(index) != ' ')
{
puzzle.setCharAt(index, '-'); //eclipse had an error on this line
}
else if (theSolution.charAt(index) == ' ')
{
puzzle.setCharAt(index, ' ');
}
}
}
}
<小时/>

更重要的是,您需要学习如何调试 NPE(NullPointerException)的一般概念。您应该仔细检查抛出它的行,找出哪个变量为空,然后追溯到您的代码以查看原因。你会一次又一次地遇到这些,相信我。

关于java - 程序无法运行,Java、StringBuilder、Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22393927/

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