Results 1 to 5 of 5
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question Re: How to check the value entered in the texbox is numeric or non numeric in java

    Quote Originally Posted by Java2012 View Post
    This is it:

    isDigit(char ch)

    Thanks
    I can appreciate what you have done for this person here, but I cannot get this to work right, because I am trying to check for a double value, and isDigit seems to not work for double values entered?? I have the following code, which is supposed to calculate annual pay for employees based on a base salary, and an annual commission determined by their sales accomplishments, but I need to add an error condition for if an inappropriate value is entered from the users, and to do this I need to make sure they entered a number and nothing else, as entering anything except for a number will crash the program....
    Java Code:
    package annual;
    import java.util.*;
    import java.lang.*;
    public class Annual {
        public static void main(String[] args)
        {   Scanner percent = new Scanner(System.in);
            System.out.println("Please enter annual sales:");
            double yearly = percent.nextInt();
            boolean Tnum = isDigit(yearly);
            double T = 50000;                 
            double Amt;
            double YrPrcnt = yearly * .05;
            Amt = T + YrPrcnt;
            System.out.print("The Annual Payrate for employee is:");
            System.out.print(Amt);
        }}
    Unfortunately, when I try to use isDigit, NetBeans keeps barking at me to create a method for it, and I have included the java.lang library, but it seems that isDigit will not check for a double?? The line boolean Tnum = isDigit(yearly); is what is upsetting NetBeans here.
    I REALLY need to create an error condition here, but I have no idea how to validate the user input in Java to be honest, so any advice will be greatly appreciated!!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: How to check the value entered in the texbox is numeric or non numeric in java

    Please don't hijack someone else's thread. I have moved your post to its own thread.

    The advice you quote is a bit out of context since I have moved the thread, but it refers to the java API, in particular the Integer (Java Platform SE 6) class, the method being static and this is how you must access it. The API has a wealth if information, for instance if you look at the Scanner method class you will see many different methods for reading input - if you need a double you can use the appropriate method of this class, or read a String and parse it using the appropriate method in the Double class (again, see the API)

  3. #3
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Smile Re: How to check the value entered in the texbox is numeric or non numeric in java

    Quote Originally Posted by doWhile View Post
    Please don't hijack someone else's thread. I have moved your post to its own thread.

    The advice you quote is a bit out of context since I have moved the thread, but it refers to the java API, in particular the Integer (Java Platform SE 6) class, the method being static and this is how you must access it. The API has a wealth if information, for instance if you look at the Scanner method class you will see many different methods for reading input - if you need a double you can use the appropriate method of this class, or read a String and parse it using the appropriate method in the Double class (again, see the API)
    My apologies, I just was interested in the idea of isDigit(). but it seems to not exist, or be of any true help....

    Also, I am not clear on this, " The API has a wealth if information", where would I find this API again?? I apologize, but although I have some background with Java, this is kind of my first experience with NetBeans, so I am not really well versed on the application to be honest.

    Thanks for letting me know that isDigit() is outdated, I will try to figure out Try & Catch a little better, and maybe go a different route with my validation concept, and again, thanks for taking a look at my post here anyway!!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to check the value entered in the texbox is numeric or non numeric in java

    He's given you a link to the API in his reply. As to why NetBeans is "barking", it's not NetBeans, its Java that's complaining because you're calling a method that doesn't exist pure and simple, and this has nothing to do with something being "out dated", it's just that you're calling the method as if you've created a static method with this name, but you haven't. But regardless of that, a method of this type at this location being called on a double variable is really meaningless. What are you trying to do with this any way?

  5. #5
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question Re: How to check the value entered in the texbox is numeric or non numeric in java

    Quote Originally Posted by Fubarable View Post
    As to why NetBeans is "barking", it's not NetBeans, its Java that's complaining because you're calling a method that doesn't exist pure and simple, and this has nothing to do with something being "out dated", it's just that you're calling the method as if you've created a static method with this name, but you haven't. But regardless of that, a method of this type at this location being called on a double variable is really meaningless. What are you trying to do with this any way?
    I am trying to validate user input at a prompt, because my program relies on them entering a numeric double value, and if they enter anything except a number it crashes.
    As to me calling a method that doesn't exist, I was trying to follow the instructions provided here to use isDIgit(), which I was led to believe was a pre-designed function provided by the java.lang library
    If that is incorrect, then I must have mistaken what I read on this forum concerning isDigit(), and would like any suggestions about how to verify that the users only input a number, Please!!

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 05:22 AM
  2. Replies: 0
    Last Post: 03-19-2012, 06:09 PM
  3. Replies: 3
    Last Post: 01-19-2011, 07:25 PM
  4. Replies: 13
    Last Post: 09-23-2010, 08:21 PM
  5. Replies: 2
    Last Post: 04-16-2008, 01:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •