Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: converting words to numbers like a phone. i.e. 1800CALLMEE = 1-800-225-5544

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default converting words to numbers like a phone. i.e. 1800CALLMEE = 1-800-225-5544

    Please Help! I can't get this program to run correctly...
    I have 2 questions so far:
    1.) How do i get my program to print each number instead of just the very last number? I was thinking maybe it had something to do with the break;'s. this is the first time i've ever used a switch statement.
    2.) Why do I keep getting a "NullPointerException" error? it says "java.lang.NullPointerException
    at PhoneClientDialog.main(PhoneClientDialog.java:10)"

    Here's my student class code:
    public class Phone
    {
        private String word;
        private String number;
     
        public Phone(String word)
        {
            this.word = word;
            this.setNumber();
        }
     
        private void setNumber()
        {
           //Assign number to empty string to avoid null. 
           this.number = ("");
     
           //Convert characters in word to digits.
           int index = 0;
     
           while (index < this.word.length())
           {   
                   //Places digits in number.
                   switch(this.word.toLowerCase().charAt(index))
                   {
                           //#2
                           case 'a': this.number = "2"; break;
                           case 'b': this.number = "2"; break;
                           case 'c': this.number = "2"; break;
                           //#3
                           case 'd': this.number = "3"; break;
                           case 'e': this.number = "3"; break;
                           case 'f': this.number = "3"; break; 
                           //#4
                           case 'g': this.number = "4"; break;
                           case 'h': this.number = "4"; break;
                           case 'i': this.number = "4"; break;
                           //#5
                           case 'j': this.number = "5"; break;
                           case 'k': this.number = "5"; break;
                           case 'l': this.number = "5"; break;
                           //#6
                           case 'm': this.number = "6"; break;
                           case 'n': this.number = "6"; break;
                           case 'o': this.number = "6"; break;
                           //#7
                           case 'p': this.number = "7"; break;
                           case 'r': this.number = "7"; break;
                           case 's': this.number = "7"; break;
                           //#8
                           case 't': this.number = "8"; break;
                           case 'u': this.number = "8"; break;
                           case 'v': this.number = "8"; break;
                           //#9
                           case 'w': this.number = "9"; break;
                           case 'x': this.number = "9"; break;
                           case 'y': this.number = "9"; break;
                   }
             index++;
           }        
        }
     
        //public method that returns number.
        public String toString()
        {
            return this.number;
        }
     
     
    }

    and here is my parent class code:

    import javax.swing.JOptionPane;
     
    public class PhoneClientDialog
    {
       public static void main(String[] args)
       {
          String input = JOptionPane.showInputDialog(
             "Complete the phone number with a word (quit terminates program): 1-800-");
     
    	  while(!(input.equalsIgnoreCase("quit")))
    	  {
    		  Phone myPhone = new Phone(input);
    		  input = JOptionPane.showInputDialog("The actual phone number is 1-800-" + myPhone + '\n' +
             	"Complete the phone number with a word (quit terminates program): 1-800-");
    	  }
     
          System.exit(0);
       }
    }

    the parent class is the one throwing codes but my professor wrote it and we're not allowed to touch it. i would e-mail him but it's the weekend. :/
    Last edited by helloworld922; November 13th, 2010 at 12:31 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: converting words to numbers like a phone. i.e. 1800CALLMEE = 1-800-225-5544

    Words to numbers.

    Well, entering anything in JOptionPane will be taken as a String.

    To get it to ints,

    int x = Integer.parseInt(StringFromJOptionPane);

    I think I have an idea.

    For every case, call the toString().

    Right now, it'll just do it for the last number due to your while loop.

    If you want it every time:

    while (index < this.word.length())
           {  
                   //Places digits in number.
                   switch(this.word.toLowerCase().charAt(index))
                   {
                           //#2
                           case 'a': this.number = "2"; 
    this.toString();
    break;
                           case 'b': this.number = "2"; 
    this.toString();
    break;
    // you get the idea
     
    // if you want to print out to, just call prinltln(toString())
     
    }

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    Laken (November 14th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: converting words to numbers like a phone. i.e. 1800CALLMEE = 1-800-225-5544

    Okay, I'll take a crack at the first part of your question.

    If it seems to be only displaying the very last character, I think that would be because each time you run through your for loop, you are over-writing the previous entry by saying
    this.number = "#";

    What if you tried this instead:
    this.number += "#";
    or, if you prefer
    this.number = this.number + "#";

    Hope this helps.

  5. The Following 2 Users Say Thank You to mystikaljester For This Useful Post:

    javapenguin (November 14th, 2010), Laken (November 14th, 2010)

  6. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: converting words to numbers like a phone. i.e. 1800CALLMEE = 1-800-225-5544

    Quote Originally Posted by mystikaljester View Post
    Okay, I'll take a crack at the first part of your question.

    If it seems to be only displaying the very last character, I think that would be because each time you run through your for loop, you are over-writing the previous entry by saying
    this.number = "#";

    What if you tried this instead:
    this.number += "#";
    or, if you prefer
    this.number = this.number + "#";

    Hope this helps.
    That worked! Yay! Thank you so much! You're the bomb!

  7. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: converting words to numbers like a phone. i.e. 1800CALLMEE = 1-800-225-5544

    My evaluate method in OperatorNode is having a problem. Data is being updated. Sorry for losing my patience, but I do have a rapidly approaching deadline.

Similar Threads

  1. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  2. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM
  3. Help: Num to words
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2010, 06:55 PM
  4. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 04:28 AM
  5. Adding Marathi words to MySQL table
    By vaishali in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 8th, 2009, 06:43 AM

Tags for this Thread