2010-10-24 235 views
0

我是新来的java,并通过询问我确信什么是愚蠢的问题来咬牙切齿。我创建了一些方法,只是简单地打电话给他们。主要方法中的while循环出现错误。编译器说“线程主java.lang.StringIndexOutOfBoundsException中的异常:字符串索引超出范围:0在java.lang.String.charAt(String.java:686)在Project3.main(Project3.java:61)Java-字符串索引超出范围异常“字符串索引超出范围”

任何帮助将不胜感激感谢完整的代码如下:

import javax.swing.JOptionPane; 
import java.util.Scanner; 
public class Project3 
{ 
public static void main(String[] args) 
{ 
int iScore1; //first variable input by user to calc average 
int iScore2; //second variable input by user to calc average 
int iScore3; //third variable input by user to calc average 
double dAverage; //returned average from the three test scores 
char cLetterGrade; //letter grade associated with the average 
double dGPA; //the GPA associated with the letter grade 
char cIterate = 'Y'; // loop check 
String strAgain; //string user inputs after being asked to run again 

System.out.print(createWelcomeMessage()); 


//pause in program 
pressAnyKey(); 

while (cIterate == 'Y') 
{ 
//prompt user for test scores 
System.out.print("\n\nPlease enter the first test score: "); 
Scanner keys = new Scanner(System.in); 
iScore1 = keys.nextInt(); 

System.out.print("\nPlease enter the second test score: "); 
iScore2 = keys.nextInt(); 

System.out.print("\nPlease enter the third test score: "); 
iScore3 = keys.nextInt(); 

//calculate average from the three test scores 
dAverage = calcAverage(iScore1, iScore2,iScore3); 
System.out.print("\nThe average of the three scores is: " + dAverage); 

//pause in program 
pressAnyKey(); 

//get letter grade associated with the average 
cLetterGrade = getLetterGrade(dAverage); 
System.out.print("\nThe letter grade associated with the average is " + cLetterGrade); 

//pause in program 
pressAnyKey(); 

//get the GPA associated with the letter grade 
dPGA = calcGPA(cLetterGrade); 
System.out.print("\nThe GPA associated with the GPA is "+ dGPA); 

//pause in program 
pressAnyKey(); 

System.out.print("\nDo you want to run again?(Y or N):_\b"); 
strAgain = keys.nextLine; 
strAgain = strAgain.toUpperCase(); 
cIterate = strAgain.charAt(0); 
}//end while 

//display ending message to user 
System.out.print(createEndingMessage()); 

}//end main method 
}//end class Project3 

public static String createWelcomeMessage() 
{ 
String strWelcome; 
strWelcome = "Why hello there!\n"; 
return strWelcome; 
}//end createWelcomeMessage() 

public static String createEndingMessage() 
{ 
String strSalutation; 
strSalutation = "See you later!\n"; 
return strSalutation; 
}//end createEndingMessage() 

public static void pressAnyKey() 
{ 
JOptionPane.showMessageDialog(null, "Press any key to continue: "); 
}//end pressAnyKey() 

public static int getTestSCore() 
{ 
int iScore; 
System.out.print("Enter a test score: "); 
Scanner keys = new Scanner(System.in); 
iScore = keys.nextInt(); 
return iScore; 
}//end getTestSCore() 

public static int calcAverage(int iNum1, int iNum2, int iNum3) 
{ 
double dAverage; 
dAverage = ((double)iNum1 + (double)iNum2 + (double)iNum3)/(double)3.0; 
return dAverage; 
}//end calcAverage(int iNum1, int iNum2, int iNum3) 

public static char getLetterGrade(double dGrade) 
{ 
char cLetter; 

if (dGrade <60) 
{ 
    cLetter = 'F'; 
} 
else if (dGrade >=60 && dGrade <70) 
{ 
    cLetter = 'D'; 
} 
else if (dGrade >=70 && dGrade <80) 
{ 
    cLetter = 'C'; 
} 
else if (dGrade >=80 && dGrade <90) 
{ 
    cLetter = 'B'; 
} 
else if (dGrade >=90) 
{ 
    cLetter = 'A'; 
} 

return cLetter; 
}//end getLetterGrade(double dGrade) 

public static double calcGPA(char cLetterGrade) 
{ 
double dGPA; 

if (cLetterGrade == 'A') 
{ 
    dGPA = 4.0; 
} 
else if (cLetterGrade == 'B') 
{ 
    dGPA = 3.0; 
} 
else if (cLetterGrade == 'C') 
{ 
    dGPA = 2.0; 
} 
else if (cLetterGrade == 'D') 
{ 
    dGPA = 1.0; 
} 
else 
{ 
    dGPA = 0.0; 
} 
return dGPA; 
}//end calcGPA(char cLetterGrade) 
+0

原因很明显,您尝试访问超出绑定范围的字符串索引,请尝试查看第61行代码 – 2010-10-24 11:37:25

+0

该代码甚至无法编译..请粘贴工作代码。 – 2010-10-24 11:48:59

+0

我不认为*编译器*正在编写该消息。 – Raedwald 2014-12-03 14:15:11

回答

0

问题是由这一行造成的:

cIterate = strAgain.charAt(0); 

字符串显然不会在索引0有一个字符,换句话说,它是空的。 你可能想检查我们呃输入并再次询问是否没有提供。

5

您正在阅读使用scanner.nextInt()的三个整数。由于nextInt在读取令牌后不消耗任何空格或换行符,这意味着如果用户输入一个数字并按回车,流中仍然有换行符。

因此,当您稍后致电nextLine时,它会读取该换行符并返回空字符串。

由于对空字符串调用charAt会导致出界限错误,因此会出现您遇到的错误。

为了解决这个问题,或者使用next代替nextLine,其将读出下一个字(消耗之前它的任何空白),而不是下一行,或拨打nextLine两次。一次消耗换行符,一次读取实际行。

您应该仍然检查用户是否输入空行。

-1

如果您将第67行左右移动,则该行结束该行。将其移至最后,并将其降至三个错误。这三个错误之一是拼写错误,其中一个关于keys.nextLine needs() - > keys.nextLine(),最后一个错误是方法头返回一个int,而不是double。这样做的确会产生另一个错误,但是如果你用单引号将cLetter设置为空格,那么代码就会被编译。

相关问题