1

I have a little question. I need to read two words in a line for put name and lastname.

public void Promedios5(){
    Scanner Marbis=new Scanner(System.in);
    String[] x=new String[5];
    double[][] a=new double[5][4];
    double[] b=new double [5],c=new double[5];
    System.out.println("Este programa genera los promedios de las notas de cuatro unidades\n"
            + "se le solicitarán a usted, el nombre y las cuatro notas");
    System.out.println("Podría ingresarlas ahora por favor:");
    for(int y=0;y<=4;y++){
        System.out.println("Ingrese el nombre:");
        x[y]=Marbis.nextLine();
        for(int z=0;z<=3;z++){
            a[y][z]=Marbis.nextDouble();
        }
        b[y]=a[y][0]+a[y][1]+a[y][2]+a[y][3];
        c[y]=b[y]/4;
    }
    System.out.println("Ahora usted verá los promedios de las personas:");
    System.out.println("Nombre:\t\t\tPromedio");
    for(int m=0;m<=4;m++)
        System.out.printf("%s:\t\t%.2f\n",x[m],c[m]);
}

Here I have the error: x[y]=Marbis.nextLine();
I know that I use it for two or more words in a Line, but in the second chances it marks me error, like this (This is the result, I think that I can use arrays in nextLine):

MArio Albert
100
100.00
78.00
100.00
Ingrese el nombre:
John Antoinie
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at vectormarbis1.MarbisVectors2.Promedios5(MarbisVectors2.java:125)
    at vectormarbis1.VectorMarbis1.main(VectorMarbis1.java:28)
C:\Users\ManoloAurelio\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 39 seconds)
  • It is because your `x[y]=Marbis.nextLine();` is taking the new line character as a string after you are taking the double value. Try flushing the input after taking double values. – VatsalSura Aug 07 '16 at 03:01

1 Answers1

0

You only flush the output in Java.

When to discard the rest of the line? To solve your problem you can call

input.nextLine();

You need to do this after nextDouble() as you expect to be reading from the next line.

I hope the below given code helps you solve your problem.

public void Promedios5(){
  Scanner Marbis=new Scanner(System.in);
  String[] x=new String[5];
  double[][] a=new double[5][4];
  double[] b=new double [5],c=new double[5];
  System.out.println("Este programa genera los promedios de las notas de cuatro unidades\n"
  + "se le solicitarán a usted, el nombre y las cuatro notas");
  System.out.println("Podría ingresarlas ahora por favor:");
  for(int y=0;y<=4;y++){
    System.out.println("Ingrese el nombre:");
    x[y]=Marbis.nextLine();
    for(int z=0;z<=3;z++){
      a[y][z]=Marbis.nextDouble();
    }
    Marbis.nextLine();    //Just add this line here
    b[y]=a[y][0]+a[y][1]+a[y][2]+a[y][3];
    c[y]=b[y]/4;
  }
  System.out.println("Ahora usted verá los promedios de las personas:");
  System.out.println("Nombre:\t\t\tPromedio");
  for(int m=0;m<=4;m++)
  System.out.printf("%s:\t\t%.2f\n",x[m],c[m]);
}
VatsalSura
  • 926
  • 1
  • 11
  • 27