Limiting user input to only 'R', 'L', and 0-9

147 views Asked by At

I am having trouble getting the rest of my code to have the following limits

  1. It consists of only the following characters: ’R’, ’L’, and ’0’ through ’9’
  2. The ’R’ character must appear exactly twice
  3. The ’L’ character must appear exactly once
  4. The ’L’ character must appear between the two ‘R’ characters
  5. Each ‘R’ and ’L’ character must be followed by at least one ’0’ through ’9’ character
  6. No more than two ’0’ through ’9’ characters may appear consecutively

    import java.util.Scanner;
    
    public class HW04
    {
    
    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);
    // Started by naming all the variables 
    String combination;
    // 
    char R, L;
    int length;
    boolean big_R, big_L;    
    System.out.print("Please enter a valid turn dial lock combination : ");
    combination = stdIn.nextLine();
    System.out.println("");
    System.out.println("");
    length = combination.length(); 
    
    if (length <= 9 && length >= 6 )
    {
        R = combination.charAt(0);
        if (R == 'R' )
            big_R = true;
        else
            System.out.println(combination + " is not a valid turn dial lock 
       combination");
        if 
      }
     else 
     {
        System.out.println(combination + " is not a valid turn dial lock 
     combination");
     }
     stdIn.close();
     }
    
     }
    
1

There are 1 answers

0
ShoeLace On

given your list of requirements this shoudl be albo to be validated by a regular expression.

maybe something like R\d\d?L\d\d?R\d\d?

which reads as: an R followed by 1 digit and an optional second digit an L followed by 1 digit and an optional second digit and finally a second R followed by 1 digit and an optional second digit

you can find elsewhere examples of how to apply that to your code.