So I have this palindrome program that I am working on and am having some difficulty related to passing the string into my isPalindrome method. This is also my second attempt at creating multiple classes so if there are other major glaring issues I need to keep in mind let me know too. I am just starting out with java and have done this through Main of course and JOptionPane, but not through a JTextField via a frame and pane. I know I can use
var_name.getText(), but then what to do with it is confusing me. With that being said this is what I have:
//MainQ3.java
import javax.swing.JFrame;
import javax.swing.*;
import java.util.*;
import java.awt.FlowLayout;
public class MainQ3
{
private PalindromeFrame frame;
public MainQ3()
{
frame = new PalindromeFrame();
frame.setTitle("Palindrome Checker");//FRAME TITLE
frame.setLocation(250,250);//LOCATION OF FRAME
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//CLOSE OPERATION
frame.setSize(650, 80);//FRAME LxW
frame.setVisible(true);//FRAME IS VISIBLE
}
public static void main (String [] args)
{
MainQ3 app = new MainQ3();
}
}
From there I have:
//PalindromeFrame.java
import javax.swing.JFrame;
import javax.swing.*;
import java.util.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JLabel;
public class PalindromeFrame extends JFrame
{
private JButton btnCheck = new JButton ("Is it a Palindrome"); //BUTTON
private JLabel lblCheck = new JLabel("No Results Yet", 10);//LABEL
private JTextField txtPhrase = new JTextField(20);//TEXT FIELD
private JLabel lblPhrase = new JLabel("Enter a Phrase");
private JPanel panel = new JPanel();
String p;
public PalindromeFrame()
{
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(panel);//ADDS PANEL TO FRAME
panel.add(lblPhrase);
panel.add(txtPhrase);
panel.add(btnCheck);
panel.add(lblCheck);
btnCheck.addActionListener(new CheckListener() );//LOOKS FOR BUTTON PRESS
}
//INNER CLASSES
class CheckListener implements ActionListener//ACTION EVENT FOR DEAL BUTTON
{
public void actionPerformed(ActionEvent e)
{
CheckPalindrome();
}
}
private void CheckPalindrome()
{
System.out.println(" in check palindrome");
System.out.println(txtPhrase.getText());
//if(isPalindrome() == true)
//{
//lblCheck.setText("Yes it is a Palindrome");
//}
}
}
And finally:
//Palindrome.java
import javax.swing.*;
import java.util.Scanner;
public class Palindrome
{
boolean isPalindrome = true;
String phrase = new Scanner(System.in).nextLine();
String eval = phrase.toLowerCase();
int first, last;
public Palindrome()//CONSTRUCTOR
{
}
public void setPhrase(String p)
{
phrase = p;
}
public boolean isPalindrome()//CHECKS FOR PALINDROME
{
for(first = 0, last = eval.length()-1; first<last; first++, last--)
{
while( (int)eval.charAt(first)< 'a' || (int)eval.charAt(first) > 'z') first++;
while( (int)eval.charAt(last)< 'a' || (int)eval.charAt(last) > 'z') last--;
if(first>last || eval.charAt(first) != eval.charAt(last) ) isPalindrome = false;
}
return isPalindrome;
}
}
I am trying to "set" the phrase and then check it in isPalindrome, and then send isPalindrome to checkPalindrome and display either yes it is or no it isn't. It compiles fine so there is no issue there but lack of sleep/training has me doing dumb stuff. So any assistance would be appreciated.