archaeogisci
archaeogisci

Reputation: 1

Checking first character in a string when reading from text file causes exception: Should be quick/easy :)

This should be a relatively quick fix for a more experienced programmer...I just wanted to input a text file and put a '#' in front of every line that doesn't begin with a '>' or a '+'. The problem is that I'm getting an exception, the .charAt(0) is out of the bounds of the String. But before the console executes that line the String should be read from the text file--what am I missing? Do I need to insert a line of something so that lineFromFile String declared earlier isn't empty? I don't know how to go about that...

import java.io.*;

public class FirstTry {

/**
 * @param args
 */
public static void main(String[] args) throws Exception{
    // Declare variables corresponding to names of i/o files
    String oldFilename = "";
    String newFilename = "";

    // Console input of i/o filenames
    BufferedReader cin;
    cin = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("What is the input text? Include path and .txt extension) ");
    oldFilename = cin.readLine();
    System.out.print(oldFilename);

    System.out.println("What is the output text? Include path and .txt extension) ");
    newFilename = cin.readLine();
    System.out.print(newFilename);

    cin.close();

    // Read text from old file,     
    BufferedReader fin;
    fin = new BufferedReader(new FileReader(oldFilename));

    while (fin.ready())
    {
      String lineFromFile;
      lineFromFile = fin.readLine();
      System.out.println(lineFromFile);

      if (lineFromFile.charAt(0) != '>' || lineFromFile.charAt(0) != '+')
      {

        PrintWriter fout;
        fout = new PrintWriter(new FileWriter(newFilename));
        fout.println('#' + lineFromFile);
        fout.close();
      }
    } // while

    fin.close();


    System.out.print("Text processing complete.");

}//main

}//public class

Upvotes: 0

Views: 2635

Answers (2)

supersam654
supersam654

Reputation: 3244

As @Patashu mentioned, lines may have 0 characters. This would cause lineFromFile to not have a character at position 0 because it's an empty string. I would change part of the while loop to:

String lineFromFile;
lineFromFile = fin.readLine();
System.out.println(lineFromFile);

if (lineFromFile.length() == 0) continue;

if (lineFromFile.charAt(0) != '>' || lineFromFile.charAt(0) != '+') {...

That line I inserted checks to see if the line is blank. If it is, skip to the next iteration of the loop which reads the next line from the file.

And as a complete side note, you might want to reconsider creating and closing a PrintWriter for (potentially) every line in the input file.

Upvotes: 0

Patashu
Patashu

Reputation: 21773

What if the first line of the file has 0 characters?

I think the syntax in java is lineFromFile.Length == 0. If this is true, you know this line is empty and want to handle it in a different way (making it a single # I imagine)

Upvotes: 0

Related Questions