user3316886
user3316886

Reputation: 83

how to find out if user input has x characters

trying to make another && to see if the lock_combination is exactly 9 characters long

import java.util.Scanner;

public class charlespeppers {

    public static void main(String[] args) {

        Scanner user_input = new Scanner (System.in);

        String lock_combination;
        System.out.print ("Please enter a lock combination ( ddRddLddR ) : ");
        lock_combination = user_input.next();


        if (lock_combination.charAt(0) >= '0' && lock_combination.charAt(0)<= '9' 
        &&  lock_combination.charAt(1) >= '0' && lock_combination.charAt(1)<= '9'
        &&  lock_combination.charAt(3) >= '0' && lock_combination.charAt(3)<= '9'
        &&  lock_combination.charAt(4) >= '0' && lock_combination.charAt(4)<= '9'
        &&  lock_combination.charAt(6) >= '0' && lock_combination.charAt(6)<= '9'
        &&  lock_combination.charAt(7) >= '0' && lock_combination.charAt(7)<= '9'
        &&  lock_combination.charAt(2) == 'R'
        &&  lock_combination.charAt(8) == 'R'
        &&  lock_combination.charAt(5) == 'L'
        &&  lock_combination == 9);
        {
            System.out.println("This is a valid lock combination.");
        }

}
}

Upvotes: 1

Views: 48

Answers (2)

Christian Tapia
Christian Tapia

Reputation: 34146

You can use the length() method of String:

public int length(): Returns the length of this string. The length is equal to the number of Unicode code units in the string.

if (... && lock_combination.length() == 9)
    System.out.println("This is a valid lock combination.");

Upvotes: 1

Kakarot
Kakarot

Reputation: 4252

To find length of lock_combination you can use the following piece of code :

if(lock_combination.length() == 9)

Upvotes: 0

Related Questions