Java Basics

topic posted Wed, October 12, 2005 - 3:20 PM by  JavaJay
Share/Save/Bookmark
Advertisement
hello, I am working on a low level java project in which I will need to differentiate a string, char, or int from a user input.

I am working with character recognition commands such as
isLetter, isChar, etc.

any suggestions about a decent reference for such topics online?
posted by:
JavaJay
Columbus
Advertisement
Advertisement
  • Re: Java Basics

    Thu, October 13, 2005 - 8:03 AM
    What type of input are you getting data from?
    Is it from the command line?
    Reading from a stream?
    other type of input?

    If you are getting the input from the command line I would think that the first thing you would do is check to see the length of the argument... if the length of the string that you get is greater than 1 (assumming ascii) then your input is either a String or an int. Depending on the version of java that you are using you could then use the regular expresions and the String.match() method to determine if the value is an integer. If the arg length is only one character you could use the match() method again to determine whether the String is an int. If not than it must be a character. Well that is just off the top of my head.
    • Unsu...
       

      Re: Java Basics

      Thu, October 13, 2005 - 8:21 PM

      Another quick way to tell an int is to compare toLowerCase() with toUpperCase().
      • Re: Java Basics

        Fri, October 14, 2005 - 8:29 AM
        That would probably a good check to user for the isLetter() method.


        //might want to make this a static utility function.
        public boolean isLetter( String input_str, int pos )
        {
        boolean truth = false;
        String sub_str;//will contain a one character substring

        if( pos >= input_str.length() )//might want to throw an exception instead or not check... depending on your logic.
        {
        return truth;
        }

        sub_str = input_str.substring( pos, pos + 1 );// used to get the character at the position
        if( !sub_str.toUpperCase().equals( sub_str.toLowerCase() )//if the uppercase of the character does not match the lowercase then this must be a character a-zA-Z
        {
        truth = true;
        }
        return truth;
        }
  • Re: Java Basics

    Thu, October 13, 2005 - 5:39 PM
    I'm just using the keyboard... nothing heavy.

    Thanks for your help, appreciate it much.
    • Re: Java Basics

      Fri, October 14, 2005 - 3:32 PM

      Here's a nice clean way to test if a String is an integer...

      int intVal = 0;
      boolean isInteger = false;
      try {
      intVal = Integer.parseInt(s); // s is the String to test
      isInteger = true;
      }
      catch (NumberFormatException) {
      }

Recent topics in "Java Monkeys"

Topic Author Replies Last Post
Javascripting costs Schirin 3 May 17, 2007
Design patterns and principles emblylan 6 February 14, 2007
open source hosting alternatives? Mark 2 February 3, 2007
Ruby > Java Unsubscribed 6 January 9, 2007