954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

converting a word into numbers

Ok so i have this work for my java class done so far. And i cannot figure out how to get the different letters from an inputted word to output as a number. The assignment is get a user to input a 7 letter word and output it as a phone number. The code I have is crap I'm sure but yeah.

import java.util.*;
import javax.swing.JOptionPane;
import java.lang.*;
public class Veneski_Chp5
{
	static Scanner console = new Scanner(System.in);
	
	public static void main(String[] args) 
	{
		char letter1;
		String inputMessage;
		String inputString;
		String outputMessage;
		int outputWord;
		
		
		inputMessage = "Enter in a word to be converted to a phone number";
		inputString = JOptionPane.showInputDialog(inputMessage);
		
		
		
		 letter1 = inputString.charAt(0);
		 letter2 = inputString.charAt(1);
		 letter3 = inputString.charAt(2);
		 letter4 = inputString.charAt(3);
		 letter5 = inputString.charAt(4);
		 letter6 = inputString.charAt(5);
		 letter7 = inputString.charAt(6);
		
		
		
	
		while (letter1 != '#' )
		{
			outputMessage = "The word you entered is: "
				+ inputString + "\n"
				+ "The Phone number corresponding with that word is: ";
		
		if (letter1 >= 'A' && letter1 >= 'Z')
		{
			switch (letter1)
			{
			case 'A':
			case 'B':
			case 'C':
			case 'a':
			case 'b':
			case 'c':
				char letter1Num2;
				letter1 = letter1Num2;
				
				break;
			
			case 'D':
			case 'E':
			case 'F':
			case 'd':
			case 'e':
			case 'f':
				char letter1Num3;
				letter1 = letter1Num3;
				
				break;
				
			case 'G':
			case 'H':
			case 'I':
			case 'g':
			case 'h':
			case 'i':
				char letter1Num4;
				letter1 = letter1Num4;
				
				break;
				
			case 'J':
			case 'K':
			case 'L':
			case 'j':
			case 'k':
			case 'l':
			   char letter1Num5;
			   letter1 = letter1Num5;
			   
				break;
				
			case 'M':
			case 'N':
			case 'O':
			case 'm':
			case 'n':
			case 'o':
				char letter1Num6;
				letter1 = letter1Num6;
				
				break;
				
			case 'P':
			case 'Q':
			case 'R':
			case 'S':
			case 'p':
			case 'q':
			case 'r':
			case 's':
				char letter1Num7;
				letter1 = letter1Num7;
				
				break;
				
			case 'T':
			case 'U':
			case 'V':
			case 't':
			case 'u':
			case 'v':
				char letter1Num8;
				letter1 = letter1Num8;
				
				break;
				
			case 'W':
			case 'X':
			case 'Y':
			case 'Z':
			case 'w':
			case 'x':
			case 'y':
			case 'z':
				char letter1Num9;
				letter1 = letter1Num9;
				
				break;
			}	
		}
		else 
			outputMessage = outputMessage + "Invaild Input";
		 outputWord = Character.Digit(letter1, 10);
		JOptionPane.showMessageDialog(null, outputWord);
				
				
		}
	System.exit(0);
	}
		 
}
matt1117
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
how to get the different letters from an inputted word to output as a number.


Do you have an algorithm for converting the letters to a number?
Can you give some examples?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

That's what's in the switch code. The program looks at the letter, if it's an a,b, or c then it outputs it as a 2. and so on.

matt1117
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Then please explain what your problem is.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I don't see you definitions for; letter1, letter2, etc.
Instead of having separate variables for each letter,
Use an array.
Then, loop through the array of letters checking each one.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

You need to actually initiate a number for each case and create a shared/global phone number string to add this number (as a string) to. Then finally output it.

e.g. [case1 = 5, phonenumber = phonenumber & case1], [case2 = 10, phonenumber = phonenumber & case2].

That's not the exact syntax, but the logic I would use.

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: