I'm making a game where the user enters his/her first name and last name. The program extracts the first letters from the names, then outputs names from a string array.
I'm thinking the issue is in the last part of the code, where I'm comparing the Strings firstletter and lastLetter to the array. But I'm not sure. I have spent some time researching, and I'm stuck.
Any comments welcome. You won't hurt my feelings.
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public abstract class ChristmasName extends JFrame implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("What is your Christmas Name?");
frame.setVisible(true);
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JLabel firstName = new JLabel("First name:");
JTextField first = new JTextField(15);
JLabel lastName = new JLabel("Last name:");
JTextField last = new JTextField(15);
panel.add(firstName);
panel.add(first);
panel.add(lastName);
panel.add(last);
JButton submit = new JButton("Submit");
panel.add(submit);
submit.addActionListener(new ActionListener() {
@Override
public actionPerformed(ActionEvent e) {
String[] first_name = {"Apple","Eggnogg","Candy","Jingle","Holly","Goldie","Ho Ho","Frosty","Joyous","Mittens","Tinsel","Turkey","Tiny","Cranberry","Bloated","Angel","Bauble","Bulb","Ginger","Blitzen","Eve","Faith","Fruitcake","Goose","Glitter","Grinch"};
String[] last_name = {"Tidings","Swan","Jolly","Claus","Mistletoe","Punch","Chimney","Coal","Igloo","Jumper","Myrhh","Pudding","Reindeer","Rejoice","Icicle","Midnight","Shepherd","Surprise","Gift","Magi","Train","Tree","White","Donkey","Wreath","Stuffing"};
String firstLetter = first.getText();
firstLetter = firstLetter.substring(0,1);
String lastLetter = last.getText();
lastLetter = lastLetter.substring(0,1);
if (firstLetter == "A") {
firstLetter = first_name[0];
}
JOptionPane.showMessageDialog(null, firstLetter + " " + lastLetter);
System.exit(0);
}
});
}
}