12 Replies - 967 Views - Last Post: 07 December 2012 - 11:31 AM Rate Topic: -----

#1 xfire  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

double to string?

Posted 05 December 2012 - 02:44 PM

I can not figure out why the number will not come back as a string instead of a double, it used to work, now it no longer works.

snum=(Double.parseDouble(String.valueOf(display.getText())));


Any help would be nice!
Is This A Good Question/Topic? 0
  • +

Replies To: double to string?

#2 Ravioli27  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 34
  • Joined: 03-December 12

Re: double to string?

Posted 05 December 2012 - 02:49 PM

snum=""+display.getText()should work as far as i know. Double.parseDouble(String str) is a method that converts a string to a double not the other way around.
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: double to string?

Posted 05 December 2012 - 02:52 PM

getText() returns a String so that is enough.
Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: double to string?

Posted 05 December 2012 - 02:55 PM

display.getText() should be sufficient. You want a String, it's a String. /shrug

IF you want to validate that it's actually a double, the easiest way is to wrap a call to parseDouble in a try block. Use the catch block to reject the offered value (if you're in the catch block, it's not a double, so return to the user for another try) otherwise discard the parsed value and continue.

This post has been edited by jon.kiparsky: 05 December 2012 - 02:57 PM

Was This Post Helpful? 2
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: double to string?

Posted 05 December 2012 - 03:03 PM

Good advice on try/catch block for number verification. Swing also allows you to consume() some stuff.
Was This Post Helpful? 0
  • +
  • -

#6 xfire  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Re: double to string?

Posted 05 December 2012 - 03:09 PM

it did not really change anything
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: double to string?

Posted 05 December 2012 - 03:12 PM

Explain what you're trying to do. What are you expecting to find in display, and what does that need to be at the end of the day? And, by the way, what sort of component is display?

This post has been edited by jon.kiparsky: 05 December 2012 - 03:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 xfire  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Re: double to string?

Posted 05 December 2012 - 05:02 PM

i'm attempting to make a GUI-designed calculator, but when ever i do a simple equation, "2 + 2" = "0" but then when i try to go to the next equation, it doesn't clear like it's supposed to or the numbers that click add afterwards, as well as, i can't do numbers over "9 + 9" either

I can add the code and/or attach the program, if you want
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: double to string?

Posted 05 December 2012 - 05:28 PM

Code like you posted is designed to make things difficult to inspect:

snum=(Double.parseDouble(String.valueOf(display.getText())));


This should be written as:

String rawValue = display.getText();
String fiddledWith = String.valueOf(rawValue);
double result = Double.parseDouble(fiddledWith);


You can probably come up with better variable names but this is the best I can do without more context. As others have pointed out, this middle line does nothing (which is more obvious in my code than in yours):

String rawValue = display.getText();
double result = Double.parseDouble(rawValue);


Now you can start to inspect it. You can either set breakpoints in your debugger or insert a couple of println into your code to show the values of these two variables. Are they what you expected?

Edit: Is this piece of code even called? (a more common bug than you might think! Prove it with breakpoints/println)
Was This Post Helpful? 1
  • +
  • -

#10 xfire  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Re: double to string?

Posted 05 December 2012 - 06:21 PM

here's the program below, this is the entire program after a few different theories have been tried.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: double to string?

Posted 05 December 2012 - 08:33 PM

String.valueOf() returns as a String the argument.... don't what you would apply it to a String ???

double snum= Double.parseDouble(display.getText());

should achieve what you want or

String rawValue = display.getText();
double result = Double.parseDouble(rawValue);
Was This Post Helpful? 2
  • +
  • -

#12 xfire  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 12

Re: double to string?

Posted 06 December 2012 - 11:08 PM

i can still only do a single digit math
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: double to string?

Posted 07 December 2012 - 11:31 AM

You mean single number ?
If you want more than one number use a STring split() on + / - *
an process each substring individually... you will have to find a way to save the operator thought
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1