Noah
Noah

Reputation: 1

Program won't run scanner cannot be resolved

This is the code I have so far and this is only one file of multiple ones that all belong to the same program.

As soon as I want to compile and check my errors eclipse tells me that it cannot resolve the scanner and I have no idea how to fix this problem nor what it exactly means.

import java.util.Scanner;

public class PieShop {
    static FoodItem foodItem = new FoodItem();
    public static void main(String[] args) {
        Scanner_in.consoleLine("Enter Food item File name:");
        foodItem.foodItemFile=new File(Scanner_in.getConsole());
        foodItem.addFoodItem();
        foodItem.displayAll();
        foodItem.choices();
    }
}

Upvotes: 0

Views: 103

Answers (2)

JDC
JDC

Reputation: 4385

This would be the correct way if you wan to read the input from the console:

import java.util.Scanner;

public class PieShop {

    private static FoodItem foodItem = new FoodItem();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Initialize scanner
        System.out.println("Enter Food item File name:"); // Print yourtext
        foodItem.foodItemFile = new File(scanner.nextLine()); // Read from scanner
        foodItem.addFoodItem();
        foodItem.displayAll();
        foodItem.choices();
    }
}

Upvotes: 1

Fairoz
Fairoz

Reputation: 1666

Below code should work fine provided that you pass right value to scanner source

import java.util.Scanner;

public class PieShop {
    static FoodItem foodItem = new FoodItem();
    public static void main(String[] args) {
        Scanner Scanner_in = new Scanner(source);
        Scanner_in.consoleLine("Enter Food item File name:");
        foodItem.foodItemFile=new File(Scanner_in.getConsole());
        foodItem.addFoodItem();
        foodItem.displayAll();
        foodItem.choices();
    }
}

Upvotes: 1

Related Questions