019-005-002

Resource Management: Recipe File Reading

Easy

Problem Description

Resource Management: Recipe File Reading

In this problem, you will create a program that reads a recipe name and cooking time from standard input using a try-with-resources statement with Scanner, and displays the result to standard output.

Learning Objective: Learn to safely manage resources using try-with-resources statement

Create program to read recipe information from file. Using try-with-resources statement (try (...) {}), resources like Scanner are automatically closed, preventing resource leaks. Traditional try-finally required manual close() calls, but try-with-resources has Java handle it automatically.

Example 1: Curry Recipe (Cooking Time: 30 minutes)

Read 2 lines of data (recipe name and cooking time) from standard input. With try-with-resources statement, Scanner is automatically closed when exiting try block.

Input:
Curry
30

Output:
=== Recipe File Reader ===
Recipe: Curry
Cooking Time: 30 minutes
━━━━━━━━━━━━━━━━
Resource: Closed automatically
Status: Reading completed
```java

### Example 2: Pasta Recipe (Cooking Time: 20 minutes)
Similarly read 2 lines of data, Scanner is automatically closed. Programmer doesn't need to explicitly write resource close processing.
```java
Input:
Pasta
20

Output:
=== Recipe File Reader ===
Recipe: Pasta
Cooking Time: 20 minutes
━━━━━━━━━━━━━━━━
Resource: Closed automatically
Status: Reading completed
```java

### Example 3: Salad Recipe (Cooking Time: 5 minutes)
Works correctly even for quick preparation recipes. Try-with-resources statement safely manages resources with any values.
```java
Input:
Salad
5

Output:
=== Recipe File Reader ===
Recipe: Salad
Cooking Time: 5 minutes
━━━━━━━━━━━━━━━━
Resource: Closed automatically
Status: Reading completed
```java

## Input
Recipe name and cooking time from standard input:
Line 1: Recipe name
Line 2: Cooking time (minutes)

## Output
```java
=== Recipe File Reader ===
Recipe: [recipe name]
Cooking Time: [time] minutes
━━━━━━━━━━━━━━━━
Resource: Closed automatically
Status: Reading completed

Test Cases

※ Output examples follow programming industry standards

Normal case
Input:
Pizza
45
Expected Output:
=== Recipe File Reader ===
Recipe: Pizza
Cooking Time: 45 minutes
━━━━━━━━━━━━━━━━
Resource: Closed automatically
Status: Reading completed
Normal case
Input:
Steak
60
Expected Output:
=== Recipe File Reader ===
Recipe: Steak
Cooking Time: 60 minutes
━━━━━━━━━━━━━━━━
Resource: Closed automatically
Status: Reading completed

Your Solution

Current Mode: My Code
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here

sc.close();
}
}
0 B / 5 MB

You have 10 free executions remaining