This question already has answers here:
String replace method is not replacing characters
(5 answers)
toUpperCase in Java does not work [duplicate]
(5 answers)
Closed 4 years ago.
I have seen this question asked a lot on here but I can't seem to figure out why my code doesn't work when the input are lower case characters.
When I input lower case characters it seems to execute endlessly until I terminate it. I have used the Character.toUpperCase(char) method but I assume that I haven't used it properly or there's a problem with the format of my code?
I just can't figure it out. I know that I can simply add case 'a': case 'A': to allow for both lowercase and uppercase input but I want to be able to use the Character.toUpperCase(char) method in future.
Can anyone help? Thanks in advance.
System.out.println("Please enter seven letters that you would like to convert into numbers:");
String inputNumber = console.next();
while (i < 8) {
letter = inputNumber.charAt(i);
Character.toUpperCase(letter);
if (i == 3)
{
outputNumber = outputNumber + "-";
}
switch(letter)
{
case 'A': case 'B':
case 'C': outputNumber = outputNumber + "2";
i++;
break;
case 'D': case 'E':
case 'F': outputNumber = outputNumber + "3";
i++;
break;
case 'G': case 'H':
case 'I': outputNumber = outputNumber + "4";
i++;
break;
case 'J': case 'K':
case 'L': outputNumber = outputNumber + "5";
i++;
break;
case 'M': case 'N':
case 'O': outputNumber = outputNumber + "6";
i++;
break;
case 'P': case 'Q': case 'R':
case 'S': outputNumber = outputNumber + "7";
i++;
break;
case 'T': case 'U':
case 'V': outputNumber = outputNumber + "8";
i++;
break;
case 'W': case 'X': case 'Y':
case 'Z': outputNumber = outputNumber + "9";
i++;
break;
case ' ': outputNumber = outputNumber + " ";
break;
default: outputNumber = "Invalid input.";
}
} System.out.println(outputNumber);
System.exit(0);
Character.toUpperCase(letter); doesn't modify letter, since char is immutable, and even if it wasn't, you can't modify the value of a variable by passing it to a method.
You need to write:
letter = Character.toUpperCase(letter);
Or replace:
letter = inputNumber.charAt(i);
Character.toUpperCase(letter);
with
letter = Character.toUpperCase(inputNumber.charAt(i));
Write either
letter = Character.toUpperCase(inputNumber.charAt(i));
or
switch (Character.toUpperCase(letter)) {
Related
I'm creating a code that will output a number based on the input letter. My code is stuck was stuck on a loop of doing character 0 repeatedly. Friend said that char Check = userInput.charAt(numTelephone); would fix it. When I placed that into my code I recieved the error: error: char cannot be dereferenced char Check = userInput.charAt(numTelephone);
import java.util.Scanner;
public class PhoneWords {
public static void main(String []args){
Scanner scnr = new Scanner(System.in);
int numTelephone;
char userInput;
userInput = scnr.next().charAt(0);
System.out.println("Enter a telephone number using letters (EXIT to quit): ");
System.out.println("The corresponding telephone number is: ");
if ((userInput >= 'A') || (userInput <= 'Z') || (userInput >= 'a') || (userInput <= 'z')){
for (numTelephone = 0; numTelephone < 7; numTelephone++){
char Check = userInput.charAt(numTelephone);
if (numTelephone == 3){
System.out.print("-");
}
switch (userInput) {
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
System.out.print("2");
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
System.out.print("3");
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
System.out.print("4");
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
System.out.print("5");
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
System.out.print("6");
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
System.out.print("7");
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
System.out.print("8");
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
System.out.print("9");
break;
default:
break;
}
}
}
}
}
The type char is primitive -- not an object -- so it cannot be dereferenced
Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced.
so get the char from the given input like:
char userInput = scnr.nextLine().charAt(0);
And comment this line which is suggested by your friend which is not used any where:
//char Check = userInput.charAt(numTelephone);
So I am writing code that will take a character and return its alpha-numeric keypad equivalent as a character.
The problem is, I am getting a question mark in a box as a return. I have checked that the input is correct. For example, with a char 'h' input, i should get a char '4' return. Hoping someone is able to spot my error.
Code is below:
public char getDigit(char letter) throws Exception{
switch (letter) {
case 'a': case 'b': case 'c': case '2':
return 2;
case 'd': case 'e': case 'f': case '3':
return 3;
case 'g': case 'h': case 'i': case '4':
return 4;
case 'j': case 'k': case 'l': case '5':
return 5;
case 'm': case 'n': case 'o': case '6':
return 6;
case 'p': case 'q': case 'r': case 's': case '7':
return 7;
case 't': case 'u': case 'v': case '8':
return 8;
case 'w': case 'x': case 'y': case 'z': case '9':
return 9;
default:
throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");
}
}
The return type of your method is char.
Now take your switch statement. You return char values in the range from 2 to 9. Now look at an ASCII table.
Surprise: these chars are all "unprintable" control characters. Thus your console gives you "?" when you print them!
If you want '4', your code has to return '4', not 4! Or 52, because that entry in the ASCII table represents '4'.
You are not getting proper output because you are using char as a return type of getDigit(..) function. You should use int as a return type instead of char as in switch case you are comparing with characters and returning digit values So replace your code with the following code, this will work:
public int getDigit(char letter) throws Exception{
switch (letter) {
case 'a': case 'b': case 'c': case '2':
return 2;
case 'd': case 'e': case 'f': case '3':
return 3;
case 'g': case 'h': case 'i': case '4':
return 4;
case 'j': case 'k': case 'l': case '5':
return 5;
case 'm': case 'n': case 'o': case '6':
return 6;
case 'p': case 'q': case 'r': case 's': case '7':
return 7;
case 't': case 'u': case 'v': case '8':
return 8;
case 'w': case 'x': case 'y': case 'z': case '9':
return 9;
default:
throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");
}
}
I wanna Write a program to print out the user's grade ("Excellent if grade is A, Very Good if grade is B, Good if grade is C, Fair if grade is D, Fail if grade is F"). using Switch Statement
But When User input a or b or c or d or f in lowercase Apply the Grade
I mean I wanna char = A or a Match the Grade "Excellent"
and char = B or b Match the Grade " Very Good " and so on
Thanks and I hope to Find a Quick Solution
I tried this But it doesn't Make anything
public static void main(String[] args)
{
char Grade = 'A';
switch (Grade)
{
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Very Good");
break;
case 'C':
System.out.println("Good");
break;
case 'D':
System.out.println("Fair");
break;
case 'F':
System.out.println("Fail");
break;
}
}
You have two ways:
Multiple case statements:
switch (Grade) {
case 'A':
case 'a':
System.out.println("Excellent");
break;
case 'B':
case 'b':
System.out.println("Very Good");
break;
case 'C':
case 'c':
System.out.println("Good");
break;
case 'D':
case 'd':
System.out.println("Fair");
break;
case 'F':
case 'f':
System.out.println("Fail");
break;
}
}
Converting the character to upper case:
char Grade = 'A';
Grade = Character.toUpperCase(Grade);
switch (Grade) {
...
}
You can simply covert the character entered by the user to either lower case or lower case and compare accordingly.
import java.util.Scanner;
public class ConvertLetterToNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter one letter");
String str = input.next();
char ch = str.charAt(0);
switch (ch){
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c':
System.out.println("the number is 2");
break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f':
System.out.println("the number is 3");
break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i':
System.out.println("the number is 4");
break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l':
System.out.println("the number is 5");
break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o':
System.out.println("the number is 6");
break;
case 'P':case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case's':
System.out.println("the number is 7");
break;
case 'T':case 't':case 'U':case'u':case'V':case 'v':
System.out.println("the number is 8");
break;
case 'W':case 'w':case 'X': case 'x': case 'Y':case'y':case 'Z':case 'z':
System.out.println("the number is 9");
break;
default :System.out.println("Error:invalid input");
System.exit(1);
}
}
}
the problem is if I enter two or more letters at one time (for example "ab" the answer will be "2", but what I want is "Invalid input". How can I add this instruction to ask user to enter only one letter?
You could check the str length before your switch, and return like so:
if (str.length > 1){
return;
}
So i have created this code to convert words into phone numbers, but when i try to run this code with letters under 7 word it will display string index out of range. But 7 or more is fine. How do i fix this? If so how do i set the string range?
{
System.out.println("Enter a word to be converted: ");
String telLetter = console.next ();
telLetter = telLetter.toUpperCase();
String telNumber="7";
int count=0;
int i=0;
while(count <7)
{switch(telLetter.charAt(i))
{case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
telNumber += "2";
count++;
break;
case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
telNumber += "3";
count++;
break;
case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
telNumber += "4";
count++;
break;
case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':
telNumber += "5";
count++;
break;
case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
telNumber += "6";
count++;
break;
case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
telNumber += "7";
count++;
break;
case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
telNumber += "8";
count++;
break;
case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
telNumber += "9";
count++;
break;
}
if( count==3) {
telNumber += "-";
}
i++;
}
System.out.println( telNumber );
}
}}
Fixes in code:
Use while(count < telLetter.length()) in place of while(count <7)...
telLetter.charAt(i) can be removed by (telLetter.charAt(count))... By doing this you don't need to create an extra variable int i = 0;... Its a good practice to keep variable minimum.
Use a scanner to get the input... Like.. Scanner sc = new Scanner(System.in); String telLetter = sc.next();...
After you used your resources do close them by using sc.close();....
Also i can see that you are appending 7 at the begining of every number... if that's a requirement then its ok.. other wise you can use... String telNumber="";
I would recommend using StringBuilder because in one object you can do it all.. While appending a character every time in string you are continuously creating a new string and giving it the reference of telNumber.
Also in your code either remove telLetter = telLetter.toUpperCase(); or remove the cases with lowercase alphabets.. because on making toUpperCase() you are just writing extra lines by writing cases for lowercase characters that are not needed...
System.out.println("Enter a word to be converted: ");
Scanner scan=new Scanner(System.in);
String telLetter = scan.next ();
int stringLength=telLetter.length();
telLetter = telLetter.toUpperCase();
String telNumber="7";
while(count <stringLength)
{
switch(telLetter.charAt(count))
Change your code like this.
But as per your code, the 7 is always prefixed in the converted phone number.
String telNumber=""; telNumber should be declared with empty string.
while(count <7)
Your code won't end until it has run through 7 characters. Try using while(count < telLetter.length())
Your while loop condition needs to check for the length of the telLetter string.
while (count <7 && count < telLetter.length())