0

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.

Chaffy
  • 182
  • 12
Andrey
  • 1
  • 1

1 Answers1

1

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);
thetechnician94
  • 545
  • 2
  • 21
  • Both are valid methods of printing text, the only difference is that `println` will print the text and start a new line, whereas `print` only prints the text. This would not affect the input – CodingNinja Oct 19 '20 at 20:28
  • But you are correct that parallel structure is important to make the code and the output more readable – CodingNinja Oct 19 '20 at 20:29
  • @CodingNinja. Not quite. The cursor of the scanner is affected by System.out. Without the println, you dont have a clean input. Therefore, you have to press "enter" twice to get the scanner to advance (ie "What is mounth today" line is never printed) – thetechnician94 Oct 19 '20 at 20:30
  • The input worked fine when I ran it and I did not get any errors. Does the necessity of a new line vary by IDE? I'm using eclipse by the way. – CodingNinja Oct 19 '20 at 20:35
  • @CodingNinja. It could be that. Im using Apache Netbeans 12.0 (similar to OP). To be clear, I'm also not getting an exception. Just behavior that doesn't allow you to enter in the month. It waits on the second Scanner.nextLine() before the second System.out is ever printed. – thetechnician94 Oct 19 '20 at 20:36
  • Eclipse lets me enter the month but the input is right after "What is the mounth today?" right on the same line. So if i said "October" then the output would look like "What is the mounth today?October" – CodingNinja Oct 19 '20 at 20:41
  • I just get "What is day today" ...nothing... – thetechnician94 Oct 19 '20 at 20:42
  • Interesting that it's different – CodingNinja Oct 19 '20 at 20:42
  • 1
    well it seems like you figured out OP's problem. Have an upvote :) – CodingNinja Oct 19 '20 at 20:43
  • Hi gents, thank you support. I fixed cocnern. Originally I was using Gradle and getting this concern. I have changed from Gradle to Maven and now it works fine. Magic... – Andrey Oct 20 '20 at 18:58