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:
Code Java:
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:
Code Java:
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. :/
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:
Code java:
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())
}
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
What if you tried this instead:
or, if you prefer
Code :
this.number = this.number + "#";
Hope this helps.
Re: converting words to numbers like a phone. i.e. 1800CALLMEE = 1-800-225-5544
Quote:
Originally Posted by
mystikaljester
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
What if you tried this instead:
or, if you prefer
Code :
this.number = this.number + "#";
Hope this helps.
That worked! Yay! Thank you so much! You're the bomb! :D
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.