-1

I'm getting an exception:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at package1.smth.main(clas1.java:19)

When I remove the while(go) part, everything is working fine. But I added it to be able to reset program, and now there is an exception. I also have a code for another similar program, where I added the same loop and it is working without this exception.
Can someone explain what's the problem?

public static void main(String[] args) {
    boolean go = true;
    while (go) {
        Scanner input = new Scanner(System.in);
        double a = 0;
        double b = 0;
        double c = 0;
        double discriminant = 0;
        double d = 0;
        System.out.print("Enter a : ");
        a = input.nextDouble();
        System.out.print("Enter b : ");
        b = input.nextDouble();
        System.out.print("Enter c : ");
        c = input.nextDouble();
        discriminant = (b * b - 4 * a * c);
        d = Math.sqrt(discriminant);
        if (discriminant >= 0.0) {
            System.out.println("first answer : " + (-b + d) / (2.0 * a));
            System.out.println("second answer : " + (-b - d) / (2.0 * a));
        } else if (discriminant == 0.0) {
            System.out.println("first answer : " + (-b) / (2.0 * a));
            System.out.println("second answer : " + (-b) / (2.0 * a));
        } else {
            System.out.println("no asnwers.");
            input.close();
        }
    }
}

I've read everything I could find similar to my problem, and most answers came from this site. I tried to implement the given solutions to my code, and some didn't work, some I could not understand how to use because my code is different from the example in a question. I am total newbie, it is probably third program I wrote.


UPDATE: The final code I have. The only problem it shows, is "leak: scanner not closed".

package gg;

import java.util.Scanner;

public class hbh {

    public static void main(String[] args) {
        boolean go = true;
        while (go) {
            Scanner input = new Scanner(System.in);
            double a = 0;
            double b = 0;
            double c = 0;
            double discriminant = 0;
            double d = 0;

            System.out.print("Enter a: "); 
            a = input.nextDouble(); 

            System.out.print("Enter b: "); 
            b = input.nextDouble();

            System.out.print("Enter c: "); 
            c = input.nextDouble();

            discriminant = (b * b - 4 * a * c);

            d = Math.sqrt(discriminant);
            if (discriminant >= 0.0) {
                System.out.println("First answer: " + (-b + d) / (2.0*a));
                System.out.println("Second answer: " + (-b - d) / (2.0*a));
            } 
            else if (discriminant ==0.0) {
                System.out.println("First answer: " + (-b) / (2.0*a));
                System.out.println("Second answer: " + (-b) / (2.0*a));
            }
            else {
                System.out.println("No answers");
                input.nextLine(); 
            }
        }
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Evan
  • 5
  • 5

2 Answers2

1

You should delete

input.close();

When you close the System.in once, you can not use it again after having closing it.


I can not find the source but here is a proof that this is causing the problem.

Try running the following code and you'll get the exact same exception.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    input.close();
    Scanner other = new Scanner(System.in);
    other.nextDouble();
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • i did so, but in that case program to every combination of numbers says "no answers" – Evan Nov 14 '15 at 22:08
  • @Evan this is another problem. Certainly with your algorithm. Accept this answer because it solves your problem and ask a new one concerning the algorithm. :) – Yassin Hajaj Nov 14 '15 at 22:12
  • thanks. the thing is, when I had no " boolean go = true; while (go) {" , the program gave me correct answers. so I am not sure what can be wrong in the algorithm if it worked well and Eclipse didn't mention any errors, plus I am total newbie haha – Evan Nov 14 '15 at 22:20
  • @Evan well once you looped, you are asking more than once the use of System.in after having closing it. – Yassin Hajaj Nov 14 '15 at 22:22
  • okay, I'll just leave the code without the loop :\ – Evan Nov 14 '15 at 22:29
  • @Evan You may still use your loop but you do not have to close the Scanner before your program ends... Do not forget to accept the answer :). – Yassin Hajaj Nov 14 '15 at 22:32
  • if I do not close the scanner, program always shows "no answers". – Evan Nov 14 '15 at 22:40
  • @TOm you're right. Thought this was the problem. Well, as I said, it is certainly a problem with the algo than. – Yassin Hajaj Nov 14 '15 at 22:49
  • @YassinHajaj Yes, we can just guess, since OP didn't even tell us which input he uses :(, but my bet is on a negative value in `discriminant` (obvious due to the "no answers" output :D) because of the formular `b * b - 4 * a * c`. – Tom Nov 14 '15 at 22:51
  • 1
    the input.nextLine() worked! thanks a lot :) – Evan Nov 14 '15 at 22:51
  • @Evan Well, it makes no sense, that this should help, but since your used code doesn't match the code in the question we can't know what you're _really_ doing there. – Tom Nov 14 '15 at 22:57
  • my used code IS what's in the question. maybe I explained myself incorrectly somewhere, that made you think otherwise. though it now shows "resource leak:input never closed", but it works. I'm a quite bad student, I do not know what I'm doing half the time, sorry :( – Evan Nov 14 '15 at 23:02
  • *"my used code IS what's in the question."* ... *"I'll just leave the code without the loop"* ... no what is true here? ;P. – Tom Nov 14 '15 at 23:15
  • @YassinHajaj Even it is very strange that `nextLine` is the solution here, you should add this to your answer ... but I don't know how to write that, since it is unclear why this helps here :D. – Tom Nov 14 '15 at 23:20
  • I'd delete "bootlean=true while go{ "from the code. But now I still have the same code, just instead of "input.close();" there is "input.nextLine();" – Evan Nov 14 '15 at 23:24
  • @Evan So you removed the loop (therefore the program runs only once) and you wrote `input.nextLint();` into the `else` branch ... so what issue had this fixed? You said you always get *"no asnwers."*, but how should the new `nextLine` call fix that, if this happens right after the output? – Tom Nov 14 '15 at 23:30
  • now it gives correct answers and the program starts over after the answers were given. – Evan Nov 14 '15 at 23:41
  • @Evan It starts over? Without the loop? So you have more code than just this ... another method which calls this `main` method? – Tom Nov 14 '15 at 23:50
  • "I still have the same code", I mean there is still the "bootlean=true while go{ ", the only thing that changed is NewLine instead of Close – Evan Nov 14 '15 at 23:53
  • So you still have the loop, ok. Now you removed `in.close` and with just `System.out.println("no asnwers.");` in the `else` branch you have an issue and if you add `in.nextLine()` to it, the issue is gone? If yes can you please provide example data so we can reproduce this problem? – Tom Nov 15 '15 at 00:11
  • I posted is an update for the question, the whole code(couldn't fit in as a comment). – Evan Nov 15 '15 at 03:01
  • @Evan It is preferable you post a new question because it is another problem. – Yassin Hajaj Nov 15 '15 at 10:45
  • @Evan I still don't see why you think that `input.nextLine();` is helping here, you can remove it without any harm. And please provide example data which to reproduce your former issuer. And about the other problem: there are already solution on Stackoverflow for that. – Tom Nov 15 '15 at 12:10
0

When you call, input.close() it not only closes your scanner but closes your System.in. Now you still keep iterating over the loop even when scanner is closed (and keep scanning values) which causes excpetion.

UPDATE

Your code seems to go in a infinite loop because you do not break from the loop neither do you update the value of go which at some point might terminate the loop.

Thus, the scanner will keep reading values until all the inputs are exhausted and eventually throw java.util.NoSuchElementException exception (when all the inputs are exhausted).

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38