0

I have three programs to write for my Object Oriented Programming course, all involving file input/output, each of which contain no compile errors, yet they do not do what they are supposed to in run time (they don't print to the outFile like they're supposed to).

I know that the input file is being read and saved in the correct location, because Eclipse would indicate if either of these was not the case.

Furthermore, I have not (to my knowledge) committed any of the common errors involving not including throws exceptions of closing the read/write files.

I am attaching the first of my i/o assignments here with the hopes that the other files have similar errors that I can fix as soon as I can figure out what's wrong with this one.

import java.io.*;

public class GreenK4_Lab8 {
public static void main(String[] args) throws IOException {
    int[] numbers = new int[countLines()];
    int i = 0;
    for(i = 0; i < numbers.length; i++) {
        numbers[i] = readValues(i);
    }
    printOdd(numbers);
}


public static int countLines() throws IOException {
    BufferedReader inFile = new BufferedReader(
            new FileReader( "Lab8_TestFile.txt" ) );

    int lineNumber = 1;
    String nextLine = inFile.readLine();
    while( nextLine != null ) {
        lineNumber ++;
    }
    inFile.close();
    return lineNumber;    
}

public static int readValues(int number) throws IOException {
    BufferedReader inFile = new BufferedReader(
            new FileReader( "Lab8_TestFile.txt" ) );

    int value = 0;
    for(int i = 0; i < number; i++) {
        String nextLine = inFile.readLine();
        value = Integer.parseInt( nextLine );
    }
    inFile.close();
    return value;
}

public static void printOdd(int[] array) throws IOException {
    PrintWriter outFile = new PrintWriter( "results.out" );
    for(int i = 0; i < array.length; i++) {
        int value = array[i];
        if( value % 2 != 0)
            outFile.println( value );
    }

    outFile.close();
}
}

The following are the contents of the Lab8_TestFile.txt

4
6
2
10
8
1
-1
-2147483648
2147483647
5
9
3
7
-7
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 5
    Please remove all the useless comments. Commenting `int i = 0` with `Declare integer, i and assign to it the value of 0`is completely useless and clutters the code. – JB Nizet May 08 '15 at 06:30
  • "they don't print to the outFile like they're supposed to". What happens instead? –  May 08 '15 at 06:33
  • 1
    `nextLine != null` condition is never changed - you should use something like `while((nextLine = inFile.readLine()) != null)` or simple `while(inFile.readLine() != null)` – rzysia May 08 '15 at 06:44
  • JB Nizet: Couldn't agree with you more on the comments. Unfortunately, however, that's what my professor wants. He says that our comments should describe, line by line, what our code does. If I do anything other than what I've done here, I lose points for what he calls front loading my comments. Tichodroma: it just doesn't create the outfile at all. rzysia: actually, the nextLine condition is changed. The inFile.readLine method actually does something similar to what you just described. You just don't see the code because the method is called from the Java library. – Kyle Joseph Green May 08 '15 at 08:27
  • I might try seeing what the program does when I insert that method into my while loop. Maybe I'm misunderstanding how the readLine method works. I doubt it, because I think I remember our professor giving us the code for that section, but it's worth a try. – Kyle Joseph Green May 08 '15 at 08:35
  • I would say your professor has no idea how to write clean code... Maybe tell him that. – luk2302 May 10 '15 at 08:06
  • @ryzia The first of those forms is correct. The second just throws the input away. Simple but entirely useless. – user207421 May 10 '15 at 08:09
  • @EJP he is referring to the `countLines` method, throwing the input away is perfectly fine there (ignoring the fact that the whole structure around is wrong) – luk2302 May 10 '15 at 08:15

2 Answers2

0

As other commenters pointed out, change your code in countLines function from

    String nextLine = inFile.readLine();
    while( nextLine != null ) {
        lineNumber ++;
    }

to

    while (inFile.readLine() != null) {
        lineNumber ++;
    }

With this change your program works as expected.

vbezhenar
  • 11,148
  • 9
  • 49
  • 63
0

There are multiple things wrong with your code. Let´s start from the beginning: your countLines method does not work as intended and will create a infinite loop because your while-condition will never be evaluated to false (unless your file is empty):

// String nextLine = inFile.readLine();
// while(nextLine != null) {
while (inFile.readLine() != null) {
    lineNumber++;
}

You may want to check Number of lines in a file in Java for a faster and better performing version of retrieving the line count of a file.

Additionally your readValues function opens the file for every line it wants to read, reads the file until that line and closes the file again -> BAD. What you should do instead is the following:

public static void readValues(int[] contentsOfFile) throws IOException {
    BufferedReader inFile = new BufferedReader(new FileReader("Lab8_TestFile.txt"));

    for(int i = 0; i < contentsOfFile.length; i++) {
        String nextLine = inFile.readLine();
        contentsOfFile[i] = Integer.parseInt( nextLine );
    }

    inFile.close();
}

However that is not pretty as well since you rely on a adequately sized int array to be passed in. If you still want to get the line count separately from reading the values, do so, but let the readValues handle the appropriate reading by itself. That could result in something like:

public static ArrayList<Integer> readValues() throws IOException {
    BufferedReader inFile = new BufferedReader(new FileReader("Lab8_TestFile.txt"));
    ArrayList<Integer> integerContents = new ArrayList<>();

    String nextLine = null;
    while ((nextLine = inFile.readLine()) != null) {
        integerContents.add(Integer.parseInt(nextLine));
    }

    inFile.close();
    return integerContents;
}

That way you parse the file only once for reading the values. If you need to get a int[] back, take a look at How to convert an ArrayList containing Integers to primitive int array? to get an idea on how to extract that from the given data structure.

Your main function might result in something like:

public static void main(String[] args) throws IOException {
    int numberOfLines = countLines(); // technically no longer needed.
    int[] intContents = convertIntegers(readValues());
    printOdd(intContents);
}
Community
  • 1
  • 1
luk2302
  • 55,258
  • 23
  • 97
  • 137