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 6791605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:52:00+00:00 2026-05-26T17:52:00+00:00

I have written a boolean method which can be called upon to read information

  • 0

I have written a boolean method which can be called upon to read information from a file. If certain exceptions are thrown, the method should return the boolean value “false.” If not, it should return the value “true”. However, the method always returns the value “true” no matter what. The actual file being read has a number in the first line (representing week number), and the rest of the lines consist of treatment information for one patient for each line. My code is as follows:

public boolean readTreatmentsFromFile(String filename) {
  boolean value = true;
  try {
    FileReader textFileReader = new FileReader(filename);
    BufferedReader textReader = new BufferedReader(textFileReader);

    System.out.println("READING TREATMENTS FROM FILE: " + filename);

    int week = Integer.parseInt(textReader.readLine());

    if (week != weekNumber) {
      throw new ArithmeticException(); 
    }

    String post = textReader.readLine();

    while(post != null) {
      addPost(post);
      if (!addPost(post))
        value = false;
      post = textReader.readLine();
    }
    textReader.close();
  }
  catch (ArithmeticException exception) {
    System.out.println("Invalid week number in the file: " + filename);
    value = false;
  }
  catch (FileNotFoundException exception) {
    System.out.print("The file cannot be located");
    value = false;
  }
  catch (IOException exception) {
    System.out.print("Cannot read the file:" + filename);
    value = false;
  }

  return value;
}

Any information as to how I should alter the code in order for it to give me a proper boolean result will be greatly appreciated!

  • 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-05-26T17:52:00+00:00Added an answer on May 26, 2026 at 5:52 pm

    I would consider this: return a variable ‘result’, which defaults to false; set it to true only when successful. See below (note that I have not debugged your code per se):

    public boolean readTreatmentsFromFile(String filename) {
        boolean result = false; // pessimistic default
    
            try {
                FileReader textFileReader = new FileReader(filename);
                BufferedReader textReader = new BufferedReader(textFileReader);
    
                System.out.println("READING TREATMENTS FROM FILE: " + filename);
    
                int week = Integer.parseInt(textReader.readLine());
    
                if (week != weekNumber) {
                    throw new ArithmeticException(); 
                }
    
                String post = textReader.readLine();
    
                while(post != null) {
                    addPost(post);
                    post = textReader.readLine();
                }
                textReader.close();
                result = true;  // SUCCESS!
            }
            catch (ArithmeticException exception) {
                System.out.println("Invalid week number in the file: " + filename);
            }
            catch (FileNotFoundException exception) {
                System.out.print("The file cannot be located");
            }
            catch (IOException exception) {
                System.out.print("Cannot read the file:" + filename);
            }
    
            return result;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following method to detemine whether file in question is formatted
I have always written my boolean expressions like this: if (!isValid) { // code
I have written an AIR Application that downloads videos and documents from a server.
I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have sample code written for parsing xml file using javax.xml package. it uses
I have written the following code to perform some simultaneous HTTP posting and file
I have a method which is used to check if the given class is
I have written PhoneStateListener which listens for telephony events through broadcast receiver. I am
I have a boolean method returning true or false to check whether or not

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.