| Having trouble yet again. :| We quickly went over booleans and barely went over our lab work and homework. This is what I have thus far. Not sure if it's right or anything.
The program asks the user to enter a string and inputs one. Then it looks at the string and prints a message stating whether it is the empty string, a word, a number, or something else. For this lab, a string is considered to be a word if it starts and ends with a letter; it is considered to be a number if it starts and ends with a digit; and something else if it is not empty and it is not a word and it is not a number.
For example, here are several sample runs of the program you will write (user inputs are in bold):
*
Enter a string: Table
"Table" is a word
*
Enter a string: 2abl3
"2abl3" is a number
*
Enter a string: Tabl7
"Tabl7" is something else
*
Enter a string:
Empty string
Checking if a character is a letter or a digit
To check whether a character is a letter or a digit, you can use the following methods:
* boolean Character.isLetter(char c) returns true if and only if c is a letter (lower case or upper case),
* boolean Character.isDigit(char c) returns true if and only if c is a digit (0-9).
import java.util.Scanner;
public class Lab3Part1 {
public static void main(String[] args)
{
keyboard = new Scanner(System.in);
String stringOne;
System.out.print("Please enter a string.");
stringOne = keyboard.nextString();
if ( stringOne == word )
{
System.out.println("Your string is a word.");
}
else ( stringOne = digit )
{
System.out.println("Your string is a digit.");
}
boolean StringOne.isLetter(char );
boolean StringOne.isDigit(char );
}
}
__________________ Live like you'll die tomorrow,
Dream like you'll live forever |