aspose file tools*
The moose likes Beginning Java and the fly likes double to text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double to text" Watch "double to text" New topic
Author

double to text

Ian Cockcroft
Ranch Hand

Joined: Apr 05, 2001
Posts: 46
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


- Make it idiot proof and they'll make a better idiot!
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

String.valueOf(double d) should serve nicely.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Or if you're lazy like me, just textBox.setText("" + calcSquare(9));


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

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

Joined: Apr 05, 2001
Posts: 46
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

Joined: Mar 27, 2003
Posts: 836
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).


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Ian Cockcroft
Ranch Hand

Joined: Apr 05, 2001
Posts: 46
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

Joined: Mar 27, 2003
Posts: 836
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

Joined: Apr 05, 2001
Posts: 46
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

Joined: Jun 06, 2003
Posts: 52
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");
}
}


SCJP 1.4, SCWCD 1.4, SCBCD 1.3
Ian Cockcroft
Ranch Hand

Joined: Apr 05, 2001
Posts: 46
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: double to text