Problem with String in Java using Scanner

1

I'm having a problem with this code. I want to make the user answer "yes", then ask for a number. The problem is that by asking again "More data?" it does not let you enter the string again and does not repeat the while again.

My code:

import java.util.*;

public class MaximoLibro {

    public static void main(String[]args){

        int max = Integer.MIN_VALUE; // es el Low Value
        int dato;
        boolean hubo = false;
        String resp;
        Scanner input = new Scanner(System.in);

        System.out.println("Quiere ingresar algo?");
        resp = input.nextLine();
        while (resp.equals("si")){
            hubo = true;
            System.out.println("Ingrese dato");
            dato = input.nextInt();
            if (dato > max){
                max = dato;
            }
            System.out.println("Mas datos?");
            resp = input.nextLine();
        }
        if (hubo){
            System.out.println("Maximo vale "+max);
        }
        else {
            System.out.println("No hubo datos");
        }
        input.close();
    }
}
    
asked by Mateo Kruk 14.04.2018 в 14:11
source

1 answer

2

The problem is with .nextInt() . When you enter a number and press "Enter", .nextInt() only consumes the number and not the end of the line. The end of every line is \n . Therefore, when you execute .nextLine() you consume the "end of the line" and you will be ready to read the next String you need.

You can solve this problem in one of two ways:

  • Add a .nextLine() extra, between .nextInt() and .nextLine() . The .nextLine() extra will consume the "end of the line" always.
  • Example:

    int number = scanner.nextInt();
    scanner.nextLine(); // Consume "\n"
    String string1 = scanner.nextLine();
    
  • Another way is to use .nextLine() instead of .nextInt() to read the number. Remember that .nextLine() always consumes "the end of the line". Since .nextLine() returns a String , you have to convert that String to Integer . How ?, using Integer.parseInt(...) .
  • Example:

    int number = Integer.parseInt(scanner.nextLine());
    

    If you use the second form, you can receive a Exception of the Integer.parseInt(...) method if the String that you pass to it as an argument can not be converted to Integer . For example, if you pass "Hello World" and try to convert that String to Integer, obviously it will not be able to. To handle those exceptions, you must use a try-catch block. For this reason, make sure that if you use this form, the value entered is always a number. Another advantage of this form is that you do not have to worry about doing .nextLine() every time you want to read a INT .

    Your code fixed using the first solution:

    public static void main(String[] args) {
            int max = Integer.MIN_VALUE; // es el Low Value
            int dato;
            boolean hubo = false;
            String resp;
            Scanner input = new Scanner(System.in);
    
            System.out.println("Quiere ingresar algo?");
            resp = input.nextLine();
    
            while (resp.equals("si")) {
                hubo = true;
                System.out.println("Ingrese dato");
                dato = input.nextInt();
    
                input.nextLine(); // Consume "\n"
    
                if (dato > max) {
                    max = dato;
                }
                System.out.println("Mas datos?");
                resp = input.nextLine();
            }
    
            if (hubo) {
                System.out.println("Maximo vale " + max);
            } else {
                System.out.println("No hubo datos");
            }
            input.close();
        }
    
        
    answered by 14.04.2018 / 15:25
    source