0

The following java code gives the following error: Exception in thread "main" java.util.NoSuchElementException: No line found.

boolean running = true;
    while (running) {
        Scanner sc2 = new Scanner(System.in);
        System.out.println("Enter an item to order:");
        String name = sc2.nextLine();
        
        System.out.println("Enter the price:");
        String price = sc2.nextLine();
        
        System.out.println("Enter the quantity:");
        String quantity = sc2.nextLine();
        
        orderItems.add(name);
        orderItems.add(price);
        orderItems.add(quantity);
        orderItems.add(";");
        
        System.out.println("Would you like to add another item?: (y/n)");
        if (sc2.nextLine() != "y") {
            running = false;
        }
    }

Any suggestions? I've gotten a similar error before, which was caused by using a new scanner for each input instead of using the same one for each input, but this error seems to be caused by a different issue. Thanks in advance.

  • 1
    `if (sc2.nextLine() != "y")` [hmmm.....](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) (not *the* problem, but *a* future problem you'll have) – Federico klez Culloca Jun 16 '21 at 14:19
  • 1
    Try changing `sc2.nextLine() != "y"` to `!sc2.nextLine().equals("y")`. If that fixes it, then it's a dupe of [this](https://stackoverflow.com/q/513832/3890632). – khelwood Jun 16 '21 at 14:20
  • The code you posted alone does not produce a `NoSuchElementException`. Please post a [mcve]. – Sotirios Delimanolis Jun 16 '21 at 14:46

3 Answers3

3

A similar issue is here, but you create a new instance every iteration of the while loop. Create a single Scanner in the first line of the method.

Uladzislau Kaminski
  • 2,113
  • 2
  • 14
  • 33
2
  1. Declare your scanner object only once. Your while loop was creating multiple scanners (see Multiple scanner objects)

  2. You were not performing the String comparison in the right way (see How do I compare strings in Java?)

    boolean running = true;
    Scanner sc2 = new Scanner(System.in);
    while (running) {
        System.out.println("Enter an item to order:");
        String name = sc2.nextLine();
    
        System.out.println("Enter the price:");
        String price = sc2.nextLine();
    
        System.out.println("Enter the quantity:");
        String quantity = sc2.nextLine();
    
        orderItems.add(name);
        orderItems.add(price);
        orderItems.add(quantity);
        orderItems.add(";");
    
        System.out.println("Would you like to add another item?: (y/n)");
        if (sc2.nextLine().equals("y")) {
            running = false;
        }
    }
    
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Ashish Mishra
  • 704
  • 1
  • 6
  • 20
  • I just corrected my code to put sc2 outside of the loop and change the if statement, but I still get the same error? – Dylan Payne Jun 16 '21 at 14:37
  • @DylanPayne since it's called `sc2`, do you happen to have *another* scanner somewhere before the code you posted? In which case, just get rid of `sc2` and use the other scanner directly. – Federico klez Culloca Jun 16 '21 at 14:39
  • would like to add that you should prefer BufferedReader class if you frequently face problems with Scanner class – Ashish Mishra Jun 16 '21 at 14:40
  • @FedericoklezCulloca can you please correct the code where you are assigning ```if (sc2.nextLine().equals("y")) { running = false;``` – Ashish Mishra Jun 16 '21 at 14:41
  • @AshishMishra I think you pinged the wrong person :) – Federico klez Culloca Jun 16 '21 at 14:46
  • @FedericoklezCulloca I do have another scanner, but it is being used to scan a file, not a line from the console, so surely I can't use the same scanner? It is also being used inside a try...catch, so it will not be accessible from outside the try...catch statement. – Dylan Payne Jun 16 '21 at 15:08
  • 1
    @DylanPayne then no, it's not that. But as suggested in another comment you should try to edit your question to include a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), that is the least amount of code possible that still allows us to reproduce the problem. Otherwise we can just guess. By trying to create that it's even possible that you'll discover the problem by yourself! – Federico klez Culloca Jun 16 '21 at 15:11
  • 2
    @FedericoklezCulloca Finally solved the issue. I'm using multiple classes and in one of the classes I used sc.close(). I removed this line and everything if FINALLY running smoothly. – Dylan Payne Jun 16 '21 at 15:52
1

Create only a single instance for the Scanner by providing it before the while loop. And also a suggestion, use !sc2.nextLine().equals("y") instead of sc2.nextLine() != "y"

boolean running = true;
    Scanner sc2 = new Scanner(System.in);
    while (running) {
        System.out.println("Enter an item to order:");
        String name = sc2.nextLine();
        
        System.out.println("Enter the price:");
        String price = sc2.nextLine();
        
        System.out.println("Enter the quantity:");
        String quantity = sc2.nextLine();
        
        orderItems.add(name);
        orderItems.add(price);
        orderItems.add(quantity);
        orderItems.add(";");
        
        System.out.println("Would you like to add another item?: (y/n)");
        if (!sc2.nextLine().equals("y")) {
            running = false;
        }
    }
Rohith V
  • 1,089
  • 1
  • 9
  • 23