How to compare string with String format in JAVA?

-3

To explain my question better, I need to validate the format of a string of characters, for example:

the user should enter in the text field: 6412365-4 valid that the number is correct by applying module 11.

but I want that if the user enters: 24563254 send me a message because the format should be 0000000-0

How to compare the value entered with the format I need?

    
asked by Josué Martínez 27.12.2017 в 15:20
source

4 answers

1

These types of problems are better addressed using regular expressions:

import java.util.regex.*;


public class HelloWorld
{
  public static void main(String[] args)
  {
    Pattern patron = Pattern.compile("[0-9]{7}-[0-9]{1}");
    Matcher mat = patron.matcher("6412365-4");
    if(mat.matches()){
        System.out.println("FORMATO CORRECTO");
    }else{
        System.out.println("FORMATO INCORRECTO");
    }
  }
}

I recommend you investigate and use them

    
answered by 27.12.2017 в 16:16
0

Hi Joshua your question I do not understand it well, what I understand is that you want the user to enter a value with a format with an established number of numbers and at the end of the last number have "-1". I mean you want the chain 1234567-8 is valid, however the number 12345678 is not valid? The solution would be to apply a JFormatteTextField. Here is a guide how to use it, I hope it helps you. link

    
answered by 27.12.2017 в 16:17
0

Here I leave a complete example, which validates whether or not it contains the format you have above. includes the script and also the size of it.

public class Format {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    String dato;
    int posicion = 0,size=0;
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));


    try {
        System.out.println("ingrese el dato");
        dato = bf.readLine();
        posicion = dato.indexOf("-");
        size = dato.length();
        System.out.println("rrr"+posicion+" "+size);
        if(size == 9){
            if(posicion == 7){
                System.out.println("formato correcto: "+dato);
            }else{
                System.out.println("no tiene el formato correcto. 0000000-0");
            }
        }else{
            System.out.println("faltan carácteres en el string");
        }

    } catch (IOException ex) {
        Logger.getLogger(Formato.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

    
answered by 27.12.2017 в 15:35
0

To validate data with a certain pattern you can use the java regex

Here is an example:

        String pattern  = "[0-9]{7}-[0-9]{1}";

        String verdadero = "6412365-4";
        String falso = "923412-3";
        String false2 = "1231234-99";
        String falso3 = "64123653";

        ArrayList<String> lista = new ArrayList<>();
        lista.add(verdadero);
        lista.add(falso);
        lista.add(false2);
        lista.add(falso3);
        for (String reg : lista){
            if (reg.matches(pattern){
             // Haz lo que quieras aquí
           }

        }
  

I'm going through a for loop just for the example

    
answered by 27.12.2017 в 16:10