1. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## 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  Reply With Quote

2. ## 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

Java Code:
```int[] blah = new int;
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]  Reply With Quote

3. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## is this right?
Java Code:
```//
public static void main(String[] args) {
getUserInput();
}

public static double[] getUserInput() {

double[] scores;

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

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

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

return (scores);
}```  Reply With Quote

4. ## It's close. Here is some pseudo code of this method
Java 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```  Reply With Quote

5. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## my problem is I know the steps but I'm not being able to write them as code :/  Reply With Quote

6. ## 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.
Java Code:
```for loop
while(input < 0 || input > 100){
prompt
store input
}
end for loop```  Reply With Quote

7. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## I hope this is right
Java Code:
```\\
double[] scores;
int[] numT = new  int;

for (int i = 0; i <= numT.length; i++){

while(scores >= 0 || scores <= 10)
{
JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
}
}

return (scores);
}```  Reply With Quote

8. ## It's closer, you need to remember to test yourself as well. Your code has pseudo code like this
Java 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
Java 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```  Reply With Quote

9. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## 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 :(  Reply With Quote

10. ## 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.

Java 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.

Java 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```  Reply With Quote

11. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## Thanks soo much for wanting to help me.
Java Code:
```//
public static double[] getUserInput() {
int[] scores = new  int;

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);  Reply With Quote

12. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## Java Code:
```public static double[] getUserInput() {
int[] scores = new  int;

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?  Reply With Quote

13. ## 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.  Reply With Quote

14. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## Java Code:
```public static double[] getUserInput() {
int[] scores = new  int;

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);  Reply With Quote

15. ## Java Code:
```public static double[] getUserInput() {
int[] scores = new  int;

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?  Reply With Quote

16. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## hmmm I'm confused. what do you mean by store it?  Reply With Quote

17. ## 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?  Reply With Quote

18. Member Join Date
Apr 2011
Posts
30
Rep Power
0

## 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.  Reply With Quote

19. Member Join Date
Apr 2011
Posts
30
Rep Power
0

##   Reply With Quote

20. ## No, the method should return an array. In the for loop you are doing the following
Java Code:
```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```
You are missing the most important set
Java Code:
`store input in array index i`
where should this line go in my pseudo code above?  Reply With Quote

#### Posting Permissions

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