JAVA Scanner NoSuchElementException: No line found

532 Views Asked by At

Sorry for baby question but I am very beginner. Please can you support with concern related to Scanner.

[BACKGROUND]: I wrote some code to study Scanner:

import javax.swing.JOptionPane;
import java.util.Scanner;

  class Main {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is day today? ");
        String day = scanner.nextLine();
        System.out.print("What is mounth today?");
        String mounth = scanner.nextLine();
        String outputText = "Today" + day + "mounth - " + mounth;
        JOptionPane.showMessageDialog(null, outputText);
               
    }
  
}

I have got an Exception: No line found. That is what I get in the output:

Task :run FAILED What is day today? Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at DemoOne.Main.main(Main.java:10)

FAILURE: Build failed with an exception.

I am using Apache NetBeans IDE 12.1. Looks like there is some of concern with System.in but I cannot understand how to fix it. Please support.

1

There are 1 best solutions below

10
On

You just need to make sure you are using System.out.println()

When you ask what month it is, you only used System.out.print()

See below

 Scanner scanner = new Scanner(System.in);
        System.out.println("What is day today? ");
        String day = scanner.nextLine();
        System.out.println("What is mounth today?"); //right here
        String mounth = scanner.nextLine();
        String outputText = "Today" + day + "mounth - " + mounth;
        JOptionPane.showMessageDialog(null, outputText);