• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry I really appreciate the help

Terry Amos
 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" );

}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic