-4

I have this code:

    public static void main(String[] args) {
        try {

            String filePath = (args[0]);



            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String strLine = br.readLine();
            //Read File Line By Line and Print the content on the console
             PrintStream out = new PrintStream(new FileOutputStream( //printing output to user specified text file (command line argument: outputfile)
                    args[1]+".txt"));


            while ((strLine = br.readLine()) != null) {
            char [] words = strLine.toCharArray();
            System.out.println (strLine);
                Arrays.sort(words);
                out.println(words);
            }
            //close the streams
            br.close();
            }
            catch(IOException e){
            System.err.println("An IOException was caught, Re-open program and enter correct number of arguments :"+e.getMessage());
            }

    }


}

It does what I want it to do but it is skipping the first line of an input file.

Say for example the input file contained this list of words: bye red blue

it would output: edr belu

and skip printing: bey

sdyes
  • 1

1 Answers1

2

Change

String strLine = br.readLine();

to

String strLine = null;

The first line is consumed on the current line (and later discarded) in your current version, when while ((strLine = br.readLine()) != null) executes.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249