Home > database >  How can I solve my error? "error: illegal start of type import java.util.Scanner;" [duplic
How can I solve my error? "error: illegal start of type import java.util.Scanner;" [duplic

Time:10-06

public class LabExer2 {
    
**import java.util.Scanner;**
    
public class Main {
    
    private String itemName;
    private double itemPrice;
    private int itemQuantity;
    private double amountDue;

    public void setItemName(String newItemName) {
        this.itemName = itemName;
    }

    public void setTotalCost(int quantity, double price) {
        this.itemQuantity = quantity;
        this.itemPrice = price;
    }

    public String getItemName() {
        return itemName;
    }

    public double getTotalCost() {
        return itemPrice;
    }

    public void readInput() {
    }

    public void writeOutput(){

    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the item you are purchasing ");
        String itemName = s.nextLine();
        System.out.println("Enter the quantity and price seperated by a space ");
        int itemQuantity = s.nextInt();
        double itemPrice = s.nextDouble();
        System.out.println("You are purchasing "    itemQuantity     " "    itemName   "(S)"   " at "   itemPrice   " each.");
        
            }
    
        }
    }

Output: /LabExer2.java:3: error: illegal start of type import java.util.Scanner; ^ /LabExer2.java:3: error: expected import java.util.Scanner; ^ 2 errors

CodePudding user response:

import java.util.Scanner;

public class Main{

    private String itemName;
    private double itemPrice;
    private int itemQuantity;
    private double amountDue;

    public void setItemName(String newItemName) {
        this.itemName = itemName;
    }

    public void setTotalCost(int quantity, double price) {
        this.itemQuantity = quantity;
        this.itemPrice = price;
    }

    public String getItemName() {
        return itemName;
    }

    public double getTotalCost() {
        return itemPrice;
    }

    public void readInput() {
    }

    public void writeOutput() {

    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the item you are purchasing ");
        String itemName = s.nextLine();
        System.out.println("Enter the quantity and price seperated by a space ");
        int itemQuantity = s.nextInt();
        double itemPrice = s.nextDouble();
        System.out.println("You are purchasing "   itemQuantity   " "   itemName   "(S)"   " at "   itemPrice   " each.");

    }

}
  • Related