Ver Mensaje Individual
  #2 (permalink)  
Antiguo 03/09/2014, 13:18
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 9 meses
Puntos: 182
Respuesta: Aprendiendo JAVA desde cero - Error con bucle while

Buenas,

El problema es que el nextInt() lee el numero y se deja el salto de linea (\n) y cuando haces el nextLine() siguiente interpreta este salto de linea como el input.

Yo en tu lugar utilizaria siempre el nextLine() para la entrada de datos. En tu caso bastaria con hacer:

dato = Integer.parseInt(input.nextLine());


Código Java:
Ver original
  1. package calcu;
  2. import java.util.*;
  3.  
  4. public class Calcu {
  5.  
  6.  
  7.     public static void main(String[] args) throws NumberFormatException{
  8.        
  9.         int max = Integer.MIN_VALUE;
  10.         int dato;
  11.         boolean hubo =false;
  12.         String resp;
  13.         Scanner input = new Scanner(System.in);
  14.         System.out.println("Quiere ingresar algo?");
  15.         resp = input.nextLine();
  16.         while (resp.equals("S")){
  17.             hubo = true;
  18.             System.out.println("Ingrese un dato");
  19.             dato = Integer.parseInt(input.nextLine());
  20.             if (dato > max) {
  21.                 max = dato;
  22.             }
  23.             System.out.println("mas datos?");
  24.             resp = input.nextLine();
  25.            
  26.         }
  27.  
  28.         if (hubo) {
  29.             System.out.println("Maximo vale" + max);
  30.            
  31.         } else {
  32.             System.out.println("No hubo datos");
  33.         }
  34.        
  35.         input.close();
  36.     }
  37.    
  38. }

Un saludo