Java 例外,需要了解什么是错误的?
{ } 我的错误是: 线程主java.lang.StringIndexOutOfBoundsException中的异常: 字符串索引超出范围:0处 java.lang.String.charAtString.java:658 at lab1.lab01.mainlab01.java:42 到底出了什么问题?虽然字符串不像在C中那样完全是char[],但对于某些操作,最好将其视为一个 当您尝试索引到一个空的或未初始化的数组时,您将得到一个类似的异常-ArrayIndexOutOfBoundsException。这意味着您正试图索引到不存在的内容 我们知道大小为N的数组可以索引到位置N-1。对于N=0,我们将通过数学方法索引到位置-1 当你表演角色扮演时,同样的事情也会发生。您正在尝试检索一个不存在的值。换句话说:您的字符串是空的 罪魁祸首是这样的:Java 例外,需要了解什么是错误的?,java,exception,Java,Exception,{ } 我的错误是: 线程主java.lang.StringIndexOutOfBoundsException中的异常: 字符串索引超出范围:0处 java.lang.String.charAtString.java:658 at lab1.lab01.mainlab01.java:42 到底出了什么问题?虽然字符串不像在C中那样完全是char[],但对于某些操作,最好将其视为一个 当您尝试索引到一个空的或未初始化的数组时,您将得到一个类似的异常-ArrayIndexOutOfBoundsExc
private static final int StringIndexOutOfBoundsException = 0;
//values shared within the class
private static int sideA = 0;
private static int sideB = 0;
//main method
public static void main(String [] args)
{
System.out.println("Usage: Supply 2 integer values as triangle sides.");
System.out.println(" A-integer value");
System.out.println(" B-integer value");
System.out.println(" C-attempt a pythagorean calculation");
System.out.println(" Q-quit the program");
String value = null;
String side;
char c = 0;
int s1 =StringIndexOutOfBoundsException;
Scanner scanner = new Scanner(System.in);
boolean carryOn=true;
while(carryOn) //loop until user has finished.
{
side = JOptionPane.showInputDialog(null, "A or B?");
try
{
c =side.charAt(0);
} catch(NullPointerException NP){
System.out.println("Thanks you are done!");
}
switch(c) //which side is the user trying to set
{
case 'Q':
carryOn= false; //quit the program
break;
case 'A':
try{
value = JOptionPane.showInputDialog(null, "Enter A");
sideA = Integer.parseInt(value);
} catch(NumberFormatException NF){
System.out.println("Thats not a number. Type in an integer.");
break;
}
if (sideA<=0)
{
System.out.println("Cannot compute because A is zero. Try another integer");
break;
}
if(sideA>0)
{
System.out.println("You've inputed an A value. That value is "+sideA);
break;
}
break;
case 'B':
try{
value = JOptionPane.showInputDialog(null, "Enter B");
sideB = Integer.parseInt(value);
} catch(NumberFormatException NF){
System.out.println("Thats not a number. Type in an integer.");
break;
}
if (sideB<=0)
{
System.out.println("Cannot compute because B is zero. Try another integer");
break;
}
if(sideB>0)
{
System.out.println("You've inputed an B value. That value is "+sideB);
break;
}
break;
case 'C': //calculate an answer
double temporary = (sideA * sideA) + (sideB * sideB);
if(sideA <=0 && sideB <=0){
System.out.println("You don't have triangle. Try again");
break;
}
double result = java.lang.Math.sqrt(temporary);
System.out.println("The hypotenuse value is "+result);
break;
}
}
System.out.println("Thank you. Goodbye!");
return;
}
c =side.charAt(0);
若边是空的,你们就卡住了
当您转到检索side的值时,如此行所示:
private static final int StringIndexOutOfBoundsException = 0;
//values shared within the class
private static int sideA = 0;
private static int sideB = 0;
//main method
public static void main(String [] args)
{
System.out.println("Usage: Supply 2 integer values as triangle sides.");
System.out.println(" A-integer value");
System.out.println(" B-integer value");
System.out.println(" C-attempt a pythagorean calculation");
System.out.println(" Q-quit the program");
String value = null;
String side;
char c = 0;
int s1 =StringIndexOutOfBoundsException;
Scanner scanner = new Scanner(System.in);
boolean carryOn=true;
while(carryOn) //loop until user has finished.
{
side = JOptionPane.showInputDialog(null, "A or B?");
try
{
c =side.charAt(0);
} catch(NullPointerException NP){
System.out.println("Thanks you are done!");
}
switch(c) //which side is the user trying to set
{
case 'Q':
carryOn= false; //quit the program
break;
case 'A':
try{
value = JOptionPane.showInputDialog(null, "Enter A");
sideA = Integer.parseInt(value);
} catch(NumberFormatException NF){
System.out.println("Thats not a number. Type in an integer.");
break;
}
if (sideA<=0)
{
System.out.println("Cannot compute because A is zero. Try another integer");
break;
}
if(sideA>0)
{
System.out.println("You've inputed an A value. That value is "+sideA);
break;
}
break;
case 'B':
try{
value = JOptionPane.showInputDialog(null, "Enter B");
sideB = Integer.parseInt(value);
} catch(NumberFormatException NF){
System.out.println("Thats not a number. Type in an integer.");
break;
}
if (sideB<=0)
{
System.out.println("Cannot compute because B is zero. Try another integer");
break;
}
if(sideB>0)
{
System.out.println("You've inputed an B value. That value is "+sideB);
break;
}
break;
case 'C': //calculate an answer
double temporary = (sideA * sideA) + (sideB * sideB);
if(sideA <=0 && sideB <=0){
System.out.println("You don't have triangle. Try again");
break;
}
double result = java.lang.Math.sqrt(temporary);
System.out.println("The hypotenuse value is "+result);
break;
}
}
System.out.println("Thank you. Goodbye!");
return;
}
c =side.charAt(0);
…添加一个检查以确保它不是空的,并等待获得有效的长度
side = JOptionPane.showInputDialog(null, "A or B?");
你试过使用调试器吗?你看过第42行了吗?它正盯着你看。读取异常名称。转到指示的行。解决问题。信息能比现在更清晰吗???