Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double to text

 
Ian Cockcroft
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I have a little piece of code that returns a double from a function and then puts it into a text box

How do I change a doooouble into a string, and is this the best way to do it?
Thanks
Ian
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.valueOf(double d) should serve nicely.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or if you're lazy like me, just textBox.setText("" + calcSquare(9));
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, this is something that's posted over at the Cattle Drive for the Servlets assignments.
Remember, every time you concatenate with String, you create four new objects. Use an alternative to concatenation.
 
Ian Cockcroft
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats great, thanks guys.
my little function looks like this

i just need to change the string into a double???
How do i do this? Is there somewhere i can down load a spec of all the classes and there functions? there is one on the web, but i need one on my majine?
thanks guys.
ian
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just need to change the string into a double???
How do i do this?

Use the java.lang.Double class method parseDouble(String s), as in the following snippet:

Is there somewhere i can down load a spec of all the classes and there functions? there is one on the web, but i need one on my majine?

Try looking on the Java Sun website, under J2SE and documentation for the API (application programming interface).
 
Ian Cockcroft
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, now i'm getting an error at runtime
java.lang.NullPointerException at SquareRoots$ButtonListener.<init>(SquareRoots.java:75) at SquareRoots.<init>(SquareRoots.java:32) at SquareRoots.main(SquareRoots.java:87) Process Exit...
my code looks like this:

am i using the Action Listener correctly?
cheers
Ian
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post a copy of the entire code that you are using? I think the problem may lie in your use of the ButtonListener. Depending on how you've structured your code (whether the ButtonListener is an inner class or not, and if so what type), you may not be able to access certain methods of the outer class.
It seems more likely that, as you suggested, your use of the ActionListener is wrong. See if this helps:

I don't know if this helps without seeing the full code, but you could try it and see... Let me know if it works or not!
[ June 06, 2003: Message edited by: Charles Lyons ]
[ June 06, 2003: Message edited by: Charles Lyons ]
 
Ian Cockcroft
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charles,
It works great now, thanks.
My entire code follows.
Is there anywhere you can see that I should be doing differently?
Also, when I run it, the window is a little box in the top left hand side of the screen. how can i set the size and disable maximize button.
thanks alot
ian
my code:
 
Nitin Parashar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ian,
Try the following code,i have made few changes to your code.
1)Added exception handling
2)Changed the design
3)Formatted the double value.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class SquareRoots extends JFrame{
private JPanel textPanel,buttonPanel, basePanel;
private JTextField textBox, textAnswer;
private JButton btnCalculate, btnExit;

public final static Dimension hpad20 = new Dimension(20,1);
public final static Dimension vpad15 = new Dimension(1,15);
public final static Dimension hpad10 = new Dimension(10,1);
public final static Dimension vpad10 = new Dimension(1, 10);

public SquareRoots(String titleText){
super (titleText);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
SquareRoots.this.dispose();
System.exit(0);
}
});



// ******************************************************
// ******************** Text Panel*********************
// ******************************************************
textBox=new JTextField(10){
public Dimension getMaximumSize() {
return new Dimension(getPreferredSize().width, getPreferredSize().height);
}
};
textAnswer=new JTextField(10){
public Dimension getMaximumSize() {
return new Dimension(getPreferredSize().width, getPreferredSize().height);
}
};


textPanel= new JPanel();
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
textPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Find SquareRoot"));

JPanel wrapper1 =new JPanel();
wrapper1.setLayout(new GridLayout(2,2,5,10));
wrapper1.add(new JLabel("Enter Number"));
wrapper1.add(textBox);
wrapper1.add(new JLabel("Result"));
wrapper1.add(textAnswer);

JPanel wrapper2 =new JPanel();
wrapper2.setLayout(new BoxLayout(wrapper2, BoxLayout.Y_AXIS));
wrapper2.add(Box.createRigidArea(vpad15));
wrapper2.add(wrapper1);
wrapper2.add(Box.createRigidArea(vpad15));

textPanel.add(Box.createRigidArea(hpad20));
textPanel.add(wrapper2);
textPanel.add(Box.createRigidArea(hpad20));


// ******************************************************
// ******************** Button Panel*********************
// ******************************************************
btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ButtonListener());
btnExit = new JButton("Exit");
btnExit.addActionListener(new btnExitListener());
buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
JPanel butWrapper =new JPanel();
butWrapper.setLayout(new BoxLayout(butWrapper, BoxLayout.X_AXIS));
butWrapper.add(Box.createRigidArea(hpad20));
butWrapper.add(btnCalculate);
butWrapper.add(Box.createRigidArea(hpad10));
butWrapper.add(btnExit);
butWrapper.add(Box.createRigidArea(hpad20));

buttonPanel.add(Box.createRigidArea(vpad10));
buttonPanel.add(butWrapper);
buttonPanel.add(Box.createRigidArea(vpad15));

// ******************************************************
// ******************** base Panel*********************
// ******************************************************
basePanel= new JPanel();
basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.X_AXIS));

JPanel childPanel =new JPanel();
childPanel.setLayout(new BoxLayout(childPanel, BoxLayout.Y_AXIS));
childPanel.add(Box.createRigidArea(vpad15));
childPanel.add(Box.createRigidArea(vpad15));
childPanel.add(textPanel);
childPanel.add(buttonPanel);


basePanel.add(Box.createRigidArea(hpad20));
basePanel.add(Box.createRigidArea(hpad20));
basePanel.add(childPanel);
basePanel.add(Box.createRigidArea(hpad20));
basePanel.add(Box.createRigidArea(hpad20));

Container cp = getContentPane();
cp.add("Center",basePanel);

this.setSize(500,500);
this.setResizable(false);
this.pack();
this.setVisible(true);
}

public double calcSquare(double x){
return Math.sqrt(x);
}


class btnExitListener implements ActionListener{
public btnExitListener(){}


public void actionPerformed(ActionEvent e){
SquareRoots.this.dispose();
System.exit(0);
}
}


class ButtonListener implements ActionListener{
/* Use the constructor to set up any necessary constants or variables only.
Don't reference any particular events with it (e.g. clicking the button).
These should be done in the correct method */
public ButtonListener() { }

public void actionPerformed(ActionEvent e) {
/* Test to see if the source of the action event is really the textbox
(i.e. carriage return was pressed) */
if(e.getSource() == btnCalculate) {
try{
String y = textBox.getText();
double d = Double.parseDouble(y);
DecimalFormat z =new DecimalFormat("####.00");
textAnswer.setText(z.format(calcSquare(d)));
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(null,"Invalid Input. "+e1.getMessage(),"Input Error", JOptionPane.ERROR_MESSAGE);
textBox.setText("");
textAnswer.setText("");
}
}
}
}

public static void main(String[] args){
new SquareRoots("Square Roots");
}
}
 
Ian Cockcroft
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is great, thanks alot guys. Its actually for varsity, but I think I have learnt more on here than in any book. Thanks alot Guys
Warm Regards
Ian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic