Reputation: 31
im having trouble in my program. the instruction is to convert the letters that the user inputed and output its corresponding number equivalent in a phone keypad. and i keep having the same error: incompatible types
this is what i have so far:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class phone extends JFrame implements ActionListener
{
JLabel num = new JLabel("Telephone Number(phones Format): ");
JButton btn1 = new JButton(" Convert ");
JLabel tel = new JLabel("Number Format: ");
JTextField txt1 = new JTextField(10);
JTextField txt2 = new JTextField(10);
Container c = getContentPane();
public phone()
{
super("Telephone");
c.setLayout(new FlowLayout());
c.add(num);
c.add(txt1);
c.add(btn1);
c.add(tel);
c.add(txt2);
btn1.addActionListener(this);
this.setSize(160,260);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setResizable(false);
}
public void actionPerformed(ActionEvent a)
{
String txt=txt1.getText();
int x=0;
String [] phones = new String[8];
for( int i = 0; i<=8;i++)
{
if (phones=="A" || phones=="B" || phones== "C")
{
x=2;
}
else if(phones=="D" || phones=="E" || phones== "F")
{
x=3;
}
else if (phones=="G" || phones=="H" || phones== "I")
{
x=4;
}
else if (phones=="J" || phones=="K" || phones== "L")
{
x=5;
}
else if (phones=="M" || phones=="N" || phones== "O")
{
x=6;
}
else if (phones=="P" || phones=="Q" || phones== "R" || phones== "S")
{
x=7;
}
else if (phones=="T" || phones=="U" || phones== "V")
{
x=8;
}
else if (phones=="W" || phones=="X" || phones== "Y" || phones== "Z")
{
x=9;
}
}
txt2.setText(x);
}
public static void main(String args[])
{
phone M = new phone();
}
}
update:
i manage to do some changes but my problem now is the formatting part.
the format should be: ###-####
here is the update:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class phone extends JFrame implements ActionListener
{
JLabel num = new JLabel("Telephone Number(txt Format): ");
JButton btn1 = new JButton(" Convert ");
JLabel tel = new JLabel("Number Format: ");
JTextField txt1 = new JTextField(10);
JTextField txt2 = new JTextField(10);
Container c = getContentPane();
public phone()
{
super("Telephone");
c.setLayout(new FlowLayout());
c.add(num);
c.add(txt1);
c.add(btn1);
c.add(tel);
c.add(txt2);
btn1.addActionListener(this);
this.setSize(160,260);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setResizable(false);
}
public void actionPerformed(ActionEvent a)
{
String pattern="###-####";
DecimalFormat df=new DecimalFormat(pattern);
String txt= txt1.getText();
char[] data = txt.toCharArray();
for (int i=0; i<data.length; ++i) {
char c = data[i];
c = Character.toLowerCase(c);
switch (c) {
case 'a':
case 'b':
case 'c': data[i] = '2';
break;
case 'd':
case 'e':
case 'f': data[i] = '3';
break;
case 'g':
case 'h':
case 'i': data[i] = '4';
break;
case 'j':
case 'k':
case 'l': data[i] = '5';
break;
case 'm':
case 'n':
case 'o': data[i] = '6';
break;
case 'p':
case 'q':
case 'r':
case 's': data[i] = '7';
break;
case 't':
case 'u':
case 'v': data[i] = '8';
break;
case 'w':
case 'x':
case 'y':
case 'z': data[i] = '9';
}
}
String l=df.format(String.valueOf(data));
txt2.setText(l);
}
public static void main(String args[])
{
phone M = new phone();
}
}
Upvotes: 1
Views: 746
Reputation: 47259
phones
is a String array (String []
), not a String
. You are trying to compare it to a String, and that is why you are getting the incompatible types
error
A few other notes/problems:
phone
class should be named Phone
. "Class names should be nouns, in mixed case with the first letter of each internal word capitalized"phones=="A"
. String comparison does not work like this. See this StackOverflow question for plenty more details about why this does not work and how shyou should compare stringstxt2.setText(x);
will give you another error because x
is an int
, and the method takes a String
as inputthis.setDefaultCloseOperation(3);
- You should use 1 of the constants from WindowConstants
rather than than the hardcoded 3
. This would translate into this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Upvotes: 2