Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:06:42+00:00 2026-06-16T00:06:42+00:00

TO CLARIFY: I can’t even compile due to messages from Eclipse. First code snippet:

  • 0

TO CLARIFY: I can’t even compile due to messages from Eclipse. First code snippet: input and inputBuffer are not recognized. Second code snippet, Eclipse wants me to switch switch “Compliance and JRE to 1.7”

I am new to try-with-resources and I can’t quite understand the syntax or what I’m doing wrong. Here is my code

try {
   FileReader input = new FileReader(this.fileName);
   BufferedReader  inputBuffer = new BufferedReader (input);

   String line;
   while ((line = inputBuffer.readLine()) != null) {
       String[] inputData = line.split(",");

        Node<Integer> newNode = new Node<Integer>(Integer.parseInt(inputData[0]),
                                                  Integer.parseInt(inputData[1]));
        this.hashMap.add(newNode);
   }

//inputBuffer.close();
//input.close();

}catch (NumberFormatException nfe){
    System.out.println(
         "Repository could not load data due to NumberFormatException: " + nfe); 
}catch (FileNotFoundException fnfe) {
    System.out.println("File not found, error: " + fnfe);
}finally { 
    inputBuffer.close();
    input.close();
}

The finally block does not work, so i wanted to try

try (FileReader input = new FileReader(this.fileName)) {
      ......        
}catch (FileNotFoundException e) {
      ......
}finally {
      inputBuffer.close();
      input.close();
}

However

  • I should also add BufferedReader to try (...) … but how ?

  • Also this requires me to switch “Compliance and JRE to 1.7”. I don’t know what that means and how that would affect my program so far, I’m not willing to do it until someone explains what it all means or if I’m doing something wrong.

EDIT

I moved declaration before try block and initialized with null, is this “ok” ?

FileReader input = null;
BufferedReader  inputBuffer = null;

try {
    input = new FileReader(this.fileName);
    inputBuffer = new BufferedReader (input);
    ...
}   ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:06:43+00:00Added an answer on June 16, 2026 at 12:06 am

    Firstly declare input and inputBuffer before try{}catch(){}

    like this

    FileReader input = null;
    BufferedReader  inputBuffer = null;
    try
    {
     ------
     -------
    
    
    }
    

    in your first block in the finally : Check below two conditions

    if(inputBuffer != null)
    {
        inputBuffer.close();
    }
    if(input != null)
    {
          input.close();
    }
    

    Second : if you want more than one resources in the try do this:

    try ( OpenDoor door = new OpenDoor(); OpenWindow window = new OpenWindow() ) 
    {
    
    }
    

    No need to close the above resources in finally block in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone clarify the significance of the below code. class A { int i
Maybe somebody can clarify what is wrong with the code below. When I create
Ok, I realize that's not a very clear title, so hopefully this can clarify
EDIT: Due to random downvotes still coming in I guess I can clarify this
I've been looking at the following code and am wondering if someone can clarify
Can you clarify this please? public static void MyMethod() { var context= new MyModel.Entities();
Can some one clarify the space complexity of Node Relationship and property in the
Can someone please clarify using a SIMPLE user story the full slice of what
Can someone please help to clarify? Also, please mention if there are other representation
Just to clarify, by specifying something like VARCHAR(45) means it can take up to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.