I\'m having trouble with this program I attempted at it myself but get an Except
ID: 3851158 • Letter: I
Question
I'm having trouble with this program I attempted at it myself but get an Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. Modify exercise 7-2 on comparing strings so it is now done in an applet instead of an application. The prompts will now be in labels and the input will now be textfields. The user will hit enter in the second textfield to get the results in two other answer labels. The results (which is greater and the difference amount) will be in these two other answer labels. Declare everything at the top, use the init to add the components to your container (use background and foreground colors and the flow layout), and use actionPerformed to do three things: get the text out of the textfields for the two strings (instead of readLines), to perform the nested if else with the comparisons, and the setting of the answers in the two answer labels - which is greater and the difference (instead of printlns). Attach the . java file and submit.
ex7-2
Here is my attempted:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ex72 extends JApplet implements ActionListener
{
String s1, s2;
JLabel stringinput = new JLabel("Enter two strings and click the button ");
Font insfont = new Font("Courier", Font.ITALIC, 18);
JLabel string1lab = new JLabel("String #1: ");
JTextField string1field = new JTextField(7);
JLabel string2lab = new JLabel("String #2: ");
JTextField string2field = new JTextField(7);
JLabel anslab = new JLabel("Result of Strings");
JTextField ansfield = new JTextField(7);
JButton compare = new JButton("Compare");
Container c;
FlowLayout flow = new FlowLayout();
public void init()
{
setSize(390,100);
c = getContentPane();
c.setLayout(flow);
c.setFont(insfont);
c.setBackground(Color.cyan);
stringinput.setForeground(Color.blue);
c.add(stringinput);
string1lab.setForeground(Color.blue);
c.add(string1lab);
string1field.setForeground(Color.blue);
c.add(string1field);
string2lab.setForeground(Color.blue);
c.add(string2lab);
string2field.setForeground(Color.blue);
c.add(string2field);
anslab.setForeground(Color.blue);
c.add(anslab);
ansfield.setForeground(Color.blue);
ansfield.setEditable(false);
c.add(ansfield);
compare.setForeground(Color.blue);
c.add(compare);
string1field.requestFocus();
string2field.addActionListener(this);
compare.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
s1 = string1field.getText();
if (s1.equals(s2)) // (s1 == s2)
ansfield.setText("Same string");
else if (s1.equalsIgnoreCase(s2))
ansfield.setText("Same string - different case");
else if (s1.compareTo(s2) > 0) // s1 > s2
ansfield.setText(s1 + " is greater than " + s2);
else // s1 < s2
ansfield.setText(s1 + " is less than " + s2);
ansfield.setText("Difference is " + s1.compareTo(s2));
}
}
Explanation / Answer
Find the program below.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ex72 extends JApplet implements ActionListener
{
String s1, s2;
JLabel stringinput = new JLabel("Enter two strings and click the button ");
Font insfont = new Font("Courier", Font.ITALIC, 18);
JLabel string1lab = new JLabel("String #1: ");
JTextField string1field = new JTextField(7);
JLabel string2lab = new JLabel("String #2: ");
JTextField string2field = new JTextField(7);
JLabel anslab = new JLabel("Result of Strings");
JTextField ansfield = new JTextField(7);
JButton compare = new JButton("Compare");
Container c;
FlowLayout flow = new FlowLayout();
public void init()
{
setSize(390,100);
c = getContentPane();
c.setLayout(flow);
c.setFont(insfont);
c.setBackground(Color.cyan);
stringinput.setForeground(Color.blue);
c.add(stringinput);
string1lab.setForeground(Color.blue);
c.add(string1lab);
string1field.setForeground(Color.blue);
c.add(string1field);
string2lab.setForeground(Color.blue);
c.add(string2lab);
string2field.setForeground(Color.blue);
c.add(string2field);
anslab.setForeground(Color.blue);
c.add(anslab);
ansfield.setForeground(Color.blue);
ansfield.setEditable(false);
c.add(ansfield);
compare.setForeground(Color.blue);
c.add(compare);
string1field.requestFocus();
string2field.addActionListener(this);
compare.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
s1 = string1field.getText();
if (s1.equals(s2)) // (s1 == s2)
ansfield.setText("Same string");
else if (s1.equalsIgnoreCase(s2))
ansfield.setText("Same string - different case");
else if (s1.compareTo(s2) > 0) // s1 > s2
ansfield.setText(s1 + " is greater than " + s2);
else // s1 < s2
ansfield.setText(s1 + " is less than " + s2);
ansfield.setText("Difference is " + s1.compareTo(s2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.