Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Why isn't this grade averaging working?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Why isn't this grade averaging working?

    I don't know if there is something wrong with my eclipse program, but nothing shows up on the console screen after I run this code(it runs without errors). All of the other codes are working perfectly, so there must be something wrong with this code that I can't find?

    import java.util.Scanner;
     
    public class SimpleAverage {
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
    		int total = 0;
    		int grade;
    		int average;
    		int counter= 0;
     
    		while ( counter < 10 ) {
    			grade = input.nextInt();
    			total = total + grade;
    			counter = counter++;
     
     
     
    		}
     
    		average = total/10;
    		System.out.println("This is the grade average" +average);
     
     
     
     
     
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why isn't this grade averaging working?

    It's waiting for you to make an input. Have you tried typing numbers in the console view and hitting return?

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this grade averaging working?

    I can input as many numbers as I want and press return or just type in a series of numbers pressing return with each number, the program doesn't react to them.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this grade averaging working?

    It's because you are not saving the variables that the user is typing try as an example
    int input=console.nextInt;

    --- Update ---

    Oh sorry since the user will put many integer example :25 65 78 23 ...
    You should put String input = console.nextLine;
    then in the while loop use counter.hasNextInt to check on every integer

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why isn't this grade averaging working?

    So it's not that nothing shows in the console, it's that the while loop doesn't appear to end as you'd planned. Why would that be? Well, because 'counter' never gets to be 10 or greater. Why don't you try printing 'counter' in the while loop to see if that's what's happening.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this grade averaging working?

    I'm saving the them to int grade.

    Which I then use to add up to the total.

    This loop repeats itself until the user inputs a certain amount of numbers.

    EDIT: It's not printing anything when I put a "system.out.println(counter)" into the loop.

    EDIT2: Now, whatever number or how many numbers i type in and press return, a "0" shows up.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why isn't this grade averaging working?

    Response to EDIT2:

    This is an interesting logic error:

    counter = 0;
    Loop:
    counter = counter++;
    endLoop.

    Have you figured it out yet? What's another (one of several) ways to say the same thing? Well, kind of . . . to say the same thing so that it works.

Similar Threads

  1. Don't know why this isn't working
    By Eucibous in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 24th, 2013, 04:35 PM
  2. Why isn't my collision working?
    By dan0194 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 5th, 2013, 01:57 AM
  3. [SOLVED] I can't figure out why this isn't working!
    By samjoyboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:04 PM
  4. Can someone please tell me why my do while loop isn't working?
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 20th, 2011, 03:52 PM
  5. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM