Results 1 to 7 of 7
  1. #1
    jae5086 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Many Errors After Do...While

    The following is my broken code for a program that inserts 2-25 numbers into an array and averages them. It worked perfectly before I added the first do...while statement (line 9 - line 17) that ensures the user submits the correct number.
    Java Code:
    import javax.swing.*;
    public class Numbers
    {
    	public static void main(String args[])
    	{
    		double d, tot=0, av;
    		int n, counter=0;
    		do
    		{
    			String num = JOptionPane.showInputDialog("How many numbers would you like to enter?  Please choose 2 to 25 numbers.");
    			n = Integer.parseInt(num);
    			if ((n > 1) && (n < 26))
    				double[] numbers = new double[n];
    			else
    				System.out.println("Please enter a number between 2 and 25.");
    		}while ((n < 2) && (n > 25));
    		do
    		{
    			String ans = JOptionPane.showInputDialog("Enter a number.");
    			d = Double.parseDouble(ans);
    			numbers[counter] = d;
    			tot = tot + numbers[counter];
    			counter++;
    		}while (counter != n);
    		av = tot/n;
    		System.out.println("The average of these numbers is " + av);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    you are getting error here
    Java Code:
    Double[] numbers= new Double[n];
    why you are declaring the array here?
    Try this
    Java Code:
    import javax.swing.*;
    public class Numbers
    {
    	public static void main(String args[])
    	{
    		double d, tot=0, av;
    		int n, counter=0;
                     double[] numbers ;
    		do
    		{
    			String num = JOptionPane.showInputDialog("How many numbers would you like to enter?  Please choose 2 to 25 numbers.");
    			n = Integer.parseInt(num);
    			if ((n > 1) && (n < 26))
    			  numbers = new double[n];
    			else
    				System.out.println("Please enter a number between 2 and 25.");
    		}while ((n < 2) && (n > 25));
    		do
    		{
    			String ans = JOptionPane.showInputDialog("Enter a number.");
    			d = Double.parseDouble(ans);
    			numbers[counter] = d;
    			tot = tot + numbers[counter];
    			counter++;
    		}while (counter != n);
    		av = tot/n;
    		System.out.println("The average of these numbers is " + av);
    	}
    }

    And before your 2nd loop you must initialize the array.For missing this you will get an error here
    numbers[counter] = d;
    Last edited by UJJAL DHAR; 04-03-2011 at 07:40 PM.
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  4. #4
    jae5086 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    And before your 2nd loop you must initialize the array.For missing this you will get an error here
    Wasn't that what "double[] numbers;" did? Obviously not because, like you said, that is where I got the error, but how do I initialize it and do I still do so at the beginning of the program or should I be doing so between the loops?

    Edit: I realized my terminology was poor and that beginning statement is a declaration, not an initialization. However, I am still unsure how to initialize before the loop. I thought that was what the second loop was essentially doing.
    Last edited by jae5086; 04-03-2011 at 07:53 PM.

  5. #5
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    nothing just try it

    public static void main(String args[])
    {
    double d, tot=0, av;
    int n, counter=0;
    double[] numbers =null;
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  6. #6
    jae5086 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    "=null" worked like a charm! Might I inquire what that did to solve the problem exactly so I know in the future? Is it because there was no value specifically attached to the array from the beginning that lead to the error?

  7. #7
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Quote Originally Posted by jae5086 View Post
    "=null" worked like a charm! Might I inquire what that did to solve the problem exactly so I know in the future? Is it because there was no value specifically attached to the array from the beginning that lead to the error?
    Well let n<=1 OR n>=26,then in first loop
    Java Code:
    if ((n > 1) && (n < 26))
    			  numbers = new double[n];
    This will never be executed.

    Then in 2nd loop what do you think about this line???
    Java Code:
    numbers[counter] = d;
    Isn't here the array is not initialized? for this line only,you have to initialized it as null.Got it?

    Kind regards
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

Similar Threads

  1. Need help with a few errors
    By EmJay in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 06:37 PM
  2. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  3. Help with three errors -.-
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 03-30-2010, 07:52 PM
  4. getting errors
    By ravikumar in forum Threads and Synchronization
    Replies: 3
    Last Post: 08-23-2009, 03:50 PM
  5. Replies: 3
    Last Post: 03-08-2009, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •