019-007-005

Exception Handling: Try-With-Resources

Hard

Problem Description

exception handling: Try-With-Resources

In this problem, you will create a program that manages a Scanner using try-with-resources, reads two strings, concatenates them with a space separator, and displays the result along with a "Resource closed" message to standard output.

Learning Objective: Understand resource management with try-with-resources statement

Overview

The try-with-resources statement introduced in Java 7 automatically closes resources that implement the AutoCloseable interface. This eliminates the need for close operations in finally blocks, making code more concise.

Specifications

Create a program that reads two strings using Scanner and concatenates them.

  • Use try-with-resources statement to manage Scanner
  • Read strings from first and second lines
  • Concatenate two strings with space separator and output
  • Also output "Resource closed" message

Input Format

<string1>
<string2>

Output Format

<string1> <string2>
Resource closed

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up