How do i know the type of the input?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Salha
    New Member
    • Feb 2007
    • 9

    How do i know the type of the input?

    how can i make the input only string? such as other types like int and double wont be allowed?
    what kind of exception shall i use?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by Salha
    how can i make the input only string? such as other types like int and double wont be allowed?
    what kind of exception shall i use?
    if you are using Scanner
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

    you could call hasNextInt(), hasNextFloat(), etc which should all return false.

    If your strings should only contain alphabetic characters (no digits) you could use Pattern matching

    Comment

    • Salha
      New Member
      • Feb 2007
      • 9

      #3
      Originally posted by horace1
      if you are using Scanner
      http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

      you could call hasNextInt(), hasNextFloat(), etc which should all return false.

      If your strings should only contain alphabetic characters (no digits) you could use Pattern matching
      I am using GUI and i get the iput from the text field. So if the user enters digits he gets an error msg. But how can i do it?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Salha
        I am using GUI and i get the iput from the text field. So if the user enters digits he gets an error msg. But how can i do it?
        What if the user enters a string containing digits like r035198x. Would you allow that?

        Comment

        • Salha
          New Member
          • Feb 2007
          • 9

          #5
          Originally posted by r035198x
          What if the user enters a string containing digits like r035198x. Would you allow that?
          NO!! just words, it is a dictionary!!! please help :( my deadline is today!!!!!

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Salha
            NO!! just words, it is a dictionary!!! please help :( my deadline is today!!!!!
            Ah well I was just curious.

            Well do you know any regular expressions?
            If you don't then you can just write a method called say, hasDigit which returns a boolean

            Code:
             public static boolean hasDigit(String s) { 
             //Go through the string's chars one at a time looking for a digit
            }
            Then you can just use that method.

            Comment

            Working...