1

Basically I am trying to write a program that will read a finite set of values from the user then print the average. (I know how to do the calculations so I will leave those out.)

I am having a problem with the logic side of the loop.

I understand that everyone here would prefer that I attempted it but I am new to loops and I am having extreme difficulties understanding loop logic.

I am attempting to do this assignment for my class but the teacher is flying through material and does not help at all when questions are asked. When I ask for help with a problem he says do your best to attempt it and I will grade it accordingly?

I honestly do not know where to start.

import java.util.Scanner;

public class P4Point5 {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String Status = "";
    int count = 0;

    while (in.hasNext()) {
        count++;
}        

    for (int i = 0; i < count; i++) {
//Do calculations here?
    }
}
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
NewToJava
  • 59
  • 1
  • 1
  • 9
  • When I was trying other ways to get the values from the user it made an infinite loop. – NewToJava Oct 14 '13 at 15:55
  • I think it will read allways an number from keuboard, but never reach the for loop. – Cold Oct 14 '13 at 15:58
  • My problem is not reading the numbers. I just do not know how to end the loop so that it can use the numbers to perform the calculations in either another loop or just a method to calculate the average, etc. – NewToJava Oct 14 '13 at 16:00

3 Answers3

5

You never get the next element:

while (in.hasNext()) 
    count++;

You are always on the 1st element and asking if there is a 2nd element.

You should use:

while (in.hasNext()) 
  int next = sc.nextInt();

BTW: please avoid statement without curly brackets. It is the root of all evil.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • I realized the brackets after. I know it is not good practice to leave them out. The line Integer next = sc.nextInt(); This will read only a single number and set it to next, right? – NewToJava Oct 14 '13 at 16:02
  • @NewToJava Read the documentation and you'll better understand what it does. – Maroun Oct 14 '13 at 16:02
  • @NewToJava Yes, as indicated in the javadoc http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextInt() `Scans the next token of the input as an int.` – Arnaud Denoyelle Oct 14 '13 at 16:07
  • But how would I make it exit the loop? It is constantly accepting values but what would I do to make it use the values? – NewToJava Oct 14 '13 at 16:11
  • @NewToJava You have to decide how to detect the end of the loop. There are usually 3 solutions : **either** you decide that `-1` means `stop` **or** you first ask the user howmany numbers to add, **or** at each step, you ask the user if he/she wants to add another number. – Arnaud Denoyelle Oct 14 '13 at 16:16
3

When you read for the first time, you don't read anymore, so hasNext() will stay always true since there will always be next element, which is.. the current element you're reading.

One solution is to do something like that:

String input = null;
while((input = in.next()) != null) {
     //...
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
0

You can try this:

while (in.nextInt() != 'SOME INT TO STOP LOOP') 
        count++;

Instead of verify every time if we have an entered number, we can verify if the entered number is an stop condition.

Cold
  • 787
  • 8
  • 23
  • If it is supposed to take any value, wouldn't making an int to stop the loop force a certain integer to not be allowed? – NewToJava Oct 14 '13 at 16:08
  • Good question. You can use an negative number, or, better, instead of, you can use `next()` and convert de given string, if its a number you count, if not, you exit the loop. – Cold Oct 14 '13 at 16:11
  • See here[http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java], good examples of verify if is a integer. – Cold Oct 14 '13 at 16:19