例外,需要了解有什么问题?

时间:2014-06-24 22:46:13

标签: java exception

{

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;

    }

}

我的错误是:

  

线程“main”中的异常java.lang.StringIndexOutOfBoundsException:   字符串索引超出范围:0 at   java.lang.String.charAt(String.java:658)at   lab1.lab01.main(lab01.java:42)

到底出了什么问题?

1 个答案:

答案 0 :(得分:0)

虽然String并非完全与{C}中的char[]一样,但对于某些操作,最好将其想象为之一。

当您尝试索引为空或未初始化的数组时,您将获得类似的异常 - ArrayIndexOutOfBoundsException。这意味着您正在尝试索引不存在的内容。

我们知道大小为N的数组可以索引到位置N-1。对于N = 0,我们(通过数学)将索引到位置-1,which is not permissible.

执行charAt()时会发生同样的事情。您试图检索不存在的值。换句话说:你的String是空的。

罪魁祸首就是这条线:

c =side.charAt(0);

如果side为空,则表示您已被卡住。

当您去检索side的值时,如此行:

side = JOptionPane.showInputDialog(null, "A or B?");

...添加支票以确保它不会为空,并等到有效长度。

String side = JOptionPane.showInputDialog(null, "A or B?");
while(side.isEmpty()) {
   JOptionPane.showMessageDialog("Invalid input!");
   side = JOptionPane.showInputDialog(null, "A or B?");
}