0

I've fixed my previous issues with the code, now i want it to recognize if it's 4 digits and less or 6 digits and above with an "Else if".

And when i input letters to deny it with a System.out.println within an "Else if" as well.

  String digit;
  String regex;
  String regex1;
  regex = "[0-9]{5}";
  String test;
  String validLength = "5";
  char one, two, three, four, five; {
   System.out.println("In this game, you will have to input 5 digits.");
   do {
    System.out.println("Please input 5-digits.");
    digit = console.next();
    test = digit.replaceAll("[a-zA-Z]", "");
    if (digit.matches(regex)) {
     one = (char) digit.charAt(0);
     two = (char) digit.charAt(1);
     three = (char) digit.charAt(2);
     four = (char) digit.charAt(3);
     five = (char) digit.charAt(4);
     System.out.println((one + two + three + four + five) / 2);
    }
xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
thifofdeath
  • 75
  • 2
  • 11
  • Consider comparing the value to `9999` and `100000`. – abiessu Nov 03 '13 at 01:08
  • I want it to be able to know if its 4 digits or 5 digits, etc. But also being able to accept 00001, as it is 5 digits, therefore i'm attempting to use the regex which i'm not that familiar to. – thifofdeath Nov 03 '13 at 01:09
  • You have semicolons after some of your `else if` statements. Remove those to prevent the compile-time errors about else without ifs. Your code looks fine except that `length` is a method of `String`, not a field. So it needs to be `digit.length()`. – Ghostkeeper Nov 03 '13 at 01:11
  • I removed the semicolons but the problem persists. I'll look into the length method and comeback with another script. – thifofdeath Nov 03 '13 at 01:12
  • There's still one semicolon left after the first if: `if (digit.matches(regex));` – Ghostkeeper Nov 03 '13 at 01:15
  • 1
    regex should be: `regex = "[0-9]{5}";`, **not** `regex = "[0-9](5)";` – Steve P. Nov 03 '13 at 01:15
  • I've fixed the problems above but i want to know if it's possible to make my digit.length() an less or greater value to my validLength, so that i can know if the digits are 4 digits or 6. – thifofdeath Nov 03 '13 at 01:18

1 Answers1

1

This regex should match your need (with leading zeros):

[0-9]{5}

and you will use a while loop, looping until these two conditions are met, something like

while (!inputString.matches("[0-9]{5}")) {
    // ask again and again
    if (!isInteger(inputString)) {
        // invalid input
    } else {
        if (inputString.length() < 5) {
            // too low
        } else if (inputString.length() > 5) {
            // too high
        }
    }     
}

And you can use a helper method like this:

public boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    }
    return true;
}
Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49
  • I fixed most of my coding thanks to you. ;) But i still want to know if their's a way to recognize the letters in the 5 digits. I have tried `else if (digit.matches(regex1) && digit.matches(regex));` in which `regex1 = [a-zA-Z]{5}` and `regex = [0-9]{5}` – thifofdeath Nov 03 '13 at 01:44
  • This regex will match if your string contains any alphabetic characters: `.*[a-zA-Z]+.*`. – Utku Özdemir Nov 03 '13 at 01:50
  • What was wrong with the code above? I also want it to be a mixture of numbers of letters and still be able to recognize. – thifofdeath Nov 03 '13 at 01:53
  • One of the wrong things was `test = Integer.parseInt(digit);` line, parseInt method throws an Exception if it cannot parse that string, so rest of the method do not get executed if you do not handle that exception. And you are not using this `test` variable afterwards. – Utku Özdemir Nov 03 '13 at 01:56
  • If you want to recognize the number even if it contains letters, you can remove non-numeric characters from the string, like this: http://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters Google is your friend here. :) – Utku Özdemir Nov 03 '13 at 01:58
  • Ah.. Indeed it is! You should try this next time: lmgtfy / Thank you for the help – thifofdeath Nov 03 '13 at 02:02