Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help! I need to convert a string arry into double array

 
terry amos
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
terry amos
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry I really appreciate the help

Terry Amos
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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" );

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic