I am trying to create a greatest common divisor calculator. I had to create a GUI Interface for this project, and I think I did so correctly, however I can't test it because I am having an issue with my calculating.
Specifically I am getting an error in the compiler here with these two lines:
int x = xTextField.getText();
int y = yTextField.getText();
The error is "Type mistmatch, can't convert from String to int"
I've tried inserting int x = 0; and int x = 0; before it with no luck. I have also tried changing these from ints to Strings however it messes everything up more. I am not sure how to get the text what what I should be using.
Here it the calculation part of my code:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == calculateButton) {
int x = xTextField.getText();
int y = yTextField.getText();
int gcd = greatestCommonDivisor(
x, y);
gcdTextField.getText();
}
}
private int greatestCommonDivisor(int x, int y) {
// TODO Auto-generated method stub
while (x != y) {
if (x > y) {
x = x - y;
} else {
y = y - x;
}
}
return y;
}
}
Here is my entire code:
package chapt15;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class FutureValueApp {
public static void main(String[] args) {
JFrame frame = new GcdFrame();
frame.setVisible(true);
}
}
class GcdFrame extends JFrame {
public GcdFrame() {
setTitle("Greatest Common Divisor Finder");
centerWindow(this);
setSize(267, 200);
// setResizable(false);
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new GcdPanel();
this.add(panel);
}
private void centerWindow(Window w) {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width - w.getWidth()) / 2,
(d.height - w.getHeight()) / 2);
}
}
class GcdPanel extends JPanel implements ActionListener {
private JTextField xTextField, yTextField,
gcdTextField;
private JLabel xLabel, yLabel, gcdLabel;
private JButton calculateButton, exitButton;
public GcdPanel() {
// display panel
JPanel displayPanel = new JPanel();
// displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
displayPanel.setLayout(new GridLayout(4, 2));
// payment label
xLabel = new JLabel("X:");
displayPanel.add(xLabel);
// payment text field
xTextField = new JTextField(10);
displayPanel.add(xTextField);
// rate label
yLabel = new JLabel("Y:");
displayPanel.add(yLabel);
// rate text field
yTextField = new JTextField(10);
displayPanel.add(yTextField);
// future value label
gcdLabel = new JLabel("GCD:");
displayPanel.add(gcdLabel);
// future value text field
gcdTextField = new JTextField(10);
gcdTextField.setEditable(false);
gcdTextField.setFocusable(false);
displayPanel.add(gcdTextField);
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
buttonPanel.add(calculateButton);
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == calculateButton) {
int x = xTextField.getText();
int y = yTextField.getText();
int gcd = greatestCommonDivisor(
x, y);
gcdTextField.getText();
}
}
private int greatestCommonDivisor(int x, int y) {
// TODO Auto-generated method stub
while (x != y) {
if (x > y) {
x = x - y;
} else {
y = y - x;
}
}
return y;
}
}