-1

I try to accept the user input as a string first, calculate the length of the string then convert it to integer as i want the user input always between 000-999?But except 000 itself, the others work well with the coding. How can i achieve this.Any help will be appreciated. Thank you!

System.out.println("Please enter first number: ");
            String UserEnter = in.nextLine();
            if(UserEnter.length()==3){
                int UserInt1 = Integer.valueOf(UserEnter);
                if(UserInt1 >= 000 && UserInt1 <=999){
                    for(i = 0;i<=number;i++){ 
                        if(temp==array[i]||temp==array2[i]){ //compared temp with previous combination
                            display("Repeat");     
                            break;         
                        } else {
                            FirstNum = temp;
                        }
                    }
                } else {
                    System.out.println("The number must be between 000 and 999!");
                }
            } else {
                System.out.println("Invalid Input!Try again!");
            }
4
  • You don’t have 0 in array/array2 by default, right? Jul 15, 2019 at 13:03
  • It is not clear, what values temp, number, array and array2 have Jul 15, 2019 at 13:08
  • I think we're missing too much code to get a handle on what you're trying to do. Why does the user have to input "000" for 0? What is the number variable? Can you add some more context to this so we can better help you?
    – David
    Jul 15, 2019 at 13:10
  • I'd expect your code to work for "000" as well as it works for other strings in the "001"-"999" range. What error or undesired behaviour do you encounter when using "000" ? Here's a test showing how "000" passes both your conditions : ideone.com/EccKLa
    – Aaron
    Jul 15, 2019 at 13:39

4 Answers 4

0

000 is 0. Since you checked for a length of 3 before, it's enough to check against 1, 2, 3, ...

if (UserInt1 >= 0)

For the rest, it's really hard to tell what you are trying to do since your code is incomplete, however

System.out.println(001 == 1); // true
0

if the length is 3, the number can not be greather than 999 so you only need to check on > 1

String UserEnter = in.nextLine();
if (UserEnter.length() == 3){
    int UserInt1 = Integer.valueOf(UserEnter);
    if (UserInt1 > 1 ) {
       // we're good
    } else {
       System.out.println("The number must be between 1 and 999!");
    }
} else {
    System.out.println("Invalid Input!Try again!");
}
0
Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter first number: ");
    String UserEnter =scanner.nextLine();
    if(UserEnter.length()==3){
        int UserInt1 = Integer.valueOf(UserEnter);
        if(UserInt1 >= 000 && UserInt1 <=999){
            if(UserInt1==000){
                System.out.println(String.format("your number is between 000 and 999 
that is: %03d",0,0,UserInt1));
            }
            else
            System.out.println("your number is between 000 and 999 that is: 
"+UserInt1);
        }
    } else {
        System.out.println("Invalid Input!Try again! (NUMBER MUST BE BETWEEN 000 AND 
999)");
    }

This answer can be helpful for you. As you wanted to show 000 as digit,may be this can be done via String.format method in java.lang.String package because you want your own String format to show desired result to the user.

1
  • 1
    if you want to format as 000, write "%03d" and not "%d%d%d" !
    – thibsc
    Jul 15, 2019 at 13:29
0

Here is an example of what you can do using regexp:

private boolean validateUserInput(String value){
    Pattern pattern = Pattern.compile("(?!000)\\d{3}");

    Matcher matcher = pattern.matcher(value);

    return matcher.matches();
}

And a test to validate this function

@Test
public void test_regexp() {
    String noTAccepted = "000";
    assertThat(validateUserInput(noTAccepted)).isFalse();

    String accepted = "090";
    assertThat(validateUserInput(accepted)).isTrue();
}

I assumed that they always needed to input 3 digit like 090.

--- Edit

So as said in the comment, if you want to validate "000", with my solution just change the pattern to "\\d{3}" and everyhting passes !

1
  • If my understanding is correct OP wants to accept 000 as an input (see title for the clearest example) but says his code doesn't (which clearly isn't reproducible)
    – Aaron
    Jul 15, 2019 at 13:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.