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 10 of 10

Thread: Easy Java Problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Easy Java Problem

    Hi,
    I have to create a simple program that takes an unknown amount of grades and averages them using Scanner. The program is supposed to allow any amount of grades as long as the numbers fall between 0 and 100. I know what I have isn't even close to finished, and that I'm supposed to use a for loop but I provided it to give insight into what I need. I can write the method easily enough, my biggest problem is just making scanner accept multiple values . If I could do that, I could finish it.
    	int h;
     
     
    		do{
     
    		Scanner kb = new Scanner(System.in);
    		System.out.println("Enter grades to average: ");
    		h = kb.nextInt();
     
     
    		} while (h>0);

    Thank you,
    Troop


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Easy Java Problem

    Welcome AATroop..

    1. You aren't using a for loop in this code (nor should you), you are using a Do While Loop.
    2. You are on the right track for making the scanner accept multiple values. I'm not sure if you know or not, but I'll give you the benefit of the doubt... You must declare your Scanner outside of your loop.
    3. You said your loop should only accept values from 0-100, right now if I enter 101, the program will still run.
    Simplicity calls for Complexity. Think about it.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Java Problem

    Oh, right, I meant a do while loop. I haven't exactly finished writing the method to obtain the average, my main question is how do you input multiple values using scanner? As in, I want to make it so I can enter 3 values on time (70+90+80) and 5 values using the same code and input (90+90+80+100+95) another time.

  4. #4
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Easy Java Problem

    It does receive multiple Values. As of now, based off your code, the user would have to input a value less than or equal to 0, to loop out of your loop. I suggest you think your logic through a bit more (considering the Conditional in the While loop).
    Simplicity calls for Complexity. Think about it.

  5. The Following User Says Thank You to Staticity For This Useful Post:

    JavaPF (October 7th, 2011)

  6. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Easy Java Problem

    I dont see the need for a loop in this.

    A much simpler way would be this:


    int grade1, grade2, grade3;

    System.out.println("Enter 3 grades to find average: ");
    Scanner scan = new Scanner(System.in);

    grade1 = scan.nextInt();
    grade2 = scan.nextInt();
    grade3 = scan.nextInt();

    int sum = (grade1 + grade2 + grade3);
    int average = (sum/3);

    System.out.println("the average of those 3 grades is " +average);



    create 3 integers: grade1, grade2, grade3
    (the 3 numbers you type in using the Scanner will be assigned to these 3 integers)

    To request 3 numbers you just type it out 3 times like i have done in my code, I dont think you want a loop for this.

    Then create an integer called sum which adds up the 3 values the user typed in

    Then create an integer called average which just divides the integer sum by 3.

    And then print out the integer average using + after the quotation marks.


    By the way this assumes the user is going to type in values 0-100. Im not sure what to do if they type an integer outside the range you want.

    Im all new to this too.

  7. #6
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Easy Java Problem

    Quote Originally Posted by djl1990 View Post
    I dont see the need for a loop in this.
    A loop is necessary for this program because you do not know how many grades the user is going to enter. Read his previous message.

    Im all new to this too.
    Simplicity calls for Complexity. Think about it.

  8. #7
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Easy Java Problem

    Quote Originally Posted by Staticity View Post
    A loop is necessary for this program because you do not know how many grades the user is going to enter. Read his previous message.



    Ah right, for some reason i thought he wanted to type in 3 grades.

  9. #8
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Easy Java Problem

    If your tutor or assignment advises to use a for loop you may want something like this:

    Scanner input = new Scanner (System.in);
    int count, n, sum = 0;
    //count is number of integers to be read
    //n is used to store the number and read in from keyboard
    //sum is used to keep current total

    System.out.println("enter the number of integers to be added: ");
    count = input.nextInt();
    for (int i = 0; i < count; i++){
    System.out.println("Enter the next integer: ");
    n = input.nextInt();
    sum = sum + n;
    }
    System.out.println("The total sum is " + sum);
    System.out.println("The average is " + sum / count);

  10. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Java Problem

    It sounds like you need to learn about arrays. Using an ArrayList, you can add items to an object array (you have to wrap the grades in an integer object wrapper) and then average them all at the end. This may be what your teacher would want you to do. (if not, it would certainly look good, because it allows you to access all of the individual grades) However, if you just want to find the average then your code would only need to look something like this:

    int total = 0;
    int numberOfGrades = 0;

    while (conditions){
    ... //handle the input
    total += input;
    numberOfGrades ++;
    }

    int average = total / numberOfGrades;


    This way, you don't have to make the user input the number of grades to be entered before hand (which would kind of defeat the purpose) but any number of grades could be entered until your specifications were met.

  11. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Java Problem

    No, that doesn't fit with the programs specifications and we want to avoid providing fully executable code. Your example only provides the average for 3 numbers (granted, it works for as many 3 number sets as you want, but still only 3 numbers), not for an indefinite quantity, like he needs.

Similar Threads

  1. Java Image Problem! Probably easy fix!
    By TrivialFate in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 14th, 2011, 07:12 AM
  2. Array code problem Please help fairly easy
    By LOPEZR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 10:51 AM
  3. Little easy to fix problem
    By adammint7 in forum Java Theory & Questions
    Replies: 9
    Last Post: April 14th, 2011, 10:07 AM
  4. Quick easy Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2011, 02:59 PM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM

Tags for this Thread