0
    File tempFile = new File(loadedFileName);
    FileInputStream datStream = new FileInputStream(tempFile);
    InputStreamReader readDat = new InputStreamReader(datStream);
    int data = readDat.read();
    String temp = "";

    // keeps reading in one character at a time and returns -1 if there are no more          
    //  characters
    while(data != -1){  
        char datChar = (char)data;
        if(temp.length() > 2){
            if((temp.substring(temp.length()-1)).equals("\n")){
                String[] arrayTemp = temp.split("\\|");

                if(Float.valueOf(arrayTemp[columnNumber-1]) > value){
                    System.out.print(temp); 
                }
                temp = "";
            }
        }
        temp = temp+datChar;
        data = readDat.read();
    }

The code reads in a file character by character and appends it to a string. Once it reaches a new line it will split the string into an array and then checks to see if a value in that array matches and prints out the string before it was split.

The problem with this code is that even though it gets most of the job done, because of how it is in a while loop where it checks to see if it reaches the end, which if it does it returns -1. This makes it so that I can't print the last line of the file because there is no new line at the end of the file, so it kills the loop before it gets to print out the last line.

An example of a few lines being read in.

This | world | is | brown | and | dirty|
24 | hours | are | in | day| Hello|

Can't store the whole file into an array, can't use a buffered reader, I've tried doing something like counting the number of "|" but I can't seem to get that to work. So in this case if it counted to 6 pipes it would then split and then check before printing. Which I think would solve the problem of not printing the last line. Here is how I tried to implement the count for the |.

    while(data != -1){  
        char datChar = (char)data;
        // checks to make sure that temp isn't an empty string first
        if(temp.length() > 2){
            // checks to see if a new line started and if it did splits  the current string into an array.
            if((temp.substring(temp.length()-1)).equals("\\|")){
                if(count == 6){

                String[] arrayTemp = temp.split("\\|");

                //then checks the variable in the array col and compares it with a value and prints if it is greater.
                if(Float.valueOf(arrayTemp[columnNumber-1]) > value){
                    System.out.print(temp); 
                }
                temp = "";
                count = 0;
                }
            }
        }
        temp = temp+datChar;
        data = readDat.read();
    }
user2677821
  • 106
  • 9

3 Answers3

2

You'd be better off using a BufferedReader and readLine. That will be more efficient in terms of IO, and means you don't need to worry about handling the line breaks yourself:

BufferedReader reader = new BufferedReader(readDat);
String line;
while ((line = reader.readLine()) != null) {
    // Handle a line of data - check for it being empty, split it etc.
}

This will give you all the lines in the file, whether or not there's a terminating newline.

EDIT: If you really can't use BufferedReader, I would make your code significantly simpler by checking whether the current character is \n or the end of the file, and processing the "line so far" if so:

StringBuilder line = new StringBuilder();
while (true) {
    int next = readDat.read();
    if (next == '\n' || next == -1) {
        // Handle line here

        line = new StringBuilder();
        if (next == -1) {
            break;
        }
    } else {
        line.append((char) next);
    }
}

Note the use of StringBuilder instead of repeated concatenation.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Haha thanks, but I can't use bufferedreader and readline, this is kind of an assignment and actually implementing bufferedreader is a different part of it and I finished that part already and yeah it does make it so much easier. – user2677821 Sep 17 '13 at 08:57
  • @user2677821: You should have mentioned that first. It changes the situation completely. – Jon Skeet Sep 17 '13 at 09:16
  • Ahh yeah sorry about that. After looking at your edit I forgot that I can use the || in my if statements... to add another parameter. Nvm thanks read the code again and it clicked in my head! – user2677821 Sep 17 '13 at 09:27
0

Maybe you can try counting the number of lines in a file and check on that, in addition to checking for newlines.

Also, you could rewrite your code to read a whole line and split that, instead of reading a file character by character. Should you do that, you don't have to check for newlines (and I think you don't even have to check if the current read line is not the last line of the file. I'm not sure if it's faster (but I'm betting it is), but it would definitely result in better readable (and as a result, better maintainable) code.

Community
  • 1
  • 1
Stefan
  • 1,433
  • 1
  • 19
  • 30
0

I may be misunderstanding your question, but it seems to me like some line based scanning using a basic Scanner would be much easier in this situation. Would the following simplify things for you?

Scanner input = new Scanner(new File("FileName.txt"));
while (input.hasNextLine()) {
    String[] arrayTemp = input.nextLine().split("\\|");
    //etc etc.
}
ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64