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. :/