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

Thread: Using FOR and WHILE loops to find average (DUE TONIGHT)

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Using FOR and WHILE loops to find average (DUE TONIGHT)

    The user should be instructed to enter three numbers and add them up by using a for loop to accomplish this. The program should then find the average of the three numbers.

    After this is accomplished the program should allow the user to add numbers using a while loop that terminates when the user enters -999. The number of entries the user has placed into the sum should then divide the sum.

    Here is the revision, but I still need help.

     public static void main(String[] args) throws FileNotFoundException {
    		PrintWriter prw = new PrintWriter("outfile5B.txt");
    		Scanner kb = new Scanner(System.in);
     
    		String input;
    		int num, sum = 0, average, numAmount;
    		final int DIV = 3;
     
    		// Part 1
    		System.out .println("Do you wish to start?");
    		input = kb.nextLine();
    		//the program terminates here, idk why?
    		if (input == "yes"){
    			for(int count = 1 ;count <= 3; count++){
    			num = kb.nextInt();
    			sum += num;
    			average = sum / DIV; // will this give me an average?
                System.out.println("Sum is: " + sum);
    			System.out.println("Count is: " + count);
    			System.out.println("Your average is: " + average);
     
    		}
    		// Part 2
    		System.out.println("Do you wish to add numbers? If so, enter how many. If not enter -999 to abort.");
    		numAmount = kb.nextInt();
    		while(numAmount > -999){
    			System.out.println("Enter your number(s)");
    			// how do I complete the average here?
    			average = sum / numAmount;
    			System.out.println("Your average is: " + average);
    			}
    		}
    		else if (input == "no"){
    			System.out.println("You have terminated the program");


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using FOR and WHILE loops to find average (DUE TONIGHT)

    I still need help.
    Please explain and ask some specific questions about your problems.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Looks like some code is missing at the end.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using FOR and WHILE loops to find average (DUE TONIGHT)

    better? there are questions in green

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using FOR and WHILE loops to find average (DUE TONIGHT)

    There is no green in formatted code.

    One problem I see is the use of the == operator to compare Strings. You need to use the equals() method.

    Another problem is the formatting. The code nested inside {}s should be indented 3-4 spaces to make the code easer to read and understand. The ending } should be inline beneath the start of the line with the pairing {

    Another possible problem is integer division. There are no decimal places: 5/2 = 2
    If you want decimal places change one of the operands to a float: 5/2.0 = 2.5


    What happens when the code is compiled and executed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Using FOR and WHILE loops to find average (DUE TONIGHT)

    I noted the couple tweaks that had to be made
    PrintWriter prw = new PrintWriter("outfile5B.txt");
    		Scanner kb = new Scanner(System.in);
     
    		String input;
    		int num, sum = 0, numAmount;
    double average; // made this a double for increased accuracy
    		final int DIV = 3; // no need for this
     
    		// Part 1
    		System.out.println("Do you wish to start?");
    		input = kb.nextLine();
    		//the program terminates here, idk why?
    		if (input.equals("yes")){ // Use the .equals command when comparing a string rather than ==     that was why it was terminating
    			for(int count = 1 ;count <= 3; count++){
    			num = kb.nextInt();
    			sum += num;
    			average = (double) sum / count; // divide by count to get the average of numbers entered so far, and type cast to double
                System.out.println("Sum is: " + sum);
    			System.out.println("Count is: " + count);
    			System.out.println("Your average is: " + average);
     
    		}
    sum = 0; // reinitialize sum as 0 for next loop
    int ct = 0; // this variable will count the number of integers entered so you can average them
    while(numAmount > -999){
    			System.out.println("Enter your number(s)");
    numAmount=kb.nextInt(); // put this inside the loop so it can continue to get input
    sum += numAmount;
    ct += 1;
    			// how do I complete the average here?
    			average = (double) sum / ct; // computes the new average
    			System.out.println("Your average is: " + average);
    			}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using FOR and WHILE loops to find average (DUE TONIGHT)

    @JakeM1130
    I noted the couple tweaks that had to be made
    Can you explain what the problem was and how you decided what to change to help the OP learn?
    Providing code with no explanation might not tell the OP what was wrong and why it was wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Using FOR and WHILE loops to find average (DUE TONIGHT)

    I included tags in the parts of the code I altered that explain it.

  8. #8
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Using FOR and WHILE loops to find average (DUE TONIGHT)

    Take a look at this for help on how Scanners work. Scanner (Java 2 Platform SE 5.0)

Similar Threads

  1. Loops: Taking the average of a list of numbers
    By janeeatsdonuts in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 09:38 AM
  2. Replies: 1
    Last Post: December 5th, 2012, 10:58 PM
  3. Help On Java Homework (DUE TONIGHT!!!)
    By agmolina90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2011, 08:28 AM
  4. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM

Tags for this Thread