I'm working on a programme that's supposed to accept input of doubles from an array
however it's not quite working out for me.

It takes in Integers alright but not the double values.

Could someone point me in the right direction

Any help is much appreciated

double array[] = new double[10];
           int total = 0;
        for (int counter = 0; counter < array.length; counter++)
          array[counter] = Integer.parseInt(JOptionPane.showInputDialog("Enter the figures for the array"));

          total= (int) (total+array[counter]);
        int average = total/10;

        JOptionPane.showMessageDialog(null, "the total is "+total);
        JOptionPane.showMessageDialog(null, "the average is "+average);


Recommended Answers

All 3 Replies

Integer.parseInt(... takes the string that the user entered and converts it to an int. You can put that into an array of doubles, but you will have lost everything after the decimal point. If you want to parse a string for a double value it's a very similar method... Double.parseDouble(...

Probably a silly question, but why not just use your total as a double also?

to prevent future errors, total should be of type double, average should be of type double too.. and divide by 10.0 instead of 10.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.