Testing to see if a string can be converted to a number
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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!!
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!!
posted 13 years ago
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
@ 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, 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.
posted 13 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
| You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











