In: Computer Science
Determining character type without using char methods. (epic confusion)
1. Call your class Program4A, so your filename will be Program4A.java. It is essential for grading purposes that everyone have the same class name.
5. Ask the user for a single character "Please enter a single
character, followed by the enter/return key.
6. Read in the character and assign it to a variable of char.
7. compile and run program. Don't move on until this part is
working. I have entered a temporary print statement just to make
sure this is working. It should be commented out before turning in
the program.
8. Use nested if-else statements to determine the character type.
(Cannot use char methods like isLetter or isDigit) (Must use single
quotes for char)
9. Put a printf statement after each if statement to print a sentence which states the type of character. (Required ugh)
10. After you get the earlier steps working properly for letter types (uppercase or lowercase) find the opposite case and print that. Do not use char methods.
Test userString's length in an if-else statement.
Sample run: (should look like this)
Please enter a character: 2
2 is a digit.
Please enter a character: d
d is a lowercase letter.
The uppercase of d is D.
Please enter a character: G
G is an uppercase letter.
The lowercase of G is g.
Please enter a character: $
$ is a special character.
Please enter a character: asdf
You entered more than a single character. Please rerun to try again.
Please enter a character: You did not enter anything. Please rerun to try again.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// Program4A.java
import java.util.Scanner;
public class Program4A {
public static void main(String[] args) {
//Declaring variables
char ch;
String str;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
//Getting the input entered by the user
System.out.print("Please enter a character:");
str=sc.nextLine();
if(str.length()>1)
{
System.out.println("You entered more than a single
character. Please rerun to try again.");
System.exit(0);
}
else if(str.isEmpty())
{
System.out.println("You did not enter anything. Please
rerun to try again.");
System.exit(0);
}
ch=str.charAt(0);
//Checking the type of character
if((ch>='A' && ch<='Z') || (ch>='a' &&
ch<='z'))
{
System.out.printf("'%c' is a character.\n",ch);
}
else if((ch>='0' && ch<='9'))
{
System.out.printf("'%c' is a digit.\n",ch);
}
else
{
System.out.printf("'%c' is a special
character.\n",ch);
}
System.out.print("Please enter a character:");
ch=sc.next(".").charAt(0);
//If the character is Upper case, converting it to Lower case
if(ch>='A' && ch<='Z')
{
System.out.printf("'%c' is a uppercase
character.\n",ch);
System.out.printf("The lowercase of '%c' is
'%c'\n",ch,ch+32);
}
else if(ch>='a' && ch<='z') //If the character is
Lower case, converting it to Upper case
{
System.out.printf("'%c' is a lowercase
character.\n",ch);
System.out.printf("The uppercase of '%c' is
'%c'.\n",ch,ch-32);
}
}
}
____________________________
Output#1:
Please enter a character:asdf
You entered more than a single character. Please rerun to try
again.
_____________________
Output#2:
Please enter a character:
You did not enter anything. Please rerun to try
again.
_____________________
Output#3:
Please enter a character:2
'2' is a digit.
Please enter a character:d
'd' is a lowercase character.
The uppercase of 'd' is 'D'.
_____________________
Output#4:
Please enter a character:G
'G' is a character.
Please enter a character:f
'f' is a lowercase character.
The uppercase of 'f' is 'F'.
_______________Could you plz rate me well.Thank You