fName.substring超出范围

时间:2014-09-21 07:54:56

标签: java eclipse

public class Registration {

    public static void main(String[] args) {

            final String MY_DELIMITER = "','";
            boolean tryAgain = true;
            String fName = "";
            String A = fName.substring(0,2);
            String lName = "";
            int lNameLength = lName.length();
            String B = lName.substring(lNameLength-4,lNameLength);
            String address = "";
            String zip = "";
            String C = zip.substring(0,5);
            String age = "";
            String D = age.substring(0,1);
            String gender = "";
            String race = "";
            String regList = "";
            Scanner myScanner = new Scanner(System.in);
            boolean showList = false;

        // Get input from the user until they type "q"     
        // For each input check for "q"
        // if not q, append the input 
        // to the existing String + the delimiter      
        while(tryAgain)
        {
              System.out.println("Name:  (q to quit)");
              fName = myScanner.nextLine();
              System.out.println("Last Name:  (q to quit)");
              lName = myScanner.nextLine();
              System.out.println("Addess:  ");
              address = myScanner.nextLine();   
              System.out.println("Age:  ");
              age = myScanner.nextLine();
              System.out.println("Gender:  ");
              gender = myScanner.nextLine();
              System.out.println("Race:  ");
              race = myScanner.nextLine();

            if(fName.equals("q"))
            {
                    tryAgain = false;
            }
            else 
            {
                // Append new name to the list using a delimiter
                regList = fName + lName + "\n" + address + "\n" + age + "\n" + gender + "\n" + race + MY_DELIMITER;
            }         
        } // end of while( )

        System.out.println("Here is your registration:" + regList);
        // Convert the String into an array, using the same delimiter 
        String[ ] regArray = regList.split(MY_DELIMITER);

        // Ask the user if they want to display the contents of the array
        // If "y" then display the list using a foreach loop
        System.out.println("Would you like to see the registration from the Array? [y-n]");
        fName = myScanner.nextLine( );

        myScanner.close();

        fName = fName.toLowerCase( );

        showList = fName.equals("y")?true:false;

        if(showList)
        {
            // Display the results using for each
            System.out.println("Here is your registration from the array: ");
            // Use a for each statement instead of the more complex for( ) loop
            // for(int counter=0; counter < employeeArray.length; counter++)
            for(String thisReg:regArray)
            {
                    System.out.println(thisReg); 
                    System.out.printf("USER ID: ", A + "-" + B + "-" + C + "-" + D);
            }
        } // end of if(showList)
    }
}

我正在尝试提取fName输入的前3个字母,所以我想我可以使用fName.substring来做到这一点,但它给了我这个错误。

抱歉,我没有添加所有代码,以节省时间。显然它看起来很混乱。任何方式使fName输入是用户的名称。难道不是那个顺序吗?

3 个答案:

答案 0 :(得分:1)

呃......你的行动序列是可疑的。实际上到处都是。

请看以下互动:

String fName = "";
String A = fName.substring(0,2);

您声明一个空字符串,然后立即获取它的子字符串。你从哪里获得子串的数据?这里没有任何子串 - 空字符串的长度为零。

您应该 某些 ,在将数据放入字符串之前将其放入字符串中。使用Scanner会有很长的路要走。

或者更好的是,将myScanner的实例移到main的顶部会更清楚地说明应该去哪里,以及它应该如何工作。

答案 1 :(得分:0)

在子串之前总是检查字符串的长度。特别是当用户给你这个变量时。

答案 2 :(得分:0)

您正在尝试获取空字符串的子字符串。

String fName = "";
String A = fName.substring(0,2); // here fName is empty!!!

将fName更改为某个实际的String,并在调用substring之前检查String的长度,以确保所需大小的子字符串存在。

String fName = "somestring";
if(fName.length() >= 2) {
    String A = fName.substring(0,2);
    System.out.println(A); // prints out "so"
}

所有其他String也属于这种情况。