?
Solved

Is numeric

Posted on 2011-10-02
23
Medium Priority
?
234 Views
Last Modified: 2012-06-21
I need a routine to return true for
myData = 3
myData = 5.5

not for the rest.

Thank you.
0
Comment
Question by:Mike Eghtebas
  • 14
  • 9
23 Comments
 
LVL 47

Expert Comment

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

Expert Comment

by:for_yan
ID: 36901438
is myData a double variable?
or is it a String?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36901442
if it is a String

then
if(myData.equals("3") || myData.equals("5.5")){ //do when true }
else {//do when false}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Expert Comment

by:for_yan
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

by:for_yan
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");

}
}

Open in new window


Output
true

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
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

by:for_yan
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");

}
}

Open in new window

output:
 3.0  true
 5.5  true
2.0  false
-1  false
abcd false

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
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

by:Mike Eghtebas
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

by:Mike Eghtebas
ID: 36901515
5 true
2 true
0
 
LVL 47

Expert Comment

by:for_yan
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");

}
}

Open in new window



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

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36901542
-1
2
5
 
return false but they should return true because they are numeric.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
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

by:for_yan
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

by:
for_yan earned 2000 total points
ID: 36901559
this method will return true for any numeric:
please, explain waht you want.

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

    try {

      float f = Float.parseFloat(s);

    }
    catch(Exception ex){
        b = false;

    }

  return b;

    }

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
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

by:Mike Eghtebas
ID: 36901562
testing   brb
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36901565
4/3 is numeric - it is 1.333333
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36901567
4/3   false
1.2.1 false
0
 
LVL 47

Expert Comment

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

Expert Comment

by:for_yan
ID: 36901575
1.2.1 - there is no question about it - it is not numeric
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 36901576
Thank you
0
 
LVL 47

Expert Comment

by:for_yan
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question