Write code to ask the user to enter their favorite food and print the following message: I like to eat ______ as well! Where ______ is replaced with the name of the food input by the user. Remember, you'll need to create a Scanner and use the method nextLine on it to get the user input and store it in a variable.

1 answer

Here is a simple Java program that prompts the user to enter their favorite food, and then prints a message incorporating that input:

import java.util.Scanner;

public class FavoriteFood {
    public static void main(String[] args) {
        // Create a Scanner object to read user input
        Scanner scanner = new Scanner(System.in);
        
        // Prompt the user to enter their favorite food
        System.out.print("Enter your favorite food: ");
        
        // Read the input and store it in a variable
        String favoriteFood = scanner.nextLine();
        
        // Print the message
        System.out.println("I like to eat " + favoriteFood + " as well!");
        
        // Close the scanner
        scanner.close();
    }
}

You can run this code in a Java environment. When executed, it will ask the user for their favorite food and then respond with the message that includes the entered food.