NoSuchElementException error while executing application

2

I wrote a simple code in the online java ide (follow code below):

import java.util.Scanner;

public class HelloWorld
{
  public static void main(String[] args)
  {
    String nome = "Filipe";    
      final double minimo = 15;    
    Scanner let = new Scanner(System.in);

    double n1;
        System.out.print("Informe a primeira nota: ");
        n1 = let.nextDouble();

    double n2;
        System.out.print("Informe a segunda nota: ");
        n2 = let.nextDouble();

    double n3;
        System.out.print("Informe a terceira nota: ");
        n3 = let.nextDouble();

    double n = (n1 + n2 + n3)/3;

    System.out.print("A media do aluno " + nome + " e " + n);    
    if (n < minimo) {
        System.out.print("O aluno foi Reprovado");
    }     
    else { 
        System.out.print("O aluno foi aprovado");
    }

  }
}

But at the time of compiling on ide online this occurred:

Informe a primeira nota: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at HelloWorld.main(HelloWorld.java:23)

Does anyone know why?

    
asked by anonymous 20.08.2016 / 16:17

1 answer

2

Enter here at Compile and Run Java Online and ask to run your code. It is compiling normally. There you will enter the data referring to the notes as it is in the code. After entering the 3 notes, the result will tell you if it has been disapproved or not. But if you do not enter any data, it happens this NoSuchElementException error in execution. You could solve your problem using this code below. With Scanner , you need to check for a next line with hasNextLine() , then using while you would get the value until something is typed.

while(let.hasNextLine()){
        n1=let.nextDouble();
        break;
}

But it does have one though, the code will depend on whether the input is formatted correctly. There you would have to put some conditions for the entries.

Check the result below:

Yourcompletecodelookslikethis:

importjava.util.Scanner;publicclassHelloWorld{publicstaticvoidmain(String[]args){Stringnome="Filipe";    
      final double minimo = 15;    
    Scanner let = new Scanner(System.in);

    double n1 = 0;
       System.out.print("Informe a primeira nota: ");
            while(let.hasNextLine()){
                n1=let.nextDouble();
                break;
            }

    double n2 = 0;
        System.out.print("Informe a segunda nota: ");
            while(let.hasNextLine()){
                n2=let.nextDouble();
                break;
        }

    double n3 = 0;
        System.out.print("Informe a terceira nota: ");
            while(let.hasNextLine()){
                n3=let.nextDouble();
                break;
        }

    double n = (n1 + n2 + n3)/3;

    System.out.print("A media do aluno " + nome + " e " + n);    
    if (n < minimo) {
        System.out.print("O aluno foi Reprovado");
    }     
    else { 
        System.out.print("O aluno foi aprovado");
    }

  }
}

See it working here .

    
20.08.2016 / 16:31