14 Replies - 6715 Views - Last Post: 22 April 2011 - 10:55 PM Rate Topic: -----

#1 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-February 11

How to Convert Letters Into Phone Number

Posted 22 April 2011 - 01:37 PM

I am not sure what I am doing wrong. I have an idea that I need to find a way to pull the numbers/characters from the main method into the getNumber section to be able to get an output. However, I do not understand how to do that. I also get this error when trying to compile it.

java:33 cannot find symbol
symbol: variable letter
in the line with System.out.println(getNumber(letter.charAt(0)));

If I remove the main method, the code will compile without errors. How can I get the main method to pull from the getNumbers and give me a result?


import java.util.*;
public class telephone2  {
     


public static int getNumber(char uppercaseLetter)     { 
        
   
       if (letter=='A' || letter=='B' || letter== 'C')          {
             return 1; 
         }
        else if(letter=='D' || letter=='E' || letter== 'F')          {
             return 2; 
         }  
        else if (letter=='G' || letter=='H' || letter== 'I')          {
             return 3;   
         }
        else if (letter=='J' || letter=='K' || letter== 'L')          {
             return 4;
         }
        else if (letter=='M' || letter=='N' || letter== 'O')          {
             return 5;
         }
        else if (letter=='P' || letter=='Q' || letter== 'R' || letter== 'S')          {
             return 6;
         }
        else if (letter=='T' || letter=='U' || letter== 'V')          {
             return 7;
         }  
        else if (letter=='W' || letter=='X' || letter== 'Y' || letter== 'Z')          {
             return 9;   
         }
               

      public static void main (String[] args)     { 
	Scanner input = new Scanner(System.in);
	System.out.print("Enter alphanumeric phone number to convert to all numbers.");

        String phoneNumber = input.nextLine();
        System.out.println(getNumber(letter.charAt(0)));     

   }
}  


(I have searched and looked through some of the previous questions about this specific program, but was unable to find an answer to my problem.) Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to Convert Letters Into Phone Number

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,103
  • Joined: 27-December 08

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:00 PM

You don't declare a variable named letter. Look here; your String is named phoneNumber, not letter.
String phoneNumber = input.nextLine();  


Was This Post Helpful? 1
  • +
  • -

#3 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:07 PM

There was no variable called letter as macosxnerd101 pointed out, also you were not returning the integer value for your getNumber method correctly. I edited your code a bit, this worked fine:

import java.util.*;
public class Main {


public static int getNumber(char uppercaseLetter)     {

       int ret = 0;
       if (uppercaseLetter=='A' || uppercaseLetter=='B' || uppercaseLetter== 'C')          {
             ret = 1;
         
         }
        else if(uppercaseLetter=='D' || uppercaseLetter=='E' || uppercaseLetter== 'F')          {
             ret = 2;
           
         }
        else if (uppercaseLetter=='G' || uppercaseLetter=='H' || uppercaseLetter== 'I')          {
             ret = 3;
           
         }
        else if (uppercaseLetter=='J' || uppercaseLetter=='K' || uppercaseLetter== 'L')          {
             ret = 4;
           
         }
        else if (uppercaseLetter=='M' || uppercaseLetter=='N' || uppercaseLetter== 'O')          {
             ret = 5;
        
         }
        else if (uppercaseLetter=='P' || uppercaseLetter=='Q' || uppercaseLetter== 'R' || uppercaseLetter== 'S')          {
             ret = 6;
            
         }
        else if (uppercaseLetter=='T' || uppercaseLetter=='U' || uppercaseLetter== 'V')          {
             ret = 7;
         
         }
        else if (uppercaseLetter=='W' || uppercaseLetter=='X' || uppercaseLetter== 'Y' || uppercaseLetter== 'Z')          {
             ret = 9;
           
         }
       return ret;
    }
      public static void main (String[] args) {
	Scanner input = new Scanner(System.in);
	System.out.print("Enter alphanumeric phone number to convert to all numbers.");

        String phoneNumber = input.nextLine();
        for (int i = 0; i < phoneNumber.length(); i++) {
       System.out.print(getNumber(phoneNumber.toUpperCase().charAt(i)));
          }
   }
}


I hope this helps :)

