Nuno Teixeira
Nuno Teixeira

Reputation: 41

How to contain a string to certain values java

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

Answers (5)

Zymus
Zymus

Reputation: 1698

You could use regex,

String pattern = "^[01][0-4][0-3]$";
return input.matches(pattern);

Upvotes: 2

ajb
ajb

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

Vincent Ramdhanie
Vincent Ramdhanie

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

Kevin Winata
Kevin Winata

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

ShellFish
ShellFish

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

Related Questions