# Need help

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 04-19-2011, 12:09 AM
mikhail.abraham
Need help
i'm writing a program thats gonna ask the user to enter 5 test scores and calculate the avg and view the highest and lowest grade.

public class LabAssig8 {

public static void main(String[] args) {
getUserInput();
}

public static double[] getUserInput() {

int numT =4;
double[] scores;

scores = new double[numT];
do {
for (int i = 0; i <= numT; i++)

JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");

} while (scores[numT]>= 0 && scores[numT] <= 100);

return (scores);
}

The code above has no error but It's not looping. the score has to be between 0 and 100.

public static void calculateAverage(double[] scores) {

avg = scores[numT] / 5;
JOptionPane.showInputDialog("The average of 5 " + "test scores"

+ " is " + avg);

}

Is the code about right? I'm trying to calc the avg and I'm not able to do that since i'm still new to java

public static void findHighLow(double[] scores) {

Above is a method that I have to use to get the highest and lowest grade. Can any one help me PLEAAAASEE. I've been trying to figure out
something that works for like the past 3 days but nothing
Thanks
• 04-19-2011, 12:17 AM
sunde887
There are some problems. I will point out first that you may want to consider a for loop to get the input and no do while loop.

Since you know how many times you want to loop a for loop is ideal. Your goal will be to loop from 0 - array.length - 1, like this

Code:

```int[] blah = new int[10]; for(int i = 0; i < blah.length; i++){   //do stuff }```
This will loop x amount of times, where x is the length of the list. This will allow you do do something with each item in the array. With this you can get the user input and return the scores in the array correctly. The validation can also happen in the for loop with a while loop.

To find the average you will want to add the scores up, again, a for loop here is very ideal. Once you have added all the scores in the array you will need to divide it by the length of the array.

Finally, for finding the high and low variables you will also want to use for loops. Try correcting these two things and then post back. Be sure to use code tags when posting code.
[ code] //no space
[/code]
• 04-19-2011, 12:23 AM
mikhail.abraham
is this right?
Code:

```//  public static void main(String[] args) {         getUserInput(); }     public static double[] getUserInput() {                 double[] scores;         int[] numT = new int[4]; do {         for (int i = 0; i <= numT.length; i++)                 JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");         } while (scores[numT>= 0 && scores[numT] <= 10)         return (scores);         }```
• 04-19-2011, 12:30 AM
sunde887
It's close. Here is some pseudo code of this method
Code:

```declare scores array loop   get user input   loop if input is not from 0 - 100     get user input   end loop   store input in array end loop return scores array```
• 04-19-2011, 12:33 AM
mikhail.abraham
my problem is I know the steps but I'm not being able to write them as code :/
• 04-19-2011, 12:39 AM
sunde887
You don't want to use a do while loop. If your user enters a number that isn't 1 - 100 you want to start a loop which continues until the user enters a correct number. A for loop lets you loop x times, so you can loop through the array quite easily.
Code:

```for loop   while(input < 0 || input > 100){     prompt     store input   } end for loop```
• 04-19-2011, 12:43 AM
mikhail.abraham
I hope this is right
Code:

```\\ double[] scores;         int[] numT = new  int[4];         for (int i = 0; i <= numT.length; i++){             while(scores >= 0 || scores <= 10)             {                 JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");             }         }         return (scores);         }```
• 04-19-2011, 12:49 AM
sunde887
It's closer, you need to remember to test yourself as well. Your code has pseudo code like this
Code:

```declare scores declare and initialize numT for loop   while loop     prompt   end while end for return scores```
You are missing actually storing the input in the array, it should look more like this
Code:

```declare scores and initialize for loop   declare input and initialize with user input   while loop //only entered if input is out of range     store user input in input variable   end while   store in scores array end for return scores```
• 04-19-2011, 12:56 AM
mikhail.abraham
so declare input would be int = numT; right?
and how do you initialize it with user input?
I'm sorry I just don;t understand programming language :(
• 04-19-2011, 01:03 AM
sunde887
I don't mind helping you out. The input you want named inside the loop and it will be re created each time through the loop, and destroyed once it leaves the loop.

Code:

`JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");`
will return a string argument, so you want to capture it in a string, and then parse it to get the double grade.

Code:

```for loop String s = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); //stores the user input double score = Double.parseDouble(s); //parses the string above into a double while(score is not in correct range){   repeat above } store score in array end for```
• 04-19-2011, 01:11 AM
mikhail.abraham
Thanks soo much for wanting to help me.
Code:

```// 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 scores = Double.parseDouble(numT);                           while(scores <= 0 || scores >= 100)             {                           }         }         return (scores);         }```
I have a red line under double scores = Double.parseDouble(numT);
• 04-19-2011, 01:13 AM
mikhail.abraham
Code:

```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 scores = Double.parseDouble(numT);                           while(scores <= 0 || scores >= 100){                 JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");           }         }         return (scores);         }```
I guess it should be something like this?
• 04-19-2011, 01:21 AM
sunde887
This is close, but still a bit off, first the local variable in the loop should not have the same name as the array variable at the beginning, change scores inside the loop to score.

In the for loop you need to do the same thing to capture the value again(re use numT to store the input as a string, then use score to parse the string(numT), finally, in the for loop, at the end you must add the score to the scores array. Also, declare scores at the top as a double[].

You said in the method header that the method will return a double array, not an integer array, the compiler will complain if you don't match what the method header says.
• 04-19-2011, 01:30 AM
mikhail.abraham
Code:

``` 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){                 JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");                 double score = Double.parseDouble(numT);           }         }         return (scores);         }```
ok I still have a red line under double score = Double.parseDouble(numT);
• 04-19-2011, 01:42 AM
sunde887
Code:

```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) + ": ");//ADDED THE "numT =" PART HERE                 double score = Double.parseDouble(numT);           }         }         return (scores);         }```
Again, this is close, but whats the whole goal of this method? Is it to simply get user input and throw it away, or do you want to store it somewhere? What do you want to store it in? How do you store it?
• 04-19-2011, 01:49 AM
mikhail.abraham
hmmm I'm confused. what do you mean by store it?
• 04-19-2011, 02:02 AM
sunde887
At the end of your method you are returning the array named scores, but what does this array contain? When did you add values to it?
• 04-19-2011, 02:10 AM
mikhail.abraham
Am I suppose to return getUserInput(); ?
and Its only repeating once. If I enter 101 and it asks me again to reenter if I re enter a smaller number it ends.
• 04-19-2011, 02:37 AM
mikhail.abraham
```declare array initialize array for loop   declare string initialize with input   declare double initialize with parsed string     while(not in range)       repeat lines 3 and 4     end while end for```
`store input in array index i`