Kristian
Kristian

Reputation: 1239

Unable to get proper boolean output from method (Java)

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!

Upvotes: 1

Views: 1078

Answers (1)

Michael Easter
Michael Easter

Reputation: 24498

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;
    }

Upvotes: 3

Related Questions