Reputation: 41
I need to limit a 3 digit string to accept only 0 and 1 for the first number, 0 to 4 for the second number and 0 to 3 for the third number. Actually I have, after many modifications and new tries the code:
public boolean isLocalizacaoValida(String localizacao) {
String numero1 = localizacao.substring(0);
int intNumero1 = Integer.parseInt(numero1);
String numero2 = localizacao.substring(1);
int intNumero2 = Integer.parseInt(numero2);
String numero3 = localizacao.substring(2);
int intNumero3 = Integer.parseInt(numero3);
if (localizacao.length() != 3) {
System.out.println("Localização inválida!");
return false;
}
if (localizacao.length() == 3) {
if ((intNumero1 < 0 && intNumero1 > 1) || (intNumero2 < 0 && intNumero2 > 4) || (intNumero3 < 0 && intNumero3 > 3)) {
System.out.println("Localização inválida!");
return false;
} else {
System.out.println("Localização válida!");
return true;
}
}
System.out.println("Localização inválida!");
return false;
}
With this, it keeps returning true even if the string should be returned as false.
Upvotes: 0
Views: 132
Reputation: 1698
You could use regex,
String pattern = "^[01][0-4][0-3]$";
return input.matches(pattern);
Upvotes: 2
Reputation: 31699
(intNumero1 < 0 && intNumero1 > 1)
cannot be right. It will be true only if a negative number is greater than 1. And I don't know of any.
You want to use ||
. That returns true if intNumero1
is less than 0 OR if it is greater than 1. In other words, if it's anything except 0 or 1.
Upvotes: 0
Reputation: 103145
The substring method that takes a single int will usually return everything from that index to the end of the string. You should use the two parameter version like this:
String numero1 = localizacao.substring(0, 1); //return only the first character
String numero2 = localizacao.substring(1, 2); //return only the second character
and so on.
Secondly, the condition of the if statement to be joined with AND operations. That is, you want to check that the first digit is in a certain range AND the second is in a certain range AND the third is in the correct range. With OR you will get true as long as any one is in the correct range.
if ((intNumero1 >= 0 && intNumero1 <= 1) && (intNumero2 >= 0 && intNumero2 <= 4) && (intNumero3 >= 0 && intNumero3 <= 3)) {
System.out.println("Localização válida!");
return true;
} else {
System.out.println("Localização inválida!");
return false;
}
See, your condition as written could never be true:
intNumero1 < 0 && intNumero1 > 1
requires the value to be less than 0 AND greater than 1? That will always be false.
Upvotes: 0
Reputation: 379
How about
int num = Integer.parseInt(localizacao);
return (num/100 <= 1) && (num%100/10 <= 4) && (num%10 <= 3);
You can use TryParse
instead of parseInt
if you want to handle the exception.
Upvotes: -1
Reputation: 4551
Substring returns more than one char, try this:
public boolean isLocalizacaoValida(String localizacao) {
if (localizacao.length() != 3) {
System.out.println("Localização inválida!");
return false;
}
if (localizacao.length() == 3) {
String numero1 = localizacao.substring(0,1);
int intNumero1 = Integer.parseInt(numero1);
String numero2 = localizacao.substring(1,2);
int intNumero2 = Integer.parseInt(numero2);
String numero3 = localizacao.substring(2);
int intNumero3 = Integer.parseInt(numero3);
if ((intNumero1 < 0 && intNumero1 > 1) || (intNumero2 < 0 && intNumero2 > 4) || (intNumero3 < 0 && intNumero3 > 3)) {
System.out.println("Localização inválida!");
return false;
} else {
System.out.println("Localização válida!");
return true;
}
}
System.out.println("Localização inválida!");
return false;
}
Upvotes: 0