Reputation: 75
I've fixed my code so that it recognizes if their's 4 digits and less or 6 digits and higher but now I want to know whether or not it contains letters within the numbers.
The code below detects the letters and prints the line I want only when I input 5 letters, and I want it to detect even if their's more digits than letters or more letters than digits.
String digit;
String regex;
String regex1;
regex = "[0-9]{5}";
regex1 = "^[a-zA-Z0-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 );
}
else if (test.length() > 5 || test.length() < 5)
{
System.out.println("You have letters in there.");
}
else if (digit.matches(regex1))
{
test = digit.replaceAll("[a-zA-Z]", "");
System.out.println("You have letters in there.");
}
else
if (digit.length() < 5)
{
System.out.println("You don't have enough digits.");
}
else if (digit.length() > 5)
{
System.out.println("You have to many digits.");
}
} while (!digit.matches(regex));
Upvotes: 0
Views: 3478
Reputation: 13930
I won't go into regex here because honestly I think there is some misunderstandings that should be dealt with far before the road of niceties is undergone; plus, I'm no expert and I personally think they are more like rights of passage.
Anyway, let's start from the beginning, or at least when you determine they've entered a valid digit
,
if (digit.matches(regex))
.
Let's say...
String digits = "12345";
System.out.println(getSum(digits) / 2);
where...
public int getSum(String digits) {
int sum = 0;
for(int i = sum; i < digits.length(); i++) {
sum += digits.charAt(i);
}
return sum;
}
Same as your System.out.println((one + two + three + four + five) / 2 );
.
I hope the output of 127 makes you smile.
Going out on a limb, and since you didn't speak of the "char" value sum that, you expected it to treat your char
s as decimal digits. Well, that would result in 7
. I only guess because of the whole (char)charAt()
thing. This -> (char)charAt()
sort of shows a lack of understanding that would make the use of regex highly questionable, IMHO of course.
Beyond that else if (test.length() > 5 && test.length() < 5)
. This says, "if test's length is greater AND less than 5"! Without using some mathematical paradox, tell me that number.
So, on to your question -
but now i want to know whether or not it contains letters within the numbers.
well let's look at how finding out if any non-digit exists might be done - without regex so we can understand it...
public boolean containsNonDigits(String digits) {
for(int i = 0; i < digits.length(); ++i) {
if(Character.isDigit(digits.charAt(i))) {
continue;
} else {
return true;
}
}
return false;
}
This says, "if the character is a digit keep going; everything's fine, otherwise false".
The other "question" -
i want it to detect even if their's more digits than letters or more letters than digits.
is an "additive" to the method above so I'll leave that one to you.
Upvotes: 2
Reputation: 210
You can use Charachter.isDigit(char)
and Charachter.isLetter(char)
methods.
Here code sample that implements what you asked:
public static void main(String[] args)
{
System.out.println("In this game, you will have to input 5 digits.");
int validLength = 5;
boolean valid = false;
Scanner console = new Scanner(System.in);
while (!valid)
{
System.out.println("Please input 5-digits.");
String digit = console.next();
if (digit.length() != validLength)
{
//if length not valid, mark as not valid and return to next iteration
valid = false;
String message = digit.length() < validLength ? "You don't have enoght digits." : "You have to many digits.";
System.out.println(message);
continue;
}
//here digit.length = 5
int nDigits = 0,nLetters = 0,sum = 0;
for (int i = 0; i < digit.length(); i++)
{
Character ch = digit.charAt(i);
if (Character.isDigit(ch))
{
nDigits++;
sum += Integer.parseInt(ch.toString());
}
else if (Character.isLetter(ch)) {
nLetters++;
}
}
if (nLetters == 0 /* no letters */
|| /* and */
nDigits == validLength /* all chars are digits */)
{
System.out.println(sum/2);
valid = true;
}
else{
System.out.println("You have letters in there.");
}
}
}
Upvotes: 1