Results 1 to 8 of 8
  1. #1
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default Checking The Contents of a String

    Hey,

    Is it possible to check the contents of a string and treat the numeric and string values differently?

    For example if the user of the program enters "300 dogs",check for the presence of both the number "300" and the string "dogs" and use them separately.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jayragz View Post
    Hey,

    Is it possible to check the contents of a string and treat the numeric and string values differently?

    For example if the user of the program enters "300 dogs",check for the presence of both the number "300" and the string "dogs" and use them separately.
    Sure, you can use regular expressions for that task e.g. you can also do it manually by using one of the numerous methods implemented in the String class itself (with a bit of help from the utiliy methods in the Character class). Read the API documentation for those classes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    String (Java Platform SE 6)

    Just to speed up your search in the API.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    Thanx,though what Im seeing with string functions is that you have to know the exact value of what is inside the string,is there no way you can get unknown values.

    or do you have to test for every single number and letter?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    have to know the exact value of what is inside the string
    Not really. Then structure of the String must have some rules.
    If you know that the tokens/words in the String are separated by some character, you can use a method in the String class to put all the tokens/words into an array. Also the StringTokenizer class will do that.
    If there are no separators, eg "300dogs" then you'd have to look char by char.

  6. #6
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    Actually there is a space between the numerics and the characters. Maybe it just seems lazy but I want to get the number as a whole.

    Simply put If its "300 dogs" i want to make a calculation based on the fact that they are dogs.
    The checking if they are dogs is not a problem,
    its the number before the space that i want to fetch and deposit in a formula.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Again its a question of the format of your data. Do you know the first token is a number and the second token a String? Then parse the input into two tokens and use the Integer parse method to convert the first token to a number.

  8. #8
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    I solved the problem using the String.split() function and it worked just fine.

    Thanx for the help,it was really appreciated.

Similar Threads

  1. Checking If A String Contains Symbols
    By SwissR in forum New To Java
    Replies: 7
    Last Post: 07-27-2010, 09:07 AM
  2. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 06:09 PM
  3. Help with checking for a certain format in a String
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 04:56 AM
  4. Comparing contents of a file with String[]
    By eponcedeleon in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 09:10 PM
  5. spliting a string and checking each token's format
    By Implode in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 08:41 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
  •