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

Thread: help using for while loop

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help using for while loop

    having trouble using for while loop

    this is what im supposed to do:

    Write a program that will read an unspecified number of positive numbers from the keyboard and determine the sum and the average of these numbers. A negative number should be used to terminate the input, i.e. when the program encounters a negative number, it should stop prompting and reading data and at that point should output the results. Note that the negative number that terminates the looping should not be added to the sum and certainly should not be counted as one of the valid numbers.
    A sample program run would look like this :
    Enter a number that is positive or zero 3
    Enter a number that is positive or zero 5
    Enter a number that is positive or zero 6
    Enter a number that is positive or zero 4
    Enter a number that is positive or zero -1
    You have entered 4 numbers
    Total of the numbers you entered is 18
    Average of the numbers is 4.5

    The program should use a For WHILE loop to accomplish it's mission.

    here is my code and i dont think its anywhere close to right

     
    import java.util.Scanner ; 
     
    public class Assign7_1_Roberts{
     
    	public static void main (String [] args){
    		//Create Scanner
    		Scanner input = new Scanner(System.in);
     
    		i = initialValue;
    		int number = 0, i = 0; sum = 0, numamount = 0; // You can change the variable names.
    		for(i = 0; i < 100; i++)
    		while(i < endValue1)
     
     
    		{ // Begin while statement
    		System.out.println("Enter  a  number  that  is  positive  or  zero: "); // Get the next number
    		String userInput = input.nextLine();
     
    		sum = sum+number; // Get the sum so far.
    		numamount++; // Get the number amount so far.
    		} // End while statement.
    		double average = sum/numamount;  //Get the average.
    		System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
    		System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
    		System.out.println("Average of the numbers is " + average); // Output average of numbers.
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help using for while loop

    Quote Originally Posted by robertsbd View Post
    having trouble using for while loop

    this is what im supposed to do:

    Write a program that will read an unspecified number of positive numbers from the keyboard and determine the sum and the average of these numbers. A negative number should be used to terminate the input, i.e. when the program encounters a negative number, it should stop prompting and reading data and at that point should output the results. Note that the negative number that terminates the looping should not be added to the sum and certainly should not be counted as one of the valid numbers.
    A sample program run would look like this :
    Enter a number that is positive or zero 3
    Enter a number that is positive or zero 5
    Enter a number that is positive or zero 6
    Enter a number that is positive or zero 4
    Enter a number that is positive or zero -1
    You have entered 4 numbers
    Total of the numbers you entered is 18
    Average of the numbers is 4.5

    The program should use a For WHILE loop to accomplish it's mission.

    here is my code and i dont think its anywhere close to right

     
    import java.util.Scanner ; 
     
    public class Assign7_1_Roberts{
     
    	public static void main (String [] args){
    		//Create Scanner
    		Scanner input = new Scanner(System.in);
     
    		i = initialValue;
    		int number = 0, i = 0; sum = 0, numamount = 0; // You can change the variable names.
    		for(i = 0; i < 100; i++)
    		while(i < endValue1)
     
     
    		{ // Begin while statement
    		System.out.println("Enter  a  number  that  is  positive  or  zero: "); // Get the next number
    		String userInput = input.nextLine();
     
    		sum = sum+number; // Get the sum so far.
    		numamount++; // Get the number amount so far.
    		} // End while statement.
    		double average = sum/numamount;  //Get the average.
    		System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
    		System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
    		System.out.println("Average of the numbers is " + average); // Output average of numbers.
    	}
    }
    1.) You never have an ending bracket for the for loop.
    2.) Where is endValue1 defined?
    3.) Either have each variable defined on its own line if you're going to initialize them right away or
    have them on the same line, but don't initialize them till later, but not too late that your for and while loops don't know what they are, and do so one per line.
    4.) You're doing nothing with the String userInput.
    5.) It appears that number will always be 0 inside your while loop. Is that what you want it to do?
    6.) since sum is set to 0 and so is number, it'll always be 0 for each iteration of the while loop.
    in other words

    0 = 0 + 0;

    0 = 0 + 0;

    number is never changing its value and so neither is sum.


    Maybe the number the user enters is supposed to be what you're adding.

    if so, it'd be input.nextInt().
    Last edited by javapenguin; October 29th, 2010 at 03:39 PM.

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help using for while loop

    so it seems i gotta lot of work to do..im really new to java so im still tryin to figure out what all lingo used in java means

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help using for while loop

    i also have to do the exact same thing but using the do-while loop

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help using for while loop

    im working on the do-while loop assignment and the output "enter a number that is positive or zero" doesnt end

    heres my code

     
    import java.util.Scanner ; 
     
    public class Assign7_2_Roberts{
     
    	public static void main (String [] args){
     
    		int data;
    		int sum = 0, numamount = 0; // You can change the variable names.
    		//Create Scanner
    		Scanner input = new Scanner(System.in);
     
     
     
    		do { // Begin do-while statement
    		System.out.println("Enter  a  number  that  is  positive  or  zero: "); // Get the next number
    		data = input.nextInt();
     
    		sum += data; // Get the sum so far.
    		} while (data >= 0); // End while statement.
     
    		double average = sum/numamount;  //Get the average.
    		System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
    		System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
    		System.out.println("Average of the numbers is " + average); // Output average of numbers.
    	}
    }
    Last edited by robertsbd; October 29th, 2010 at 05:22 PM.

  6. #6
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: help using for while loop

    In your do while loop, you are continuing as long as data is not equal to zero, but in the description of your assignment, it says to read numbers, until the user enters a negative number.

  7. #7
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help using for while loop

    ok i changed from !=0 to >=0 but when i enter a negative number i get this
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Assign7_2_Roberts.main(Assign7_2_Roberts.java:26)

  8. #8
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help using for while loop

    bump........

  9. #9
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: help using for while loop

    You are dividing by numaount when calculating average. You initialize numamount to 0, then it never changes. You can't divide by 0

Similar Threads

  1. help with a for loop
    By newbie79 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 02:20 AM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  4. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  5. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM