Alphabetic Telephone Number Translator using JOptionPane

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 6077 Views - Last Post: 31 October 2011 - 12:36 PM Rate Topic: -----

#1 Khalix  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 30-October 11

Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 04:39 PM

Many companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion:

A, B, and C = 2
D, E, and F = 3
G, H, and I = 4
J, K, and L = 5
M, N, and O = 6
P, Q, R, and S = 7
T, U, and V = 8
W, X, Y, and Z = 0

Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The program should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-GOOD the application should display 555-438-3663.



import javax.swing.JOptionPane;

public class Telephone
{
     public static void main(String[] args)
     {
	String input;
	int phoneNum; //to hold telephone number
	int [] letter;   						//  List variables
	int [] number;							//  that you will be using
												//  in the program.





	/*****************************************************************/ 		
	   //------------------Input the Data---------------------------
	   
	input = JOptionPane.showInputDialog("Enter a XXX-XXX-XXXX Telephone Number with Letters or Numbers: ");
	
	phoneNum = input();



I know that my code is wrong. I am having trouble figuring out how to get the input from the JOptionPane to be able to use an array. I think an array is the best was to do this, but I am also unsure if I should be using one array for letter and another for numbers. I just want to make sure that I am on the right track, and a little help in the right direction getting from the JOptionPane to the array would be appreciated.

My problem is that every example I can find in my textbook and online uses the system.out.prntln method and I am struggling to use the JOptionPane method...which is required in the assignment.

Is This A Good Question/Topic? 1
  • +

Replies To: Alphabetic Telephone Number Translator using JOptionPane

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 04:55 PM

The only reason to use an array is as a lookup table. A Map is another possibility. Read the characters separately with String.charAt(n)
Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,272
  • Joined: 27-December 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 05:08 PM

I agree with g00se here. I would use a Map<Character, Character>. So:
Map<Character, Character> map = new HashMap<Character, Character>();
map.put('A', '2');
map.put('B', '2');

char translate = map.get('B'); //returns '2'



That should get you a good idea of how to proceed.

This post has been edited by macosxnerd101: 30 October 2011 - 06:14 PM
Reason for edit:: Corrected illegal syntax

Was This Post Helpful? 1
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 05:13 PM

Yes. Digits and dashes can have identity mappings, then the whole output can be derived from the Map (cleaner)
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 05:47 PM

don't forget :)
map.put('-', '-');


Was This Post Helpful? 1
  • +
  • -

#6 Khalix  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 30-October 11

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 05:50 PM

I like the idea of this,and I have started putting it together. However I am still pretty new at this, and I can't find anything in my textbook about mapping. Is there a certain variable or class I need to import for the code that you listed to work?

I get the overall idea of using mapping but there is something I am not doing because I keep getting errors.

import javax.swing.JOptionPane;
import java.util.HashMap;
import java.util.Collection;

public class Telephone
{
     public static void main(String[] args)
     {
	String input;
	String number; //to hold telephone number   		
	HashMap hashMap = new HashMap();	
	
	
		/*****************************************************************/ 		
	   //------------------Input the Data---------------------------
	   	
	Map<Character, Character> map = new Map<Character, Character>();
	map.put('A', '2');
	map.put('B', '2');
	map.put('C', '2');
	map.put('D', '3');
	map.put('E', '3');
	map.put('F', '3'); 


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 05:55 PM

which errors ?
Was This Post Helpful? 1
  • +
  • -

#8 Khalix  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 30-October 11

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:01 PM

----jGRASP exec: javac -g Telephone.java

Telephone.java:36: error: cannot find symbol
Map<Character, Character> map = new Map<Character, Character>();
^
symbol: class Map
location: class Telephone
Telephone.java:36: error: cannot find symbol
Map<Character, Character> map = new Map<Character, Character>();
^
symbol: class Map
location: class Telephone
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:05 PM

Actually Map is an Interface use

Hashtable<Character, Character> map = new Hashtable<Character, Character>();
:^:
Was This Post Helpful? 1
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:11 PM

Hashtable is really a legacy class and in this case has the unneeded property of being synchronized
Was This Post Helpful? 1
  • +
  • -

#11 Khalix  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 30-October 11

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:18 PM

I'm still getting the same error. Here is the code exactly as I have so far. And I apologize, like I said this Map is new to me. It seems very handy, if only I could get it to work :)

import javax.swing.JOptionPane;
import java.util.HashMap;
import java.util.Collection;

public class Telephone
{
     public static void main(String[] args)
     {
	String input;
	String number;   		
	HashMap hashMap = new HashMap();	
	
	
			   	
			{
			Hashtable<Character, Character> map = new Hashtable<Character, Character>();			map.put('A', '2');
			map.put('B', '2');
			map.put('C', '2');
			map.put('D', '3');
			map.put('E', '3');
			map.put('F', '3');
			map.put('G', '4');
			map.put('H', '4');
			map.put('I', '4');
			map.put('J', '5');
			map.put('K', '5');
			map.put('L', '5');
			map.put('M', '6');
			map.put('N', '6');
			map.put('O', '6');
			map.put('P', '7');
			map.put('Q', '7');
			map.put('R', '7');
			map.put('S', '7');
			map.put('T', '8');
			map.put('U', '8');
			map.put('V', '8');
			map.put('W', '9');
			map.put('X', '9');
			map.put('Y', '9');
			map.put('Z', '9');
			map.put('-', '-'); 
			}
			
		input = JOptionPane.showInputDialog("Enter a XXX-XXX-XXXX telephone number with letters or numbers: ");
	


Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,272
  • Joined: 27-December 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:19 PM

Make sure you import HashTable from the java.util package.
Was This Post Helpful? 1
  • +
  • -

#13 Khalix  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 30-October 11

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:26 PM

using HashMap as opposed to HashTable seemed to fix it. Unless I am wrong and that is something else it ran with no errors when changing it to Map :)
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,272
  • Joined: 27-December 08

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:27 PM

View Postmacosxnerd101, on 30 October 2011 - 08:08 PM, said:

I agree with g00se here. I would use a Map<Character, Character>. So:
Map<Character, Character> map = new HashMap<Character, Character>();
map.put('A', '2');
map.put('B', '2');

char translate = map.get('B'); //returns '2'



That should get you a good idea of how to proceed.

I had some illegal, and misleading syntax in my previous example that I corrected a few minutes back. I'm sorry for my misleading example. Anyways, just to point out the corrected syntax.

And glad you found a solution! :)
Was This Post Helpful? 1
  • +
  • -

#15 Khalix  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 30-October 11

Re: Alphabetic Telephone Number Translator using JOptionPane

Posted 30 October 2011 - 06:34 PM

View Postmacosxnerd101, on 30 October 2011 - 06:27 PM, said:

View Postmacosxnerd101, on 30 October 2011 - 08:08 PM, said:

I agree with g00se here. I would use a Map<Character, Character>. So:
Map<Character, Character> map = new HashMap<Character, Character>();
map.put('A', '2');
map.put('B', '2');

char translate = map.get('B'); //returns '2'



That should get you a good idea of how to proceed.

I had some illegal, and misleading syntax in my previous example that I corrected a few minutes back. I'm sorry for my misleading example. Anyways, just to point out the corrected syntax.

And glad you found a solution! :)



And thank you for the help! So now once I list the
char translate = map.get('B');

lines will it pull that from the input and translate it? Or is there something else that I need to add other than that?

How do I run the input through the map and output in a JOption pane? I guess is the question I'm asking
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3