'Returm to a specific line or place

I'm self learning Java for a few weeks and started testing my (basic) knowledge. I was trying to create something like some formula calculator, with an index selector. I want to know if it's possible to, after using a formula, go back to the first question. I tried to use a while loop, but I couldn't figure it out.

public class formulas {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // first question
            
        
        // constant values definition
        final double PI = 3.141592653589793238;
        
        //index
        ArrayList<String> index = new ArrayList<>();
                                
        index.add("1. Pythagorean Theorem");
        index.add("2. Square Area");
        index.add("2. Triangle Area");
    
        System.out.println("Formulas Index:");
        System.out.println("");
        for(int i=0; i<index.size(); i++) {
            System.out.println(index.get(i));
        }
        
        System.out.println("");
        System.out.print("Write the formulas number you want to use: ");
        
        Scanner scanner = new Scanner(System.in);
        String firstQuestion = scanner.nextLine();
        System.out.println();
        
        // methods selection
        if (firstQuestion.equals("1")) {
            System.out.println("Pythagoras Theorem");
            System.out.print("Insert side a: ");
            double a = scanner.nextDouble();
            System.out.print("Insert side b: ");
            double b = scanner.nextDouble();
            double c = pitagoras(a,b);
            System.out.println("Hypotenuse c: "+ c);
        }
        else if (firstQuestion.equals("2")) {
            System.out.println("Square area");
            System.out.print("Insert side a: ");
            double arestaA = scanner.nextDouble();
            System.out.print("Insert side b: ");
            double arestaB = scanner.nextDouble();
            double squareArea = squarearea(arestaA,arestaB);
            System.out.println("The square area is: "+ squareArea);
        }
        else System.out.println("Please insert a valid formula number.");
    }

        // methods parameters
    static double pitagoras(double a, double b) {
        double c = Math.sqrt((a*a)+(b*b));
        return c;   
    }

    static double squarearea(double a, double b) {
        double area = a*b;
        return area;
    }

}


Solution 1:[1]

I'm always surprised that people don't make more use of do-while loops, it's a severely underrated construct.

Think about it, you MUST do at least one iteration of the loop before you know if you want to continue or exit the loop. You also want to re-print the menu on each iteration, so it's easier to just put it in a do-while (IMHO)

You can take a look at Control Flow Statements and The while and do-while Statements for more details

import java.util.ArrayList;
import java.util.Scanner;

public final class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // first question

        // constant values definition
        final double PI = 3.141592653589793238;

        //index
        ArrayList<String> index = new ArrayList<>();

        index.add("1. Pythagorean Theorem");
        index.add("2. Square Area");
        index.add("3. Triangle Area");
        index.add("4. Quit");

        Scanner scanner = new Scanner(System.in);
        boolean printMenu = true;
        boolean exit = false;
        do {
            if (printMenu) {
                System.out.println("");
                System.out.println("Formulas Index:");
                System.out.println("");
                for (int i = 0; i < index.size(); i++) {
                    System.out.println(index.get(i));
                }

                System.out.println("");
                System.out.print("Write the formulas number you want to use: ");
            }
            printMenu = true;

            String firstQuestion = scanner.nextLine();
            System.out.println();

            // methods selection
            if (firstQuestion.equals("1")) {
                System.out.println("Pythagoras Theorem");
                System.out.print("Insert side a: ");
                double a = scanner.nextDouble();
                System.out.print("Insert side b: ");
                double b = scanner.nextDouble();
                double c = pitagoras(a, b);
                System.out.println("Hypotenuse c: " + c);
            } else if (firstQuestion.equals("2")) {
                System.out.println("Square area");
                System.out.print("Insert side a: ");
                double arestaA = scanner.nextDouble();
                System.out.print("Insert side b: ");
                double arestaB = scanner.nextDouble();
                double squareArea = squarearea(arestaA, arestaB);
                System.out.println("The square area is: " + squareArea);
            } else if (firstQuestion.equals("3")) {
                // Triangle area
            } else if (firstQuestion.equals("4")) {
                exit = true;
            } else {
                printMenu = false;
                System.out.println("Please insert a valid formula number.");
            }
        } while (!exit);
    }

    // methods parameters
    static double pitagoras(double a, double b) {
        double c = Math.sqrt((a * a) + (b * b));
        return c;
    }

    static double squarearea(double a, double b) {
        double area = a * b;
        return area;
    }
}

Solution 2:[2]

You can simply wrap your code starting from firstQuestion variable declaration till the end of the if condition in a while loop which is always set to true.

public class Formulas {

    public static void main(String[] args) {

        // first question

        // constant values definition
        final double PI = 3.141592653589793238;

        // index
        ArrayList<String> index = new ArrayList<>();

        index.add("1. Pythagorean Theorem");
        index.add("2. Square Area");
        index.add("2. Triangle Area");

        System.out.println("Formulas Index:");
        System.out.println("");
        for (int i = 0; i < index.size(); i++) {
            System.out.println(index.get(i));
        }
   Scanner scanner = new Scanner(System.in);
         
        while (true) {
            System.out.println("");
            System.out.print("Write the formulas number you want to use: ");

            String firstQuestion = scanner.nextLine();
            System.out.println();

            // methods selection
            if (firstQuestion.equals("1")) {
                System.out.println("Pythagoras Theorem");
                System.out.print("Insert side a: ");
                double a = scanner.nextDouble();
                System.out.print("Insert side b: ");
                double b = scanner.nextDouble();
                double c = pitagoras(a, b);
                System.out.println("Hypotenuse c: " + c);
            } else if (firstQuestion.equals("2")) {
                System.out.println("Square area");
                System.out.print("Insert side a: ");
                double arestaA = scanner.nextDouble();
                System.out.print("Insert side b: ");
                double arestaB = scanner.nextDouble();
                double squareArea = squarearea(arestaA, arestaB);
                System.out.println("The square area is: " + squareArea);
            } else
                System.out.println("Please insert a valid formula number.");
        }

    }

    // methods parameters
    static double pitagoras(double a, double b) {
        double c = Math.sqrt((a * a) + (b * b));
        return c;
    }

    static double squarearea(double a, double b) {
        double area = a * b;
        return area;
    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2