0

次のようなエラーメッセージが表示されます。スレッド「main」の例外

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at emp.MainClass.main(MainClass.java:52)

次のコードを使用して、この問題をどのように軽減しますか?

public class MainClass {
        //main class
public static void main(String[] args){
       // variable

    String input;


    boolean salaryError = true;
    boolean dependentError = true;
    boolean nameError = true;
    boolean charError = true;

    Employee emp1 = new Employee();
    displayDivider("EMPLOYEE INFORMATION");


    do{
    input = getInput(" First Name");
    nameError = nameValidate(input);
    if(!nameError){
        JOptionPane.showMessageDialog(null,"Incorrect Input. Please Try Again!");
    }
    }while(!nameError);
    emp1.setfirstName(input);
    do{
    input = getInput(" Last Name");
    nameError =nameValidate(input);
    if(!nameError){
        JOptionPane.showMessageDialog(null,"Incorrect Input. Please Try Again!");
    }
    }while(!nameError);
    emp1.setlastName(input);
    do{

    input = getInput(" Gender: M or F");

    charError = characterChecker(input.charAt(0));
    if(!charError){
        JOptionPane.showMessageDialog(null,"Incorrect Input. Please Try Again!");
    }
    }while(!charError);

    char g = input.charAt(0);
    emp1.setgender(g);// validates use of M or F for gender

    do{
    input = getInput(" number of dependents");
    dependentError = integerChecker(input);
    if(!dependentError){
        JOptionPane.showMessageDialog(null,"Incorrect Input. Please Try Again!");
    }
    }while(!dependentError);
    emp1.setdependents(Integer.parseInt(input));

    do{ 
    input = getInput(" annual salary");
    salaryError = doubleChecker(input);
    if(!salaryError){
        JOptionPane.showMessageDialog(null,"Incorrect Input. Please Try Again!");
    }
    } while(!salaryError);
    emp1.setannualSalary(Double.parseDouble(input));

    emp1.displayEmployee();//displays data for emp1

    Employee emp2 = new Employee("Speed","Racer",'M',1,500000.00);
    displayDivider("EMPLOYEE INFORMATION");
    emp2.displayEmployee();// displays data for emp2

    terminateApplication(); //terminates application

    System.exit(0);//exits program

}//end of main

    // gets Input information
 public static String getInput(String data)
  {
      String input = "";
      input = javax.swing.JOptionPane.showInputDialog(null,"Enter your " + data);
      return input;
  }// end getInput information

    // The display divider between employees
 public static void displayDivider(String outputLab)
  {
      System.out.println("********" + outputLab + "********"); 
}// end display divider

    // Terminates the application
 public static void terminateApplication()
  {   javax.swing.JOptionPane.showMessageDialog(null,"Thanks for the input!"); 

  }// end terminateApplication 
 public static boolean doubleChecker(String inStr){
     boolean outBool = true;
     double tmpDbl = 0.0;
     try{
         tmpDbl = Double.parseDouble(inStr);
         if(tmpDbl <= 0)
             throw new IllegalArgumentException();
     }
     catch (Exception e){

         outBool = false;
     }
     return outBool;
 }

 public static boolean integerChecker(String intStr){
     boolean outBool = true;
     int tmpInt = 0;
     try{
         tmpInt = Integer.parseInt(intStr);
         if(tmpInt <= 0)
             throw new IllegalArgumentException();
     }
     catch (Exception e){

         outBool = false;
     }
     return outBool;
 }

 public static boolean nameValidate(String str){
        for(char ch : str.toCharArray()){
            if(!Character.isDigit(ch)){
                return true;
            }
        }
        return false;
    }
 public static boolean characterChecker(char gen){
     boolean outBool = true;

     try{

         if(!( gen ==( 'M') || gen ==('F')))
             throw new IllegalArgumentException();
     }
     catch (Exception e){

         outBool = false;
     }
     return outBool;
 }
}//end of Main Class
4

3 に答える 3

4

string.length() > 0文字列の長さは 0です。その要素にアクセスする前に確認してください。問題は、例外が問題が発生していると言う行にあります。

于 2013-03-24T03:37:34.123 に答える
0

Better answer: are you using an IDE? If so, observe the line the exception tells you you have an error on. Set a breakpoint before that line, debug, and note the contents of the object on which the error happened (in this case the string). Then check the javadoc for the method that threw the exception to see if there is any problem calling that method on that string.

If you are not using an IDE, you will either need to use one or find a standalone debugger. Having a good debugger is a requirement of Java development.

This should save you a lot of SO questions going forward.

于 2013-03-24T03:39:49.040 に答える
0

StringIndexOutofBoundsExceptionは、インデックスを使用して文字列にアクセスしようとしており、インデックスが負であるか、文字列のサイズよりも大きいことを意味します。

あなたはこの部分で間違っています:

charError = characterChecker(input.charAt(0));

入力の長さが0かどうかをチェックしていないためです。

その行を次のように変更してみてください。

charError = input != null && input.length() > 0 && characterChecker(input.charAt(0));       
于 2013-03-24T03:50:35.963 に答える