Hi everyone, this is my first post. I'm trying to implement a case-break menu into a class. Here's the code:
import java.util.Scanner; public class UnitsToCups { public void displayMenu(){ System.out.println("Option 1: Convert teaspoons to cups\n Option 2: Convert tablespoons to cups\n" + "Option 3: Convert pints to cups\n Option 4: Convert quarts to cups\n Option 5: Convert gallons to cups\n"); } double tsp = 0.0208333333333333;//1 teaspoon = 1/48 of a cup double tbsp = 0.0625;//1 tablespoon = 1/16 of a cup int pint = 2;//1 pint = 2 cups int quart = 4;//1 quart = 4 cups int gallon = 16;//1 gallon = 16 cups public void Menu(){ Scanner menuSelect = new Scanner(System.in); displayMenu(); switch(menuSelect.nextInt()){ case 1: System.out.println("Enter number of teaspoons: "); Scanner teaspoons = new Scanner(System.in); int numTsp = teaspoons.nextInt(); double cups1 = tsp * 1; System.out.println(numTsp + " teaspoons is equivalent to " + cups1 + " Cups, or (Fraction?)"); teaspoons.close(); break; case 2: System.out.println("Enter number of tablespoons: "); Scanner tablespoons = new Scanner(System.in); int numTbsp = tablespoons.nextInt(); double cups2 = tbsp * 1; System.out.println(numTbsp + " tablespoons is equivalent to " + cups2 + " Cups, or (Fraction?)"); tablespoons.close(); break; case 3: System.out.println("Enter number of pints: "); Scanner pints = new Scanner(System.in); int numPints = pints.nextInt(); double cups3 = pint * 1; System.out.println(numPints + " pints is equivalent to " + cups3 + " Cups, or (Fraction?)"); pints.close(); break; case 4: System.out.println("Enter number of quarts: "); Scanner quarts = new Scanner(System.in); int numQuarts = quarts.nextInt(); double cups4 = quart * 1; System.out.println(numQuarts + " quarts is equivalent to " + cups4 + " Cups, or (Fraction?)"); quarts.close(); break; case 5: System.out.println("Enter number of gallons: "); Scanner gallons = new Scanner(System.in); int numGallons = gallons.nextInt(); double cups5 = gallon * 1; System.out.println(numGallons + " gallons is equivalent to " + cups5 + " Cups, or (Fraction?)"); gallons.close(); break; default: System.err.println("Invalid option. Choose an option from 1 to 5."); break; menuSelect.close(); } } public static void main(String[] args){ displayMenu(); Menu(); } }
The issues are:
Line 63: "Unreachable code"
Line 68: "Cannot make a static reference to the non-static method displayMenu() from the type UnitsToCups"
Line 69: "Cannot make a static reference to the non-static method Menu() from the type UnitsToCups"