5 Replies - 301 Views - Last Post: 20 October 2019 - 01:29 AM Rate Topic: -----

#1 smittie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

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

Reputation: 984
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 smittie   User is offline

  • New D.I.C Head

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

Re: Trying to convert Letters to telephone numbers

Posted 19 October 2019 - 09:08 PM

View PostNormR, 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?
Was This Post Helpful? 0
  • +
  • -

#5 smittie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3650
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1