1

Goal: to take the user input and output a semi randomized workout.

Issue: I've gotten the rep workout to output, the loops and everything that randomize the actual workouts can't be seen by the action handler. I can get the actual workouts to output in the console line, but not in the GUI.

I've tried just about everything from redoing the code, to moving the code around. I just need the val variable visible to the actionhandler for when the user clicks the randomize button.

I have separated the code out so that it is easier to read. The first part is the GUI and the selection stuff. The second set of code is what does the randomizing in, for example, chestDay. The code also seems to get very touchy when I move the second code snippet around.

I could really use any and all advice, I have a few days remaining to fix this problem.

The variable that I need to be seen in both snippets is the 'val' variable.

package Final;


import java.util.ArrayList;
import java.util.List;
import javafx.application.Application; 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.stage.Stage; 
import javafx.scene.text.Text;

public class FinalGUI extends Application { 
    private static final int range = 6;
    // launch the application 
    public void start(Stage stage) { 

        try { 

            // set title for the stage 
            stage.setTitle("Your Work Out Friend"); 

            // create a VBox 
            VBox vbox = new VBox(); 
            Text text = new Text();

            ComboBox choiceMuscle = new ComboBox();
            ComboBox choiceRep = new ComboBox();



            // create a label 
            Label label = new Label("Choose your muscle group and your workout goal."); 

            // Creates a list that the vbox can use
            ObservableList<String> choicesMuscle = FXCollections.observableArrayList(
                "Chest day", "Back day", "Leg day");
            choiceMuscle.setItems(choicesMuscle);


            ObservableList<String> choicesRep = FXCollections.observableArrayList(
                "Lose weight", "Gain muscle mass", "Gain Strength");
            choiceRep.setItems(choicesRep);
            // add label to vbox 
            vbox.getChildren().add(label); 

            // add buttons to VBox 

            vbox.getChildren().addAll(choiceMuscle, choiceRep, text);

            Button btnOK = new Button();
            btnOK.setText("Give me a workout!");
            vbox.getChildren().add(btnOK);

            btnOK.setOnAction(a -> {



                String cMuscle = choiceMuscle.getSelectionModel().getSelectedItem() + "";



                if (cMuscle.equalsIgnoreCase("Chest Day")) {
                    printArray(chestDay());
                } 
                else if (cMuscle.equalsIgnoreCase("Leg Day")) {
                    printArray(legDay());
                } 
                else if (cMuscle.equalsIgnoreCase("Back Day")) {
                    printArray(backDay());
                } 
                //else {
                  //  System.out.println("Invalid choice made. Choose between Chest, Leg or Back days only.");
                //}
                //});


                String cRep = choiceRep.getSelectionModel().getSelectedItem() + "";
                String repChoice = "";


                //Move code from WorkOutRep here
                if (cRep.equalsIgnoreCase("Lose Weight")) {

                    repChoice = "5 sets of 20 reps, with 1 minute of cardio in between";
                } 
                else if (cRep.equalsIgnoreCase("Gain muscle mass")) {

                    repChoice = "4 sets of 10 reps, or 5 sets of 8 reps";
                } 
                else if (cRep.equalsIgnoreCase("Gain strength")) {

                    repChoice = "5 sets of 3 reps, increasing the weight until you can no longer lift the weight.";
                } 
                //else {
                  //  System.out.println("Invalid choice made. Choose between the listed choices only.");
                //}
            //});



                text.setText("Your workout is: " + repChoice + val );

            });

            // create a scene 
            Scene scene = new Scene(vbox, 400, 200); 

            // set the scene 
            stage.setScene(scene); 

            stage.show(); 
        } 

        catch (Exception e) { 

            System.out.println(e.getMessage()); 
        }
    }
    public static String[] backDay() {
        List<String> backList = new ArrayList <>();
        backList.add("Deadlift");
        backList.add("Pull Up");
        backList.add("Back Rows");
        backList.add("Bend-Over Barbell Rows");
        backList.add("Romanian Deadlift");
        backList.add("Front Squat");

        return randomizerHelper(backList);
    }

    public String[] chestDay() {
        List <String> chestList = new ArrayList <>();
        chestList.add("Bench Press");
        chestList.add("Incline Press");
        chestList.add("Dip");
        chestList.add("Flys");
        chestList.add("Reverse Flys");
        chestList.add("Supine Press");

        return randomizerHelper(chestList);
    }


    public String[] legDay() {
        List<String> legList = new ArrayList <>();
        legList.add("Squat");
        legList.add("Leg Press");
        legList.add("Leg Extension");
        legList.add("Dumbbell Step Up");
        legList.add("Body Weight Calf Raises");
        legList.add("Walking Lunge");

        return randomizerHelper(legList);
    }

public static String[] randomizerHelper(List<String> arr) {
        String[] returnArray = new String[3];
        String chosen;
        for(int i = 0; i < returnArray.length; i++) {
            chosen = arr.get((int) (Math.random() * range));

            for(int j = 0; j < returnArray.length; j++) {
                if (!(chosen.equalsIgnoreCase(returnArray[j]))) {
                    returnArray[i]= chosen;
                    break; 
                  }
                }
            }
            return returnArray;
        }

    public void printArray(String[] arr) {
        for (String val : arr) {
            Text text = new Text();

            text.setText(val);

            }
        }


    // Main Method 
    public static void main(String args[]) { 
        // launch the application 
        Application.launch(args);
    }

}


