# Trying to convert Letters to telephone numbers

Page 1 of 1

## 5 Replies - 301 Views - Last Post: 20 October 2019 - 01:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417604&amp;s=47523e2fd3d5a8e1d6fc8bda7ab085fe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 smittie

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 18-October 19

# Trying to convert Letters to telephone numbers

Posted 18 October 2019 - 07:06 PM

I'm trying to write a program to convert letters into numbers like a phone pad. But all I get it is this:
Enter a Letter:
u
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Phone.main(Phone.java:11)
Is this code salvageable? If so help is appreciated.

```import java.util.Scanner;

public class Phone {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);

System.out.println("Enter a Letter:");

double var = in.nextDouble();

if( var >= 'a' && var <='c' || var >= 'A' && var <= 'C')
{System.out.println("2");
}
else if ( var >= 'd' && var <='f' || var >= 'D' && var <= 'F') {
System.out.println("3");
}
else if ( var >= 'g' && var <='i' || var >= 'G' && var <= 'I')
{System.out.println("4");}
else if ( var >= 'j' && var <='l' || var >= 'J' && var <= 'L') {
{System.out.println("5");}}
else if ( var >= 'm' && var <='o' || var >= 'M' && var <= 'O') {
System.out.println("6");
}
else if ( var >= 'p' && var <='s' || var >= 'P' && var <= 'S')
{System.out.println("7");}
else if ( var >= 't' && var <='v' || var >= 'T' && var <= 'V') {
{System.out.println("8");}}

else if ( var >= 'w' && var <='z' || var >= 'W' && var <= 'Z') {
System.out.println("9");
}

}
}
```

Is This A Good Question/Topic? 0

## Replies To: Trying to convert Letters to telephone numbers

### #2 ndc85430

• I think you'll find it's "Dr"

Reputation: 984
• Posts: 3,879
• Joined: 13-June 14

## Re: Trying to convert Letters to telephone numbers

Posted 18 October 2019 - 11:21 PM

A letter isn't a number is it? Why are you reading a double on line 11 and then in subsequent lines trying to compare that double with chars?

### #3 NormR

• D.I.C Lover

Reputation: 797
• Posts: 6,062
• Joined: 25-December 13

## Re: Trying to convert Letters to telephone numbers

Posted 19 October 2019 - 07:23 AM

You need to look at the API doc for the Scanner class to find a useful method.
If you read into a String, the String class has methods to access the char values it contains.

### #4 smittie

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 18-October 19

## Re: Trying to convert Letters to telephone numbers

Posted 19 October 2019 - 09:08 PM

NormR, on 19 October 2019 - 07:23 AM, said:

You need to look at the API doc for the Scanner class to find a useful method.
If you read into a String, the String class has methods to access the char values it contains.

So if my plumber brain is understanding this correctly I need to convert an input String to a Char, correct?

### #5 smittie

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 18-October 19

## Re: Trying to convert Letters to telephone numbers

Posted 19 October 2019 - 10:09 PM

Okay I think it's working now. Here's what I've got.
```import java.util.Scanner;

public class Phone {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);

System.out.println("Enter a Letter:");

String v = in.nextLine();

char var = v.charAt(0);

if( var >= 'a' && var <='c' || var >= 'A' && var <= 'C')
{System.out.println("The Corresponding number is 2");
}
else if ( var >= 'd' && var <='f' || var >= 'D' && var <= 'F') {
System.out.println("The Corresponding number is 3");
}
else if ( var >= 'g' && var <='i' || var >= 'G' && var <= 'I')
{System.out.println("The Corresponding number is 4");}
else if ( var >= 'j' && var <='l' || var >= 'J' && var <= 'L') {
{System.out.println("The Corresponding number is 5");}}
else if ( var >= 'm' && var <='o' || var >= 'M' && var <= 'O') {
System.out.println("The Corresponding number is 6");
}
else if ( var >= 'p' && var <='s' || var >= 'P' && var <= 'S')
{System.out.println("The Corresponding number is 7");}
else if ( var >= 't' && var <='v' || var >= 'T' && var <= 'V') {
{System.out.println("The Corresponding number is 8");}}

else if ( var >= 'w' && var <='z' || var >= 'W' && var <= 'Z') {
System.out.println("The Corresponding number is 9");}

else  {
System.out.println("Invalid Input");
}
}
}
```

This post has been edited by g00se: 20 October 2019 - 01:27 AM
Reason for edit:: Fixed code tags

### #6 g00se

• D.I.C Lover

Reputation: 3650
• Posts: 16,773
• Joined: 20-September 08

## Re: Trying to convert Letters to telephone numbers

Posted 20 October 2019 - 01:29 AM

Yes. Of course you can simplify that greatly. Just change the case to upper or lower and then you only need to check one set each time. You could also use a lookup table

Better to make your variables self-documenting. v is meaningless

This post has been edited by g00se: 20 October 2019 - 01:31 AM