Results 21 to 40 of 48
Thread: Need help
- 04-19-2011, 02:49 AM #21
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
Not sure.whats that line?
After trying a lot of things to be honest with you i'm lost.
I think i did the avg but I still have a line under "return"
I don't know whats the prob;/
Thats what I have soo far. and I don't think I can get any further lolJava Code:public class LabAssig8 { public static void main(String[] args) { getUserInput(); } public static double[] getUserInput() { int[] scores = new int[4]; for (int i = 0; i <= scores.length; i++) { String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); double score = Double.parseDouble(numT); while (score <= 0 || score >= 100) { numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); score = Double.parseDouble(numT); } } return (); } public static void calculateAverage(double[] scores) { double sum = 0; for (int i = 0; i <= scores.length; i++) sum = sum + scores[i]; double avg = sum / scores.length; JOptionPane.showInputDialog("The averageof 5 test scores is " + avg); } public static void findHighLow(double[] tests) { } }
- 04-19-2011, 03:04 AM #22
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Your average method looks correct. You are very close on the getUserInput() method.
Your loop doesn't actually do anything but collect the data and then forget about it. You are supposed to return an array of user input data, so you need a return statement. You are also supposed to be using a double[] not an int [].
- 04-19-2011, 03:14 AM #23
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
ohhh now I got it. I wasn't payin attention to the int[], I had return(scores); but it wasn't working because I had int[] not double. I'm right right? haha
ok I tested it and its working fine but instead of show enter score 1,2,3,4,5... it shows enter score 1.0, 2.0,.... why?
and how am I suppose to display avg?
thanks soooo much for being patience
- 04-19-2011, 03:17 AM #24
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Show me your most up to date code. It should be saying "Please enter score 1", 2, 3, since i is the variable you are using and i is an int.
- 04-19-2011, 03:23 AM #25
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
Got it fixed
now the problem is displaying the avg. I think I calculated it right but Its not displaying
here's my code
Java Code:public class LabAssig8 { public static void main(String[] args) { double[] r; r= getUserInput(); calculateAverage(r); } public static double[] getUserInput() { double[] scores = new double[4]; for (int i = 0; i <= scores.length; i++) { String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); double score = Double.parseDouble(numT); while (score <= 0 || score >= 100) { numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); score = Double.parseDouble(numT); } } return (scores); } public static void calculateAverage(double[] scores) { double sum = 0; for (int i = 0; i <= scores.length; i++) { sum = sum + scores[scores.length]; } double avg = sum / scores.length; JOptionPane.showInputDialog("The averageof 5 test scores is " + avg); System.out.println(avg); } public static void findHighLow(double[] tests) { } }
- 04-19-2011, 03:28 AM #26
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
There is still 1 big flaw, and 1 minor nitpick. The array only contains 4 elements. When you declare an array you specify the size in the brackets, not the max index it will contain.
Now for the big flaw. It's a small mistake but causes big problems. While I can give you a fairly good statement, I need you to be the one that actually figures it out.Java Code:int[] x = new int[4]; //has 4 elements, 0, 1, 2, 3 int[]x = new int[6]; //has 6 elements, 0, 1, 2, 3, 4, 5
Your code does nothing with the user input. All it does is collect it, parse it, and then move onto the next prompt. It never "saves" it. Try this
put that in your main method and see what is printed.Java Code:double[] r = getUserInput(); for(int i = 0; i < r.length; i++){ System.out.println(r[i]); }
- 04-19-2011, 03:33 AM #27
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
it gave me
Java Code:run: 0.0 0.0 0.0 0.0 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at LabAssig8.calculateAverage(LabAssig8.java:46) at LabAssig8.main(LabAssig8.java:19) Java Result: 1 BUILD SUCCESSFUL (total time: 7 seconds)
- 04-19-2011, 03:36 AM #28
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Alright, Im assuming you entered values in for each input. Why do you think all the values in the array are 0?
- 04-19-2011, 03:39 AM #29
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
causae r = 0?
I don't know haha kinda confusing
- 04-19-2011, 03:42 AM #30
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
But r got it's input from you, how come it never got added? Shouldn't the array that getUserInput() returned be filled with the user input? Why isn't it filled? Did you fill the returned array before returning it?
- 04-19-2011, 03:44 AM #31
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
let me guess. i'm missing count++ ?
If yes its probably cause i don;t know how to use it :)
- 04-19-2011, 03:55 AM #32
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
haha I'm tired of this. please tell me i'm a bit close to the end?
I've been workin on it since ever. like 5 hours every day for the past 3 days
My brain is damaged hahaha
- 04-19-2011, 04:04 AM #33
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are very close to the answer. In the getUserInput() method you are never populating the array you are returning. The population should occur in the for loop(after you get and verify user input.)
- 04-19-2011, 04:09 AM #34
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
My brain is about to explode haha
Sorry but I really didn't get it.
are we still working with getting the input?
I thought its right.
- 04-19-2011, 04:13 AM #35
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
We are still working on getting the user input. Since it doesn't actually get user input. The method may as well say
This is all your code is doing. The loop is really just asking the user for information and doing NOTHING WITH THE INPUT.Java Code:public static double[] getUserInput(){ double[] x = new double[4]; return x; }
- 04-19-2011, 04:15 AM #36
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
so whats the solution?
- 04-19-2011, 04:16 AM #37
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The solution is to store the user input in the array. Not going to give you the direct answer, sorry.
the input should be stored in the for loop.
- 04-19-2011, 04:21 AM #38
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
when you say input you mean numT right?
- 04-19-2011, 04:22 AM #39
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Perhaps. I mean the input you got and parsed from the user.
- 04-19-2011, 04:25 AM #40
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks