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.
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.
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?
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?
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.
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
Re: How to Determine if a String is a Word or Number?
I'd say the one with all the numbers is a number.
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.
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.
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?
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.
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?
Re: How to Determine if a String is a Word or Number?
I would state whether or not it was.
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?
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"
?
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.
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?
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.
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.