0

I am a rookie in programming world and would appreciate if anyone can solve this problem and make me understand.the output is always "no data entered".

public class Trial {

public static void main(String[] args){
    Scanner firstno = new Scanner(System.in);
    int count = 0;
    double sum = 0;
    System.out.print("enter first number: ");
    double firstnoin = firstno.nextDouble();
    while (firstnoin != 0){
         sum += firstnoin;
         count = count++;
         System.out.print("Enter next number, or 0 to end: ");
         firstnoin = firstno.nextDouble();

    }
    if(count == 0){
        System.out.println("No data is entered.");
    }
    else {
        System.out.println("Average of the data is: "+(sum/count)+", number of terms are "+ count);
    }           

}
}
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Drake
  • 11
  • 4
  • Could you try assigning another variable for example int counter = count++; System.out.println("the value of counter :" + counter); – Blaze Jan 29 '16 at 09:47

1 Answers1

0

You have simply made a semantic error - this is when your program still compiles and works, but just doesn't work exactly how you wanted it to or doesn't do what you expected it to.

In your case, you have the following:

count = count++ 

If count = 0, this piece of count will simply keep count = 0. Hence why you are getting 'no data entered' every time.

Instead you need:

count++;

This will increment count by 1 each time.

Alternatively, you can use:

count = count + 1;

Again, this will take count's value and add 1 to it and then save this new value of count.

You can also change this to + 2, + 3 and so on and so forth as you wish and you can also do:

count += 1

which will also increment count by 1 and this can also be changed to 2, 3 and so on as you wish.

James
  • 1,471
  • 3
  • 23
  • 52
  • @Tunaki it definitely is a semantic error, this is when it still works but doesn't do what you wanted it to do, I wrote syntax the first time by accident. – James Jan 29 '16 at 09:47