2 Replies - 1136 Views - Last Post: 04 October 2014 - 07:40 PM Rate Topic: -----

#1 1290r   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-October 14

Cannot convert from String to Double

Posted 04 October 2014 - 06:04 PM

	   //Students Full Name  
	      firstName = JOptionPane.showInputDialog("Enter student " +
	                                              "first name.");

	      lastName = JOptionPane.showInputDialog("Enter student " +
	                                             "last name.");
           // Get test grade (numbers)
	      [b]test1 = JOptionPane.showInputDialog("Enter test1 grade")[/b];

The line in bold is where that error comes up. I know it something simple but I can't remember.
I declared both firstName and lastName as Strings and then the test1 I declared as double. I had a similar error in a previous assignment where I had a integer(age) input and then i had an output statement asking for a name all I needed to do was put keyboard.nextLine(); after my age input and I was fine. I'm sure it's something like that but I can't figure it out right now.

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot convert from String to Double

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon

Reputation: 12390
  • View blog
  • Posts: 45,519
  • Joined: 27-December 08

Re: Cannot convert from String to Double

Posted 04 October 2014 - 06:19 PM

You have to get the input as a String. Then you can use the Double.parseDouble(yourString) method to parse the String to a double.
Was This Post Helpful? 0
  • +
  • -

#3 AGENT_P6   User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 40
  • Joined: 05-May 14

Re: Cannot convert from String to Double

Posted 04 October 2014 - 07:40 PM

As a general rule, anything with an input field will by default take input as a String because any input field allows a user to use the keyboard - over 3/4 of which is NON-NUMBERS!! So since a user can input so many things other than numbers, it just make sense to take input as a String instead, even if the user ends up inputting all numbers

In Java, there is an OBJECT CLASS for primitive data types. So although there is a primitive double, there is also an object Double. Although there is a primitive int, there is also an object Integer class in Java. (and so on and so forth for all primitive data types. And then to top it off, there is another more generic superclass called Number, which is parent to all of the number object types like Integer, Double, and so forth.

One might wonder why the heck the ppl that made Java would make classes for things that already are primitives, especially when most people know that primitives are WAYYYYYY better in terms of performance. Well, your question is a perfect example: You have a program that takes a String and needs to treat that String as if it were a number. Ask yourself how would you do that? How could you take a String and be ABSOLUTELY SURE IT IS A NUMBER?!?!

The number object classes take care of that for you. They provide static methods (static means you can call them any time from anywhere in any program without creating an object first) to change data types. This is why macosxnerd5 said you have to do Double.parseDouble - you are calling the static method of the Double class. That method in class double will take a String and turn it into a primitive double if it is valid, or throw an exception if it is not valid, like if the number had a letter "r" in the middle or something weird like that.

The String class BTW also has similar methods. It can take primitive numbers and turn them into Strings.

Hope that helps...was a little long but hopefully informative :) Happy coding!

This post has been edited by AGENT_P6: 04 October 2014 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1