Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: How to Determine if a String is a Word or Number?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How to Determine if a String is a Word or Number?

    I need to write a program that determines whether a string inputted by the user is a word (all letters), a number (all digits), "something else" (starts with digit, ends with number, or vice versa), or an empty string. This is what I have so far:


    import java.util.Scanner;

    public class Lab3Part1 {

    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter a string: ");
    keyboard.nextLine();
    String userInput = keyboard.nextLine();

    char c1 = userInput.charAt(userInput.length());
    char c2 = userInput.charAt(userInput.length()-1);
    Character.isDigit(c1);
    Character.isDigit(c2);

    if (userInput = )
    {
    System.out.println("\"" + userInput + "\" is a word");
    }
    else if (userInput = )
    {
    System.out.println("\"" + userInput + "\" is a number");
    }

    }

    }


    Where do I go from here? I don't know how to code for determining if a string is a word, number, something else, or empty. Please help! Thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    Please post the full text of any error messages you get.
    Where do I go from here?
    Make a list of the steps you would take if you were to do it manually. Be real simple minded about the steps you'd take, computers have no imagination and do exactly what you tell them to do.

    You are using several methods that return values but are not saving and using those values to make decisions.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    That's the thing...I don't know what steps to take. If YOU were writing a program like this, what would YOU do?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    Forget about the program until you have the list of steps it would take to solve the problem. If some wrote something on a piece of paper and gave it to you, How would you decide if what was written was a number or not?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    If what was written was a number, then I would say it was a number. If what was written was a letter, then I would say it was a letter.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    Not simple minded enough. How would you determine if what was written was a number?
    Say there are 4 characters on the paper: 1234 or 1e33 or a444 or zesd
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    I'd say the one with all the numbers is a number.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    Not simple minded enough. What if you can only see ONE character at a time?

    Or say some one gives you the characters one at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    If I can only see one character at a time then I can only choose between whether it's a number or a letter.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    What would you do in each case? If the character was a numeric digit or if it was a non-numeric?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    If it was a numeric digit, then I would say so. If it wasn't, then I would say so.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    You were looking at one of 4 characters. You need to see all four before you can say for sure.
    What would you do after each of the two possible results of looking at the first character?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    I would state whether or not it was.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    But you have only seen 1 of 4 characters. Looking at The next three could change the results.

    Remember there are 4 characters written on the paper. The question was: Is it a number or not?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    Ok, I've figured most of it now. I have one last question, though. If I enter a string of "Tab17" for example, what would I code so that the output says, "'Tab17' is something else"

    ?

  16. #16
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    Because it keeps coming up as either a number or word, even though the string starts with a number and ends with a digit (and vice versa).


    Sorry for the double post.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    what would I code so that the output says, "'Tab17' is something else"
    What are the steps you'd take doing it manually? You should design the steps to be taken before writing the code.
    If you have the steps the program should take, which step are you having problems coding?
    Last edited by Norm; April 19th, 2012 at 04:12 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Determine if a String is a Word or Number?

    It seems to be a problem with the boolean operators I'm using.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to Determine if a String is a Word or Number?

    Can't say what is wrong without seeing the code and knowing what the code is supposed to be doing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 01:43 PM
  2. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  3. How to invert a word String
    By Truffy in forum Java Programming Tutorials
    Replies: 16
    Last Post: October 3rd, 2009, 02:44 AM
  4. How to invert a word String
    By Truffy in forum Java Code Snippets and Tutorials
    Replies: 16
    Last Post: October 3rd, 2009, 02:44 AM
  5. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM