2010-10-24 127 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設置爲空格,那麼代碼就會被編譯。

相關問題