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

您的字符串长度为 0。请确保string.length() > 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 回答