Solved

# Is numeric

Posted on 2011-10-02
Medium Priority
234 Views
I need a routine to return true for
myData = 3
myData = 5.5

not for the rest.

Thank you.
0
Question by:Mike Eghtebas
• 14
• 9

LVL 47

Expert Comment

ID: 36901435
if(myData == 3 || myData == 5.5) {//do when is true}
else {//do when  is false}
0

LVL 47

Expert Comment

ID: 36901438
is myData a double variable?
or is it a String?
0

LVL 47

Expert Comment

ID: 36901442
if it is a String

then
if(myData.equals("3") || myData.equals("5.5")){ //do when true }
else {//do when false}
0

LVL 47

Expert Comment

ID: 36901453
Is it some string which is input by the user?

then

public boolean isGood(String s) {
float f = -1f;

try {

f = Float.parseFloat(s);

}
catch(Exception ex){

}

if(f == -1f) return false;

if(f==3.0f || f == 5.5f) return trye;
else return false;

}

0

LVL 47

Expert Comment

ID: 36901461
``````  public static boolean isGood(String s) {
float f = -1f;

try {

f = Float.parseFloat(s);

}
catch(Exception ex){

}

if(f == -1f) return false;

if(f==3.0f || f == 5.5f) return true;
else return false;

}

public static void main(String[] args) {

if(isGood("3.0")){
System.out.println("true");
}  else System.out.println("false");

}
}
``````

Output
``````true
``````
0

LVL 34

Author Comment

ID: 36901465
re:> is myData a double variable?

It is string actually. myData is string and is to have "5" or "5.6" etc
0

LVL 47

Expert Comment

ID: 36901468
This is with Strings and only 3 and 5.5 will be true, the rest would be false

``````public class Test {
public static boolean isGood(String s) {
float f = -1f;

try {

f = Float.parseFloat(s);

}
catch(Exception ex){

}

if(f == -1f) return false;

if(f==3.0f || f == 5.5f) return true;
else return false;

}

public static void main(String[] args) {

if(isGood("3.0")){
System.out.println(" 3.0  true");
}  else System.out.println("3.0  false");

if(isGood("5.5")){
System.out.println(" 5.5  true");
}  else System.out.println("5.5  false");
if(isGood("2.0")){
System.out.println(" 2.0  true");
}  else System.out.println("2.0  false");

if(isGood("-1")){
System.out.println(" -1  true");
}  else System.out.println("-1  false");
if(isGood("abcd")){
System.out.println(" abcd  true");
}  else System.out.println("abcd false");

}
}
``````
output:
`````` 3.0  true
5.5  true
2.0  false
-1  false
abcd false
``````
0

LVL 47

Expert Comment

ID: 36901471
Of course if they enter 5.6 that would be false - taht's what you want as I undersatnd
0

LVL 34

Author Comment

ID: 36901513
I was hoping for result like:

3.0  true
5.5  true
2.0  true
-1  true
abcd false
2.4.1  false
0

LVL 34

Author Comment

ID: 36901515
5 true
2 true
0

LVL 47

Expert Comment

ID: 36901526
no, both 5 and 2 returtn false
(look below)

Are you using my latest tested code?

Use the code below, or fro my previous posting

The fiorst posting was just outline
``````public class Test {

public static boolean isGood(String s) {
float f = -1f;

try {

f = Float.parseFloat(s);

}
catch(Exception ex){

}

if(f == -1f) return false;

if(f==3.0f || f == 5.5f) return true;
else return false;

}

public static void main(String[] args) {

if(isGood("3.0")){
System.out.println(" 3.0  true");
}  else System.out.println("3.0  false");

if(isGood("5.5")){
System.out.println(" 5.5  true");
}  else System.out.println("5.5  false");
if(isGood("2.0")){
System.out.println(" 2.0  true");
}  else System.out.println("2.0  false");

if(isGood("-1")){
System.out.println(" -1  true");
}  else System.out.println("-1  false");
if(isGood("abcd")){
System.out.println(" abcd  true");
}  else System.out.println("abcd false");

if(isGood("5")){
System.out.println(" 5  true");
}  else System.out.println("5  false");

if(isGood("2")){
System.out.println(" 2  true");
}  else System.out.println("2  false");

}
}
``````

``````3.0  true
5.5  true
2.0  false
-1  false
abcd false
5  false
2  false
``````
0

LVL 34

Author Comment

ID: 36901542
-1
2
5

return false but they should return true because they are numeric.
0

LVL 34

Author Comment

ID: 36901548
re:> Are you using my latest tested code?

I was describing that I desire them to return true. I wasn't testing 2 and 5 using your solution.
0

LVL 47

Expert Comment

ID: 36901556
OK, so you want true for any numeric or just when they are equakl to 3.0 and 5.5 ?
0

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36901559
this method will return true for any numeric:

``````   public static boolean isNumeric(String s) {
boolean b = true;

try {

float f = Float.parseFloat(s);

}
catch(Exception ex){
b = false;

}

return b;

}
``````
0

LVL 34

Author Comment

ID: 36901560
If they are numeric, return true, like:

3.0   true
5.5  true
2.0   true
-1    true
abcd false
5     true
2     false
4/3  false
2.1.1 false
0

LVL 34

Author Comment

ID: 36901562
testing   brb
0

LVL 47

Expert Comment

ID: 36901565
4/3 is numeric - it is 1.333333
0

LVL 34

Author Comment

ID: 36901567
4/3   false
1.2.1 false
0

LVL 47

Expert Comment

ID: 36901572
But "4/3" will still return false as you want
0

LVL 47

Expert Comment

ID: 36901575
1.2.1 - there is no question about it - it is not numeric
0

LVL 34

Author Closing Comment

ID: 36901576
Thank you
0

LVL 47

Expert Comment

ID: 36901583
But if you want to have epression and evaluate expressions, like 4/3, 3*5, 3+2, etc  - that is a whole different story
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Javaâ€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month13 days, 17 hours left to enroll