3
  • "I've gotten the rep workout to output, the loops and everything that randomize the actual workouts can't be seen by the action handler." sorry, I can't understand the problem description.
    – c0der
    Dec 14, 2019 at 6:47
  • 1
    This is more or less the same question you posted yesterday and you're still trying to access a loop variable in another method which makes little sense, since during the execution of that method multiple values will be assigned to that variable. Note that your printArray method also makes little sense. You just create Text nodes that are thrown away. I recommend to a) learn about fields or b) use return values according to your needs. In any case explain the problem better: "I want to do X; I try this by doing Y" is better than "I want to do Y"...
    – fabian
    Dec 14, 2019 at 11:52
  • (For the latter the best answer we can provide is "It's not possible to do it this way")
    – fabian
    Dec 14, 2019 at 11:53

1 Answer 1

1

Your printArray() method creates new Text nodes but never adds them to the scene graph. Only the last string is set, and none are seen. Instead, you can create a string containing a randomized list of exercises focused on a particular muscle group; the resulting string can then be added to the exisitng Text. In the variation below,

  • The method randomExercises() invokes Collections.shuffle() to randomize the list and choose a suitable subList(). The button handler can then invoke selectMuscles() and add the relevant muscleChoice to the text.

    muscleChoice = selectMuscles(…);
    repChoice = …;
    text.setText("Your workout: " + "\n" + muscleChoice + "\n" + repChoice);
    
  • The list of exercises for each muscle group should be created just once; as class members, the lists are visible to class methods.

    private final List<String> backList = new ArrayList<>();
    private final List<String> chestList = new ArrayList<>();
    private final List<String> legList = new ArrayList<>();
    …
    private String[] backDay() {
        if (backList.isEmpty()) {
            backList.add("Deadlift");
            …
        }
        return randomExercises(backList);
    }
    …
    
  • In general, class methods should be private unless wider access is specifically required.

  • For safer operation, avoid raw types; note that no cast or implicit conversion is required.

    ComboBox<String> choiceMuscle = new ComboBox<>();
    String cMuscle = choiceMuscle.getSelectionModel().getSelectedItem();
    

image

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.text.Text;

/** @see https://stackoverflow.com/a/59334974/230513 */

public class FinalGUI extends Application {

    private final List<String> backList = new ArrayList<>();
    private final List<String> chestList = new ArrayList<>();
    private final List<String> legList = new ArrayList<>();

    @Override
    public void start(Stage stage) {
        stage.setTitle("Your Work Out Friend");

        Label label = new Label("Choose your muscle group and your workout goal.");

        ComboBox<String> choiceMuscle = new ComboBox<>();
        ObservableList<String> choicesMuscle = FXCollections.observableArrayList(
            "Chest day", "Back day", "Leg day");
        choiceMuscle.setItems(choicesMuscle);
        choiceMuscle.getSelectionModel().select(0);

        ComboBox<String> choiceRep = new ComboBox<>();
        ObservableList<String> choicesRep = FXCollections.observableArrayList(
            "Lose weight", "Gain muscle mass", "Gain Strength");
        choiceRep.setItems(choicesRep);
        choiceRep.getSelectionModel().select(0);

        Button btnOK = new Button("Give me a workout!");

        Text text = new Text("\n\n\n");

        VBox vbox = new VBox(8);
        vbox.setPadding(new Insets(8));
        vbox.getChildren().addAll(label, choiceMuscle, choiceRep, btnOK, text);

        btnOK.setOnAction(a -> {
            String cMuscle = choiceMuscle.getSelectionModel().getSelectedItem();
            String muscleChoice = "";
            if (cMuscle.equalsIgnoreCase("Chest Day")) {
                muscleChoice = selectMuscles(chestDay());
            } else if (cMuscle.equalsIgnoreCase("Leg Day")) {
                muscleChoice = selectMuscles(legDay());
            } else if (cMuscle.equalsIgnoreCase("Back Day")) {
                muscleChoice = selectMuscles(backDay());
            }

            String cRep = choiceRep.getSelectionModel().getSelectedItem();
            String repChoice = "";
            if (cRep.equalsIgnoreCase("Lose Weight")) {
                repChoice = "5 sets of 20 reps, with 1 minute of cardio between";
            } else if (cRep.equalsIgnoreCase("Gain muscle mass")) {
                repChoice = "4 sets of 10 reps, or 5 sets of 8 reps";
            } else if (cRep.equalsIgnoreCase("Gain strength")) {
                repChoice = "5 sets of 3 reps, increasing the weight\n"
                    + "until you can no longer lift the weight.";
            }

            text.setText("Your workout: " + "\n" + muscleChoice + "\n" + repChoice);
        });

        Scene scene = new Scene(vbox);
        stage.setScene(scene);
        stage.show();
    }

    private String[] backDay() {
        if (backList.isEmpty()) {
            backList.add("Deadlift");
            backList.add("Pull Up");
            backList.add("Back Rows");
            backList.add("Bend-Over Barbell Rows");
            backList.add("Romanian Deadlift");
            backList.add("Front Squat");
        }
        return randomExercises(backList);
    }

    private String[] chestDay() {
        if (chestList.isEmpty()) {
            chestList.add("Bench Press");
            chestList.add("Incline Press");
            chestList.add("Dip");
            chestList.add("Flys");
            chestList.add("Reverse Flys");
            chestList.add("Supine Press");
        }
        return randomExercises(chestList);
    }

    private String[] legDay() {
        if (legList.isEmpty()) {
            legList.add("Squat");
            legList.add("Leg Press");
            legList.add("Leg Extension");
            legList.add("Dumbbell Step Up");
            legList.add("Body Weight Calf Raises");
            legList.add("Walking Lunge");
        }
        return randomExercises(legList);
    }

    private String[] randomExercises(List<String> arr) {
        Collections.shuffle(arr);
        return arr.subList(0, 3).toArray(new String[3]);
    }

    private String selectMuscles(String[] arr) {
        return (Arrays.toString(arr));
    }

    public static void main(String args[]) {
        Application.launch(args);
    }

}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.