Count number of characters that have been entered into the console

1

When I run the program by console, I would like it to count the number of characters I have pressed, by the time I reach 13, I would like to press ENTER to enter the number entered.

The example of the method is this:

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader (isr);

    public void escribirCodigo(){
            long codigo = 0;
                System.out.println("Introduce el código de barras.");
            try{             
                    codigo = Long.parseLong(br.readLine());             

                    revisarCodigoBaseDatos(codigo);
                    System.out.println("El valor introducido es: "+codigo);


            } catch(IOException e){
                    System.out.println("Ha ocurrido un error.");
            }
        }

The method 'checkCodeDatabase (code)' is where I check if the number entered is in the database. I want that when I check that there are 13 digits automatically in the console that take the value entered and perform the method 'checkCodeDownDates (code)'

    
asked by Arkhan6 06.03.2017 в 14:18
source

2 answers

1

You have two ways of doing it, the first one and the simple way is that you enter manually after entering the 13 digits. In addition it is a string you can use it like that. and when you need it as a number the parsees

public static void main (String args[]){
   InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader (isr);
    String codigo= null;
   System.out.println("Introduce el código de barras.");
   codigo=br.readLine();
   escribirCodigo(codigo);
}
public void escribirCodigo(String codigo){
        try{                 
                revisarCodigoBaseDatos(Long.parseLong(codigo));
                System.out.println("El valor introducido es: "+codigo);

        } catch(IOException e){
                System.out.println("Ha ocurrido un error.");
        }
    }

The other way is with a keyboard event that detects that 13 keys are pressed. It is depending on the needs you have.

Remember not to put prints on methods, it's bad practice.

    
answered by 06.03.2017 в 15:05
0

As I understand you want to have something similar to a listener but in console, that can not be done, but if you can generate a loop similar to this:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = "";

while (line.equalsIgnoreCase("quit") == false) {
   line = in.readLine();

   //do something
}

in.close();   
    
answered by 06.03.2017 в 14:29