2

I'm using NetBeans 8.2 on Ubuntu. Is there any way to make s.hasNextDouble() returns false using keyboard input, so that the last line of the code is executed, without changing the code? This code snippet calculates the average of the entered numbers.

double sum = 0;
int n = 0;

Scanner s = new Scanner(System.in);
System.out.print("Enter 1 number: ");

while (s.hasNextDouble()) {
    double number = s.nextDouble();

    sum += number;

    n++;

    System.out.print("Enter " + (n + 1) + " number: ");
}

System.out.println();
if (n == 0) {
    System.out.println("Error!!!");
} else {
    System.out.println("Average: " + sum / n);
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
maria
  • 278
  • 4
  • 15
  • 1
    You want to send an EOF. See [this question](https://stackoverflow.com/a/16136924/5267751). – user202729 Jul 08 '18 at 13:08
  • For netbeans... see [this](https://stackoverflow.com/a/50672258/5267751). – user202729 Jul 08 '18 at 13:09
  • Hey OP. There are many ways to accept a key input in java. Maybe start with some hard requirements. A program can act exactly as you wish it to act (thats what make CS so cool). How do you want it to end? Pressing escape? Pressing ctrl+c? putting in no input? Without hard deliverable features, we cant help you. We're not mind readers ;) – flakes Jul 08 '18 at 13:15
  • @flakes I don't really know, basically, I'm still at the beginning, so I only know if I'm using if (number <0) but now I can't do that way :( – maria Jul 08 '18 at 13:20
  • 1
    @Maria "now I can't do it that way". How come? Is there any more to this question other than that someone told you not to do it this way? Maybe you need to go back to client/customer/teacher/whoever and try to figure out what they really want!! – flakes Jul 08 '18 at 13:21
  • 1
    @Maria, I can't make a post right now, but I will come back later if no one else has answered!! – flakes Jul 08 '18 at 13:53

2 Answers2

2

hasNextDouble returns false if the next token can't be interpreted as a double, so you can break that loop by entering something that's not a number. This could be anything like for example x, abc or !.

An example of running the program would be:

Enter 1 number: 3
Enter 2 number: 6
Enter 3 number: x

Average: 4.5

Process finished with exit code 0

You could change the message to something like:

System.out.print("Enter " + (n + 1) + " number, or anything else to end: ");
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • That's a possible way, but a way to send EOF may be more useful (for example OP use `hasNext()`) – user202729 Jul 08 '18 at 14:01
  • @user202729 The problem with that is that there's no guarantee that the user's console has a convenient way to send an EOF, or that the user knows how to do it. You wouldn't want to write a program which relies on it as a feature. – Radiodef Jul 08 '18 at 14:06
1

So, there are different ways to do this.

First way, the nearest to yours, is to simulate EOF with pressing CTRL+D as mentioned here How to send EOF via Windows terminal (I've tried, it realy works)

Second way is to think about some flag. For example, read until your number isn't -1 or SIMPLY ENTER SOME CHARACTER or something NOT DOUBLE or INT.

Third way is to read line by line, separate numbers using String[] nums = yourLine.split("\\s"); and than just go through your splited string array until Double.parseDouble(nums[i]) will throw exception

That's ways i could remember now. Good luck ;)