dav191
dav191

Reputation: 75

How to make the program read the characters in a line in the file and jump into the next line if a certain character count is reached?

I have a text file which the program will right data to. I want to give a maximum character count to a line and ensure that a line will have only that amount of characters. (So it should automatically switch to the next line when the character count has reached)

//my code

 public static void main(String[] args) {

    // The name of the file to open.
    String fileName = "file.txt";
    int counter = 0;

    // This will reference one line at a time
    String line = null;
    FileReader fileReader = null;
    int chars = 0;
    int max = 55;
    try {
        // FileReader reads text files in the default encoding.
        fileReader
                = new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader
                = new BufferedReader(fileReader);

        while ((line = bufferedReader.readLine()) != null) {
            counter++;
            if (counter == 1) {
                chars += line.length();
                System.out.println(chars);
                System.out.println(line);
                if (chars == max) {
                //if max characters reached jump to the 7th line
                    counter += 6;
                }
                System.out.println(counter);
            }
        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

}

How can i modify this code so it will automatically jump into the 7th line when the maximum character count (55) has reached?

Upvotes: 1

Views: 52

Answers (1)

D3181
D3181

Reputation: 2092

I think you already had the correct idea by keeping track of your current position but it may be easier to just create a substring if your current line exceeds your desired size rather than keeping track of how many characters have been read so far :

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;


public class StackQuestions {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileInputStream fstream = null;
        int max = 55;
        int desiredIndex = 0; //the line number you want to reach
        int currentIndex=0; //your current line number

        try {
            // TODO code application logic here
            fstream = new FileInputStream("file.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;

            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console if the string length is larger than the max, after creatng a substring
                if(strLine.length()> max && currentIndex==desiredIndex){
                strLine=strLine.substring(0, max);
                desiredIndex=currentIndex+6;
                System.out.println(strLine);
                }

                currentIndex++;
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StackQuestions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StackQuestions.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fstream.close();
            } catch (IOException ex) {
                Logger.getLogger(StackQuestions.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

}

Anyway hope this helps a bit if not lemme know and i will try to help :) good luck on your programming

Upvotes: 2

Related Questions