This post has been edited by v0rtex: 22 April 2011 - 02:09 PM

Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,103
  • Joined: 27-December 08

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:08 PM

@v0rtex: Seriously, let's not take ALL the fun out of debugging by fixing the code. :)
Was This Post Helpful? 0
  • +
  • -

#5 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:11 PM

lol macosxnerd101 :/
Sorry, I will take this in mind for the future :P
Was This Post Helpful? 0
  • +
  • -

#6 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-February 11

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:13 PM

Thank You Both! I found the other error after macosxnerd pointed out how simple the first one was. :)

After fixing that part, I ended up with a lot more errors and managed to fix the majority of them. The one I am stuck on now is that it shows it is missing a return on line 34.


Here is the code a few lines before and a few lines after. I have a return for the above portion, unless it needs an additional return in case a 1 or 0 is input.

        else if (uppercaseLetter=='W' || uppercaseLetter=='X' || uppercaseLetter== 'Y' || uppercaseLetter== 'Z')          {
             return 9;   
         }
               
}
  
Line 34 is the last line on this peice of code.

This post has been edited by MoonBeam080680: 22 April 2011 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#7 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:16 PM

I suggest returning the way I did, look @ the code I entered, might help. You still are not returning the value in the scope of the function's opening parentheses.
Was This Post Helpful? 2
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,103
  • Joined: 27-December 08

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:31 PM

All of your conditionals are if/else if statements. So if you don't return something after them, or even inside an else statement, then you have a method that may not return anything, when you are defining it to return something.
Was This Post Helpful? 2
  • +
  • -

#9 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-February 11

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 02:54 PM

Please forgive me for asking so many questions. I can write very, very, very basic programs, but I get lost with everything else. I am surprised I got as far as I did before asking for help.

Thank you both very much. The code compiles now and works almost exactly as i need it to. I just have one more question.

I know I can go in and add additional else if statments to cover if someone enters in 1800 at the first so that it stays 1800 instead of ending up all zeros. Is there a simplier way to tell the program to keep the numbers as numbers?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,103
  • Joined: 27-December 08

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 03:04 PM

You could use the Character.isDigit() method in your method. So that if it returns true, then simply return the given char parameter.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 03:05 PM

Much more efficient that way


public static int getNumber(char uppercaseLetter)     { 
        
       char[] letter = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
       int[] value = {1, 1, 1, 2, 2, 2, 3, 3, 3,, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 9, 9, 9,9};

        for(int i = 0; i < letter.length; i++) {
           if(uppercaseLetter == letter[i])
              return value[i];
        }
        return 0;   // <--- shouldn't happen
    }


Was This Post Helpful? 2
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 06:19 PM

I would be tempted to take pbl's method one step further and take advantage that all chars can be treated as ints.


private static final int[] value = {1,1,1, 2,2,2, 3,3,3, 4,4,4, 5,5,5, 6,6,6,6, 7,7,7, 9,9,9,9};
public static int getNumber(char uppercaseLetter)     { 
  if (uppercaseLetter < 'A') return 0;
  if (uppercaseLetter > 'Z') return 0;
  return value[uppercaseLetter - 'A'];
}

Was This Post Helpful? 1
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 06:28 PM

While I'm at it, I'm surprised nobody has mentioned the switch statement:

public static int getNumber(int uppercaseLetter) {
  switch (uppercaseLetter) {
    case 'A': case 'B': case 'C':           return 1;
    case 'D': case 'E': case 'F':           return 2;
    case 'G': case 'H': case 'I':           return 3;
    case 'J': case 'K': case 'L':           return 4;
    case 'M': case 'N': case 'O':           return 5;
    case 'P': case 'Q': case 'R': case 'S': return 6;
    case 'T': case 'U': case 'V':           return 7;
    case 'W': case 'X': case 'Y': case 'Z': return 9;
    default: return 0;
  }
}

This post has been edited by cfoley: 22 April 2011 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 07:33 PM

this is the first "horrible" suggested way
switch() is just a if() else if() in disguise :)
Was This Post Helpful? 0
  • +
  • -

#15 qxc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-April 11

Re: How to Convert Letters Into Phone Number

Posted 22 April 2011 - 10:55 PM

lol! I was doing a similiar project, these posts helped me alot :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1