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

• April 19th, 2012, 02:31 PM
megnog
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.
• April 19th, 2012, 02:45 PM
Norm
Re: How to Determine if a String is a Word or Number?
Please post the full text of any error messages you get.
Quote:

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.
• April 19th, 2012, 02:47 PM
megnog
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?
• April 19th, 2012, 02:49 PM
Norm
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?
• April 19th, 2012, 03:00 PM
megnog
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.
• April 19th, 2012, 03:02 PM
Norm
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
• April 19th, 2012, 03:05 PM
megnog
Re: How to Determine if a String is a Word or Number?
I'd say the one with all the numbers is a number.
• April 19th, 2012, 03:07 PM
Norm
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.
• April 19th, 2012, 03:08 PM
megnog
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.
• April 19th, 2012, 03:12 PM
Norm
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?
• April 19th, 2012, 03:16 PM
megnog
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.
• April 19th, 2012, 03:19 PM
Norm
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?
• April 19th, 2012, 03:22 PM
megnog
Re: How to Determine if a String is a Word or Number?
I would state whether or not it was.
• April 19th, 2012, 03:26 PM
Norm
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?
• April 19th, 2012, 04:06 PM
megnog
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"

?
• April 19th, 2012, 04:06 PM
megnog
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.
• April 19th, 2012, 04:09 PM
Norm
Re: How to Determine if a String is a Word or Number?
Quote:

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?
• April 19th, 2012, 04:22 PM
megnog
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.
• April 19th, 2012, 04:31 PM
Norm
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.