0

i made javafx app that show the gcd of two integer number to do that i have to make a loop , i used while loop but i have a problem i cant show the result from that loop the error i get is "Cannot resolve symbol t6" and "Cannot resolve symbol t4" here is the code

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    int q, r1, r2, r3, r4, r;


    private Main main;
    private Stage primaryStage;


    public void setMain(Main main, Stage primaryStage) {
        this.main = main;
        this.primaryStage = primaryStage;
    }

    @FXML
    private Label label;
    @FXML
    private TextFlow tf;
    @FXML
    private TextArea ta;
    @FXML
    private Text t;
    @FXML
    private TextField field;
    @FXML
    private TextField field1;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        String t1 = field.getText();
        String t2 = field1.getText();
        r1 = Integer.parseInt(t1);
        r2 = Integer.parseInt(t2);
        if (r1 < r2) {

            r3 = r2;
            r4 = r1;
            r = r3 % r4;
            q = r3 / r4;
            String t = ("r1=" + r3 + "     " + "r2=" + r4 + "       " + "q=" + q + "      " + "r=" + r);
            while (r != 0) {


                r3 = r4;
                r4 = r;
                r = r3 % r4;
                q = r3 / r4;
                String t6 = "r1=" + r3 + "     " + "r2=" + r4 + "       " + "q=" + q + "      " + "r=" + r;

            }
            String t7 = ("gcd=" + r4);
            label.setText(t+t6+t7);
        } else {
            r = r1 % r2;
            q = r1 / r2;
            String t3 = ("r1=" + r1 + "     " + "r2=" + r2 + "       " + "q=" + q + "      " + "r=" + r);
            while (r != 0) {


                r1 = r2;
                r2 = r;
                r = r1 % r2;
                q = r1 / r2;
                String t4 = ("r1=" + r1 + "     " + "r2=" + r2 + "       " + "q=" + q + "      " + "r=" + r);

            }
            String t5 = ("gcd=" + r2);
            label.setText(t3+t4+t5);




        }
        field.clear();
        field1.clear();
    }






    public void close(){
        primaryStage.close();
    }
    @Override
    public void initialize(URL url, ResourceBundle rb){}
    public void changeWindow(){
         main.secondWindow();
    }

}
5
  • Check the scope of the variables. (It's just the body of the loop in both cases.) Commented Dec 1, 2016 at 20:40
  • @fabian if i do that the string will = null and thats not what i want Commented Dec 1, 2016 at 20:42
  • You simply cannot reference a variable after it's scope is left in java. There is no way around this whether this appeals to you or not. I don't really get why you only want to use the information from the last loop though, but there in no way to use a variable declared in the loop body outside of said loop body. Commented Dec 1, 2016 at 20:53
  • If you have a lot of local variables you could declare them all at once at the beginning of your method, like you are doing with the class int variables. That way you avoid these problems. Commented Dec 1, 2016 at 21:00
  • thank you very much u both helped me fabian @n247s Commented Dec 1, 2016 at 21:03

1 Answer 1

1

The scope of String t4 is only the while loop, just declare it out the loop and it will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.