1

I'm Trying to except employeenumber XXX-L where x a digit in range 0-9 and L a letter in range of A-M. I'm trying to get this code work. but I can't enter A valid input.

import java.util.Scanner;
import java.util.InputMismatchException;

public class ObjectOrinetPrograming
{
    public static void main( String [] args )
    {
        Scanner input = new Scanner (System.in);
        System.out.println("Please Enter elements: ");
        String employeenumber = input.nextLine();
        while (employeenumber.length() != 5)
        {
            System.out.println("invalid input; lenght, Try again:");
            employeenumber = input.nextLine();
        }


            while (employeenumber.charAt(4) != ('A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'))
            {
                System.out.print("invalid input; charrecter match, try again:");
                employeenumber = input.nextLine();
            }


        while (employeenumber.charAt(0) == '-')
        {
            System.out.println("Invalid Input; form, try again:");
            employeenumber = input.nextLine();
        }
        }

}

2 Answers 2

1

You can use regular expression to match input employeenumber.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please Enter elements: ");
    String employeenumber = input.nextLine();
    while (!employeenumber.matches("[0-9]{3}-[A-M]")) {
        System.out.println("invalid input; lenght, Try again:");
        employeenumber = input.nextLine();
    }

    System.out.println("Your employee id is " + employeenumber);

}
Sign up to request clarification or add additional context in comments.

6 Comments

A small correction. the regex should be [0-9]{3}-[A-M]
@iNan thank you so much but i tried it and whenever I input for example : 123-A which is the correct input i still get invalid input. I think your code except one character as an input what i'm trying to do is excepting 5 characters. But thanks anyways.
@m4heshd after applying the editing it worked. Thanks a lot guys.
Can some one explain why {3} matter and what does it means or do?
@m4heshd thanks a lot so i'm trying to play with it little bit i noticed that if i add another [a-m] beside the [A-M] wont work for small letters i'm trying to figure it out. thanks alot
|
0

You should use matches, which will allow you validate all your inputs:

import java.util.Scanner;
import java.util.InputMismatchException;
public class ObjectOrinetPrograming
{
    public static void main( String [] args )
    {
        Scanner input = new Scanner (System.in);
        System.out.println("Please Enter elements: ");
        String employeenumber = input.nextLine();

        while (!employeenumber.matches("[0-9]{3}-[A-Ma-m]")) {
            System.out.println("invalid input; lenght, Try again:");
            employeenumber = input.nextLine();
        }

    }

}

1 Comment

@Muzy then the regex would simply be [0-9]{3}-[A-Ma-m] to match your requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.