java - Scanner cannot be used when declared, sc cannot be resolved -
the problem i'm having program scanner, i've used many times in program won't run right. error in public static double getcandlecost() , public static int getshippingtype() methods. under int shippingtype = sc.nextint(); , double candlecost = sc.nextdouble(); both "sc cannot resolved" , in main class did declare it.
import java.util.scanner; import java.text.decimalformat; public class candel { public static void main(string[] args) { scanner sc = new scanner(system.in); double candlecost, shippingcost; int shippingtype; candlecost = getcandlecost(); shippingtype = getshippingtype(); shippingcost = getshippingcost(candlecost, shippingtype); output(candlecost, shippingcost); } public static double getcandlecost() { boolean done = false; do{ try { system.out.print("enter cost of candle order "); double candlecost = sc.nextdouble(); done = true; return candlecost; } catch (inputmismatchexception e) { system.out.println("error, enter dollar amount greater 0"); } } while (!done); return 0; } public static int getshippingtype() { system.out.println("enter type of shipping: "); system.out.println("1> priority <overnight>"); system.out.println("2> express <2 business days>"); system.out.println("3> standard <3 7 business days>"); system.out.println("enter type number: "); int shippingtype = sc.nextint(); if(shippingtype == 1){} else if(shippingtype == 2){} else if(shippingtype == 3){} return shippingtype; } public static double getshippingcost(double candlecost, int shippingtype) { switch(shippingtype) { case 1: candlecost = 16.95 + candlecost; break; case 2: candlecost = 13.95 + candlecost; break; case 3: if (candlecost > 100.00){ candlecost = candlecost; } else{ candlecost = 7.95 + candlecost; } break; } return candlecost; } public static void output(double candlecost, double shippingcost) { decimalformat twodigits = new decimalformat("$#,000.00"); system.out.println("the candle cost of " + twodigits.format(candlecost) + " shipping costs of " + shippingcost + " equals " + twodigits.format(candlecost + shippingcost)); } }
your scanner out of scope. since have declared in main()
method, method can access it. scanner needs static well. write instead:
public class candel { static scanner sc = new scanner(system.in); public static void main(string[] args) { ... } ... }
Comments
Post a Comment