Shahrokh Aryana Shahrokh Aryana - 6 months ago 6x
Java Question

Loop to read doubles inputted from a user

So i have a program which is reading doubles inputted from a user, however, if the user does not input any data at the beginning, i want to print a message as so:

Enter data set:


No data!

However when i run my program and when the user inputs values and then presses ctl+d, even though there are values inputted the program still gives the No Data message. This is my code:

public static void main(String[] args) {

Scanner keyboard = new Scanner(;
System.out.println("Enter data set: ");
ArrayList<Double> nums = new ArrayList<Double>();

while (keyboard.hasNextDouble()) {
//I think this is the part that is causing the bug but im not sure how to fix it
if (!keyboard.hasNextDouble()) {
System.out.println("\nNo Data!");

How can i mody this program so that only when the user doesnt input any data does the message print, rather than all the time whenever the user ends the file? Thank you.

Jan Jan


 if (nums.isEmpty ()) {

as your condition maybe?