Results 1 to 7 of 7
Thread: Many Errors After Do...While
- 04-03-2011, 06:00 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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); } }
-
- 04-03-2011, 06:32 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
you are getting error here
why you are declaring the array here?Java Code:Double[] numbers= new Double[n];
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 06:40 PM.
Don't Forget to try yourself before asking others help.....
Press REP, if you find their advices/solutions effective.
- 04-03-2011, 06:49 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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 06:53 PM.
- 04-03-2011, 07:00 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
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.
- 04-03-2011, 07:05 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
"=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?
- 04-03-2011, 07:15 PM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Well let n<=1 OR n>=26,then in first loop
This will never be executed.Java Code:if ((n > 1) && (n < 26)) numbers = new double[n];
Then in 2nd loop what do you think about this line???
Isn't here the array is not initialized? for this line only,you have to initialized it as null.Got it?Java Code:numbers[counter] = d;
Kind regardsDon't Forget to try yourself before asking others help.....
Press REP, if you find their advices/solutions effective.
Similar Threads
-
Need help with a few errors
By EmJay in forum New To JavaReplies: 3Last Post: 02-15-2011, 05:37 PM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
Help with three errors -.-
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 03-30-2010, 06:52 PM -
getting errors
By ravikumar in forum Threads and SynchronizationReplies: 3Last Post: 08-23-2009, 02:50 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks