• Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing to see if a string can be converted to a number  RSS feed

 
Natasha Reaney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Again, I'm new to Java so please bear with me.

My professor is using this code to test if a string can be converted to a number. But I can't seem to understand it. I get that ageAsString is assigned to the String data type, but how does testing to see if it can be converted to a number work? Can anyone explain it?



Thank you!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natasha Reaney wrote:how does testing to see if it can be converted to a number work? Can anyone explain it?


Forget Java for the moment. If you saw a sequence of characters written down on a piece of paper, how would you decide whether that sequence of characters could be interpreted as representing a numerical value? (And note that there can be many different rules defining what constitutes a number. It depends on your particular context and requirements.) If you figure out those rules, write them down in English (or whatever your native language is) as a sequence of very simple, clear, precise steps, then all that's left to do is translate those steps to their equivalent constructs in the Java language.

There are two important things to note about this: 1) Getting the steps right in English is the hard part. If you do it well, translating to Java will be much easier, probably even trivial. 2) This is the fundamental approach to solving any problem by writing a program. If you can't specify the requirements and the steps to implement them outside the computer language, it will be impossible--or at least very difficult--to express them in the programming language.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, having said all that, are you supposed to write the code to do this test yourself? Or can you/are you supposed to use the tools already provided in the core API that do this?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that particular code fragment, it's this line of code:



which does that testing. In particular it calls a method "isDouble()" which takes a String parameter and returns a boolean result -- this much can be determined by looking at the code. What the method actually does, and how it does it: it isn't possible to determine that without looking at the source code for the method.
 
Natasha Reaney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Paul, thank you, that helps.

@ Jeff, I think I understand what you are trying to get me to do, only maybe I am thinking too hard about it, i.e. trying to write down the rules but getting confused. I am just supposed to use the code provided by the core API.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natasha Reaney wrote:I am just supposed to use the code provided by the core API.


Okay, then, if you are going to have that boolean isDouble() method, then you need to define it as you're using it (and as Paul described it) and fill in its body to use what the API provides.

Look at the Integer class or the Double class, depending on your rules for what constitutes a number. There's a method there than can help you. You'll have to catch an exception if it's not a number, and handle it appropriately.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!