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);
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)) {
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.
I have a problem with this java script if I enter the input with spacing cant work. without spacing no problem. I have try a lot of edit but still cannot solve this problem, I wrote getloan will show the result, but if I write
get loan with spacing will show error. what I should add on to solve this?
Scanner read = new Scanner(System.in);
{
System.out.println("Enter the phone number: ");
String telLetter = read.next();
telLetter = telLetter.toUpperCase();
String telNumber = " ";
int count = 0;
int i = 0;
while (count <7)
{
switch (telLetter.charAt(i))
{
case 'A':case 'B':case 'C':
telNumber += "2";
count++;
break;
case 'D':case 'E':case 'F':
telNumber += "3";
count++;
break;
case 'G':case 'H':case 'I':
telNumber += "4";
count++;
break;
case 'J':case 'K':case 'L':
telNumber += "5";
count++;
break;
case 'M':case 'N':case 'O':
telNumber += "6";
count++;
break;
case 'P':case 'R':case 'Q':case 'S':
telNumber += "7";
count++;
break;
case 'T':case 'U':case 'V':
telNumber += "8";
count++;
break;
case 'W':case 'X':case 'Y':case 'Z':
telNumber += "9";
count++;
break;
}
if (count == 3)
telNumber += "-";
i++;
}
System.out.println(telNumber);
}
It fails because Scanner.next() will give you get and not get loan and you have hardcoded your loop to process 7 valid letters. Scanner by default delimits on whitespace.
You should probably use nextLine() instead of next() if you want to delimit on the new line.
Can anyone tell me what's wrong with my code, why am I not getting the correct letter count?
This program reads a text file and count each English letters, A-Z, and a-z, not case sensitive.
Thank you for helping.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Solution {
private static int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
public static void print(){
int[] in = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
for (int i = 0; i < in.length; i++){
System.out.println(in[i]);
}
}
public static void main(String[] args) throws FileNotFoundException{
File file = new File("t.txt");
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.toLowerCase();
for (int i = 0; i < line.length(); i++) {
switch(line.charAt(i)) {
case 'a': a++;break;
case 'b': b++;break;
case 'c': c++;break;
case 'd': d++;break;
case 'e': e++;break;
case 'f': f++;break;
case 'g': g++;break;
case 'h': h++;break;
case 'i': i++;break;
case 'j': j++;break;
case 'k': k++;break;
case 'l': l++;break;
case 'm': m++;break;
case 'n': n++;break;
case 'o': o++;break;
case 'p': p++;break;
case 'q': q++;break;
case 'r': r++;break;
case 's': s++;break;
case 't': t++;break;
case 'u': u++;break;
case 'v': v++;break;
case 'w': w++;break;
case 'x': x++;break;
case 'y': y++;break;
case 'z': z++;break;
}
}
}
print();
}
}
The problem is that when you encounter a i, that will increment the variable of the loop, not the one in the array. So you will skip letters.
Change it with :
for (int counter = 0; counter < line.length(); counter++) {
switch(line.charAt(counter)) {
your problem is in the use the variable i
in your for loop the index counter is i and i is also a variable which you are using to count the occurrences of alphabet 'i'. use this main method, it will work.
public static void main(String[] args) throws FileNotFoundException{
File file = new File("t.txt");
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.toLowerCase();
for (int index = 0; index < line.length(); index++) {
switch(line.charAt(index)) {
case 'a': a++;break;
case 'b': b++;break;
case 'c': c++;break;
case 'd': d++;break;
case 'e': e++;break;
case 'f': f++;break;
case 'g': g++;break;
case 'h': h++;break;
case 'i': i++;break;
case 'j': j++;break;
case 'k': k++;break;
case 'l': l++;break;
case 'm': m++;break;
case 'n': n++;break;
case 'o': o++;break;
case 'p': p++;break;
case 'q': q++;break;
case 'r': r++;break;
case 's': s++;break;
case 't': t++;break;
case 'u': u++;break;
case 'v': v++;break;
case 'w': w++;break;
case 'x': x++;break;
case 'y': y++;break;
case 'z': z++;break;
}
}
}
print();
}
also dont forget to close the scanner after you are done.