| Author |
Help! I need to convert a string arry into double array
|
terry amos
Greenhorn
Joined: Apr 19, 2009
Posts: 2
|
|
I am new to Java and trying to input 6 numbers in the JOptionPane and store those in a string array. I then need to convert those into doubles and find the largest and smallest then dispaly that in an applet.
The issue I am having is that I get this error when I try to run the program:
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:991)
at java.lang.Double.parseDouble(Double.java:510)
at Eagle.init(Eagle.java:32)
at sun.applet.AppletPanel.run(AppletPanel.java:425)
at java.lang.Thread.run(Thread.java:619)
Any help would be greatly appreciated.
Here is my code:
import javax.swing.JApplet; // program uses class JApplet
import javax.swing.JOptionPane; // program uses class JOptionPane
public class MinMax extends JApplet
{
// initialize applet by obtaining values from user
public void init()
{
//Create string array with six places
String[] sArray = new String[6];
//create double array same length as string array
double [] dArray = new double [sArray.length];
//create for loop to store six numbers read in from JOptionPane
for (int i = 0; i < sArray.length; i++) {
sArray[0] = JOptionPane.showInputDialog("Enter a number" );
}
//create a for loop to convert string array to double
for(int i = 0; i < sArray.length; i++)
{
dArray[i] = Double.parseDouble(sArray[i]);
}
//create for loops to determine smallest and largest values
double max = dArray[0];
for (int i=0; i<dArray.length; i++){
if (dArray[i] > max){
max = dArray[i];
}
}
double min = dArray[0];
for (int i=0; i<dArray.length; i++){
if (dArray[i] < min){
min = dArray[i];
}
}
//report the largest and smallest numbers entered in a JOptionPane
JOptionPane.showMessageDialog(null,
"The number with the smallest value is " + min
+ "\nThe number with the largest value is " + max);
}
}
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Your problem is here...
Basically, you are storing all the numbers into the zero element. This means that the zero elment will have the last number, and the other elements has null. Hence, the null pointer exception.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
terry amos
Greenhorn
Joined: Apr 19, 2009
Posts: 2
|
|
Thanks Henry I really appreciate the help
Terry Amos
|
 |
santhosh.R gowda
Ranch Hand
Joined: Apr 06, 2009
Posts: 296
|
|
please correct the code.... sArray[i] instead of sArray[0].....
for (int i = 0; i < sArray.length; i++) {
sArray[0] = JOptionPane.showInputDialog("Enter a number" );
}
|
Creativity is nothing but Breaking Rules
|
 |
 |
|
|
subject: Help! I need to convert a string arry into double array
|
|
|