Nosuchelementexception error when executing application

Asked

Viewed 312 times

2

I wrote a simple code in the online java ide (follows 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 in the online ide 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)

Can anyone explain to me why?

  • I’m new to java, so I can’t do anything but something as simple as that --'

  • Where is line 23?

  • Pow dude, line 23 corresponds to n1 = Let.nextDouble(); I saw it here

  • Probably because nothing was typed, see working: http://ideone.com/FYmje4

  • This, n has how to put some input

  • but how do I use this ideone?

  • The error occurs as @bigown explained, nothing has been typed. Validate the values before comparing them to see if any of them are null.

  • @Filipeteixeira going there and typing a code, choosing the Java language.

  • vlw ai for help

Show 4 more comments

1 answer

2


Get in the Compile and Run Java Online and ask to execute your code. It is compiling normally. There you will enter the data referring to the notes as it is in the code. After typing the 3 notes, the result will be warning you if you failed or not. However, if you do not enter any data, this error happens NoSuchElementException on execution. It would solve your problem using this code below. With Scanner, you need to check if there is a next line with hasNextLine(), ai using while you would pick up the amount until you typed something.

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

But there is a however, the code will depend on whether the input is formatted correctly. Ai you would have to put some conditions to the entries.

See the result below:

inserir a descrição da imagem aqui

Your complete code would look like this:

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 = 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.