I\'m in java one and i need help with this code. Please follow the directions an
ID: 3668719 • Letter: I
Question
I'm in java one and i need help with this code. Please follow the directions and use material corvered on java one. thank you.
Programming Concepts
1) Logical Operators
2) Compound if-Statements
3) while loops
Assignment Description
For this assignment, you will create a loop, in which you will continuously ask the user to enter a character. You will count how many of each type of character they entered, and display the results at the end.
Assignment Requirements
You must use if/else-if/else statements; no switch statements are allowed.
Do not have a condition for every character (i.e. don't have 94 if statements. You should only have about 4 or 5)
Do not count the '#' character itself as an 'other' character.
Assignment Suggestions
Each character is attributed to a numerical value, otherwise known as an "ASCII value." Look at the website http://www.asciitable.com.
On the website, there are 4 different columns that describe information about the ASCII value, which are "Dec", "Hx", "Oct", "Html", and "Chr". The only columns that we are interested in are "Dec" and "Chr".
"Chr" means char, which is the character that the user entered.
"Dec" means decimal, which is the decimal value (decimal, meaning base 10, so we are talking about int), of the character.
Each character has an int value. For example, the character 'K' has an int value of 75. The character '5' has an int value of 53. The character 'p' has an int value of 112. The character '+' has an int value of 43.
To determine if the user entered in a digit, we can use the code:
char c = in.next().charAt(0);
int numDigits = 0;
if (c >= '0' && c <= '9')
numDigits++; // Increase the numDigits counter by one.
Java interprets the character c as a numerical value. On the ASCII table, the "Dec" (this stands for "Decimal") value of the character '0' is 48, and the decimal value of the character '9' is 57, so the above if statement is exactly the same as:
char c = in.next().charAt(0);
int numDigits = 0;
if (c >= 48 && c <= 57)
numDigits++; // Increase the numDigits counter by one.
So if the character you enter is a '0', '4', '6', or '9', for example, the if statement will evaluate to true.
Suggested Variables
Four int variables, each one to hold a counter for each character. Examples:
int digits = 0, lower = 0, upper = 0, other = 0;
A char variable to hold user input. (Use in.next().charAt(0) to grab a char as input)
char c = in.next().charAt(0);
Sample Output User input is underlined Enter a character ('#' to stop): Enter a character ('#' to stop): e Enter a character '' to stop): S Enter a character ('#' to stop): 4 Enter a character '' to stop): 9 Enter a character ('#' to stop): % Enter a character ('#' to stop) : # Number of digits: 2 Number of lowercase letters: 1 Number of uppercase letters: 2 Number of other characters: 1 userCloki~$Explanation / Answer
Solution:
package com.wkrp.nancy.solutions;
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char inputChar;
int numDigits = 0;
int numLower = 0;
int numUpper = 0;
int numOther = 0;
do {
System.out.println("Enter a character('# to stop') : ");
inputChar = in.next().charAt(0);
if (inputChar >= '0' && inputChar <= '9') {
numDigits++; // Increase the numDigits counter by one.
} else if (inputChar >= 'a' && inputChar <= 'z') {
numLower++;
} else if (inputChar >= 'A' && inputChar <= 'Z') {
numUpper++;
} else {
if (inputChar != '#')
numOther++;
}
} while (inputChar != 35);
System.out.println("Number of digits : " + numDigits);
System.out.println("Number of lower letters : " + numLower);
System.out.println("Number of upper letters : " + numUpper);
System.out.println("Number of other character : " + numOther);
}
}
Sample Run:
Enter a character('# to stop') : 2
Enter a character('# to stop') : @
Enter a character('# to stop') : d
Enter a character('# to stop') : S
Enter a character('# to stop') : #
Number of digits : 1
Number of lower letters : 1
Number of upper letters : 1
Number of other character : 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.