Java index out of range: 0

I am desperately trying to figure out a way to stop the "String index out of range: 0" errors ... This happens when I enter nothing and continue execution:

static String getRef(Scanner Keyboard)
{
    Scanner keyboard = new Scanner(System.in);        
    String ref= "";        
    boolean valid = false;
    int errors = 0;
    boolean problem = false;

    while(valid==false)
    {
        System.out.println("Please enter a reference number which is two letters followed by three digits and a letter(B for business accounts and N for non business accounts)");
        ref = keyboard.nextLine();

        for (int i=0; i<6; i++)
        {   
            if (ref.charAt(i)=='\0')            
            {
                problem = true;
            }                
        } 

        if(problem == true)
        {
            System.out.println("The reference must consist of 6 Characters");
        }
        else
        {             
            if ((Character.isDigit(ref.charAt(0))== true) || (Character.isDigit(ref.charAt(1))== true))
            {
                System.out.println("The first 2 characters must be letters");
                errors = errors + 1;
            }

            if ((Character.isDigit(ref.charAt(2))== false) || (Character.isDigit(ref.charAt(3))== false)||(Character.isDigit(ref.charAt(4))== false))
            {
                System.out.println("The 3rd,4th and 5th characters must be numbers");
                errors = errors + 1;
            }

            if ((!ref.toUpperCase().endsWith("B"))&&(!ref.toUpperCase().endsWith("N"))) 
            {
                System.out.println("The 6th character must be either B(for business accounts) or N(for non business accounts) ");
                errors = errors + 1;
            }

            if (errors==0)
            {
                valid=true;
            }
        }

    }        
    return ref;        
}

I want to display an error message if the string does not contain a character with a specific index, for example. ref.charAt(0).

This will help (example):

if(ref.charAt(aNumber)==null) { 
    // display error message 
}
+4
source share
2 answers

Your problem is here:

ref.charAt(i)=='\0'

What happens if ref- a string with zero length? In this case, trying to access the character with index 0 (where the first character will usually be) will give you your error index out of range: 0. First check the line length:

if (ref != null && !ref.isEmpty() &&ref.charAt(i)=='\0')  { .. }
+4
source

Adding Verification length()

ref ( ""), . , 0 ( 1, 2, 3...). if length, -

if (ref.length() > 5) {
    for (int i = 0; i < 6; i++) {   
        if (ref.charAt(i) == '\0') {
            problem = true;
        }                
    } 
} else {
    System.out.println("Please enter at least 6 characters");
}

( ) Pattern (2 , 3 , b n). - ,

String ref; // <-- get input
Pattern p = Pattern.compile("([a-zA-Z]{2})(\\d{3})([B|N|b|n])");
Matcher m = p.matcher(ref);
if (m.matches()) { // <-- valid = m.matches();
    System.out.println("ref is valid");
} else {
    System.out.println("ref is not valid");         
}
+3

Source: https://habr.com/ru/post/1622565/


All Articles