New to Java, having trouble [closed]

2 days ago 3
ARTICLE AD BOX

I have only been doing javascript for the past 2 or so weeks, and I am having trouble with a college assignment that gives errors when trying to use if, ifelse, and else commands, and issues if I try to use the variable, switch, and case commands. I do not know what is going wrong. but it keeps saying that I have a non-static variable that cannot be referenced from a static context. So if anyone knows how to fix this, that would be amazing. I have attached the code to this message, along with the errors.

[UPDATE] Changed the formatting to what some people have been suggesting. and now I have this.

com/mycompany/pizzaorderprogram/PizzaOrderProgram.java:[19,25] non-static >variable scanner cannot be referenced from a static context

com/mycompany/pizzaorderprogram/PizzaOrderProgram.java:[29,24] non-static >variable scanner cannot be referenced from a static context

com/mycompany/pizzaorderprogram/PizzaOrderProgram.java:[41,23] non-static >variable scanner cannot be referenced from a static context

com/mycompany/pizzaorderprogram/PizzaOrderProgram.java:[53,65] cannot find >symbol

symbol: variable pizzatype

[SIDENOTE] I have a strange black bar wherever I put my cursor and tried hitting insert again and it didnt work.

package com.mycompany.pizzaorderprogram; import java.util.Scanner; public class PizzaOrderProgram { Scanner scanner = new Scanner (System.in); public static void main(String[] args) { int TotalCost = 0; int SizeCost = 0; int CrustCost = 0; int ToppingCost = 0; System.out.println("Welcome to Willey's Pizza!\n"); System.out.println("What size pizza are you wanting?: "); var pizzasize = scanner.nextLine(); switch(pizzasize){ case "Small" -> SizeCost = 8; case "Medium" -> SizeCost = 12; case "Large" -> SizeCost = 16; default -> System.out.print("You did not pick a valid pizza size. Please try again."); } System.out.println("Please enter what type of crust you would like"); var crusttype = scanner.nextLine(); switch(crusttype){ case "Regular" -> CrustCost = 0; case "Pan Crust" -> CrustCost = 2; case "Stuffed Crust" -> CrustCost = 4; case "Ranch Crust" -> CrustCost = 2; default -> System.out.print("You did not enter a valid crust type. Please try again."); } System.out.println("Please enter what type of toppings you would like."); var toppings = scanner.nextLine(); switch(toppings){ case "Plain" -> ToppingCost = 0; case "Pepperoni" -> ToppingCost = 1; case "Grilled Chicken" -> ToppingCost = 2; case "Extra Cheese" -> ToppingCost = 1; case "Sausage" -> ToppingCost = 3; default -> System.out.print("You did not enter a valid topping. Please try again."); } TotalCost = SizeCost + CrustCost + ToppingCost; System.out.print("You selected a: " + pizzasize + ", " + pizzatype + ", " + toppings + " pizza.\n"); System.out.print("Your total comes to: $" + TotalCost); } }
Read Entire Article