Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:46:02+00:00 2026-06-14T21:46:02+00:00

I am trying to create a greatest common divisor calculator. I had to create

  • 0

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;
 }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:46:03+00:00Added an answer on June 14, 2026 at 9:46 pm

    use this:

    int x =  Integer.parseInt(xTextField.getText());
    

    Because xTextField.getText() will return a String and you can’t assign a String to an int

    So have to parse the String to its equivalent int value using Integer.parseInt that takes a String value and parses it to int.

    Same is the case for int y = yTextField.getText();

    Also if you want the gdc calculated to bet set in the gdc text field then you should use :

    int gcd = greatestCommonDivisor(x, y);
    gcdTextField.setText(String.valueOf(gcd));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a background-image slideshow and am getting this error... This is the
I'm trying create a log specifically for logging user account activity. I created this
I'm trying create a gui using Tkinter that grabs a username and password and
I'm trying create this function such that if any key besides any of the
I am trying create a table and add this table into tablecell (table inside
I'm trying create a small http proxy service. This is not working so well.
I trying create empty white image with php, this s my code $bg =
I'm trying create an executable for Windows for a GUI application in tkinter using
I am trying create my custom Style spinner with help of this site. But
I'm trying create simple application in C++. This application has to read from file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.