简体   繁体   中英

Java: Converting letters to digits on a phone keypad

I'm working on an assignment for a class in which I need to convert a user input of a letter into the number on a phone. Like A=2 or K=5 . I converted the user input into Uppercase then into ASCII. I'm using the ASCII for my if/else and it compiles and works but always prints

System.out.println (x+"'s corrosponding digit is " + 2); 

no matter what letter I input, also the final line

else System.err.println("invalid char " + x);

doesn't work, it just prints out the digit is 2 with x being the thing I enter.

public class Phone {

    public static void main (String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println ("Please enter a letter from A-Z.");
        char x = input.next().charAt(0);
        x = Character.toUpperCase(x);
        int y = x;
        System.out.println ("You entered the letter " + x);

        if (y>=65 || y<=67)
            System.out.println (x+"'s corrosponding digit is " + 2);
        else if (y>=68 || y<=70)
            System.out.println (x+"'s corrosponding digit is " + 3);
        else if (y>=71 || y<=73)
            System.out.println (x+"'s corrosponding digit is " + 4);
        else if (y>=74 || y<=76)
            System.out.println (x+"'s corrosponding digit is " + 5);
        else if (y>=77 || y<=79)
            System.out.println (x+"'s corrosponding digit is " + 6);
        else if (y>=80 || y<=83)
            System.out.println (x+"'s corrosponding digit is " + 7);
        else if (y>=84 || y<=86)
            System.out.println (x+"'s corrosponding digit is " + 8);
        else if (y>=87 || y<=90)
            System.out.println (x+"'s corrosponding digit is " + 9);
        else System.err.println("invalid char " + x);           
    }   
}   

Replace || with && in your if else statements.

On your if-block, your first condition is if (y>=65 || y<=67) . Let's open it up a little:

IF ( y >= 65 ) OR ( y <= 67 ) .

Do you see the issue? Since you've written an OR, the whole statement will always evaluate to true : for any int y, y is necessarily either greater than 65 or less than 67. I suspect you meant to write an AND ( && ).

You have wrong conditions, change || to && :

if (y>=65 && y<=67) ...

And correct all conditions like that.

Having example (y>=65 || y<=67) . It means that the condition is true always, because any y is always larger or equal then 65 or smaler or equal than 67 (hard to explain). If you want to check, if both of conditions are true at once, you have to use &&

Your first condition is if (y>=65 || y<=67) and for any character if any of these 2 conditions is satisfied, it will print x+"'s corrosponding digit is " + 2 and never check any of the other else if conditions. You need to replace || operator with && operator in all conditions.

Why not something like:

char x = input.next().charAt(0);
x = Character.toUpperCase(x);
int digit = 0;
switch (x ) {
   case 'A': case 'B': case 'C':
      digit = 1;
      break;
   case 'D': case'E': case 'F':
      digit = 2;
      break;
   //etc.
   default:
      break;
}
if ( digit < 1 ) {
   System.out.println( "The letter " + x + " is not on a phone pad" );
} else {
   System.out.println( "x + "'s corrosponding digit is " + digit);
}

Note that some phones may use different letter combinations; for example, some phones use "PQRS" for 7, and some use "PRS", omitting the Q.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM