0

I'm trying to get the average of up to 25 numbers. Right now, I'm confused on how to parse a String into an array. Here is the code as it stands:

final int SIZE = 25;
gradeArray = new double[SIZE];
String s;
int numElem = 0;
double average = 0;

do {
    s = (String) JOptionPane.showInputDialog("Enter Grade", "");
    if (s == null || s == ("")) {

    } else {
        try {
            grades = Double.parseDouble(s);
            if (grades > SIZE) 

        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Your input must be numeric!",
                    "Bad             Data!", 0);
        }
    }
} while (s != null || !s.equals(""));

The SIZE constant is for testing purposes.

5
  • What problem are you running into? I see you have Double.parseDouble (s), so you do have some knowledge is parsing. What's not working for you? Commented Dec 7, 2013 at 2:56
  • I don't see where you are attempting to add the string to the array. But in your case, you cannot; your array is an array of double values, which cannot be coerced into a double. What are you ultimately attempting to do? If you want to convert the string to a number for summing, you've already done that with the line Double.paraeDouble(s); line. Commented Dec 7, 2013 at 2:57
  • When I type that in, the compiler says I can't because it is a double[] not a double. Commented Dec 7, 2013 at 2:58
  • You may want to change your while condtion to && instead of || Commented Dec 7, 2013 at 2:59
  • introduce some variable for counter and then do something like gradeArray[counter++] = grades... btw. your code is really of bad quality (s == (""), s != null || !s.equals(""), etc.) Commented Dec 7, 2013 at 3:00

2 Answers 2

2
for(int i=0;i<SIZE;i++)
{
 s = (String)JOptionPane.showInputDialog("Enter Grade","");
    if(s != null && !(s.trim().equals(""))){
        gradeArray[i] = Double.parseDouble(s);
    }
    else{
        gradeArray[i] = 0;
    }

}

double sum=0;
for(int j=0;j<gradeArray.length;j++)
{
      sum+=gradeArray[j];
}


System.out.println("avaerage of grades ="+SIZE+" is ="+(sum/SIZE));
Sign up to request clarification or add additional context in comments.

Comments

0

As I understand you want to read up to 25 double constants as a single string and parse them into a double array.

final int SIZE = 25;
gradeArray = new double[SIZE];
String s;
int numElem = 0;
double average = 0;

do {
s = (String) JOptionPane.showInputDialog("Enter Grade", "");
if (s == null || s == ("")) {

} else {
    try {
       // this will find double values in your string but ignores non double values
       Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(s);
       int temp = 0;
       while (m.find())
       {
         gradeArray[temp] = Double.parseDouble(m.group(1));
         temp ++;
         if(temp >= SIZE){
             break;
         }
       }

    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Your input must be numeric!",
                "Bad             Data!", 0);
    }
}
} while (s != null || !s.equals(""));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.