Home > other >  When there are multiple lines in my file, my code works with out any error but when there's a s
When there are multiple lines in my file, my code works with out any error but when there's a s

Time:03-22

My input text file is :-

kiShore,kUMar,bhAvanam                                                                                                         

My output file must be:-

Kishore,Kumar,Reddy

Simply my code needs to capitalize the 1st letters in file and This is my code

 public static void main(String[] args) throws Exception {
        // write your code here
        BufferedReader br = new BufferedReader(new FileReader("input.txt"));
        FileWriter fw = new FileWriter("output.txt");
        String line;
        while ((line = br.readLine()) != null) {
            String[] values = line.split(",");
            for (String str : values) {
                String msg = str.substring(0, 1).toUpperCase()   str.substring(1).toLowerCase();
                br.mark(0);
                if (br.read()!= -1)
                {
                    msg=msg ",";
                    br.reset();
                }
                fw.write(msg);
            }
        }
        fw.close();
        br.close();
    }

My code Works perfectly when file contains multiple lines like:

kIshore,kUMar
rEdDy

i get the exact output

Kishore,Kumar,Reddy

but if my Input file contains only a single line like:

kiShore,kUMar,bhAvanam

I get the output KishoreKumarBhavanam. I'm unable to insert commas in between words, if the input file contains only a single line

Expected result:

 Kishore,kumar,Bhavanam

actual result:

KishoreKumarReddy

CodePudding user response:

As per your code, the problem is you are only adding comma, when your buffered reader can read next character. But, the problem is since you have already read the whole line, there is no additional data to be read. Now, since there is nothing to read, the br.read() function call would return -1 and comma would not be added into your output.

You may observe the same behavior for multi-line file as well, where for the last line of input, the commas are not added.

To fix this, instead on relying on br.read() function, use index while looping and add comma, incase the item is not the last element in the list or use StringJoiner

CodePudding user response:

I think you could do something like this:

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringJoiner;

// ...

InputStream inputStream = ParseFile.class.getResourceAsStream("/input.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
FileWriter fw = new FileWriter("output.txt");
String line;
StringJoiner stringJoiner = new StringJoiner(",");
while ((line = br.readLine()) != null) {
    String[] values = line.split(",");

    for (String str : values) {
        stringJoiner.add(str.trim());
    }
}
fw.write(stringJoiner.toString());
fw.close();
br.close();
  • Related