-1

I read the content of one file into String(char[]) content:

8264,1
28462,1
15836,1

Then I want to multiply second column by 5 and append another file with processed values:

8264,5
28462,5
15836,5

The problem is with iterating over content. If I iterate as shown below, then obviously I get chars like 8, 2, 6, 4, etc., but not lines 8264,5.

FileWriter fw = new FileWriter(filew.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
StringBuilder sb = new StringBuilder();
for (int k = 0; k < content.length(); k++) 
{
    char c = content.charAt(k);
    //multiply second column by 5
} 
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

1 Answers1

0

If you want to iterate line by line I would recommend a Scanner.

PrintWriter pw = new PrintWriter(...file/stream...);
Scanner sc = new Scanner(...your file/stream/string...);
while (sc.hasNextLine()) {
    String line = sc.nextLine();
    String[] parts = line.split(",");
    int first = Integer.parseInt(parts[0]);
    int second = Integer.parseInt(parts[1]);
    second *= 5;
    pw.println(first + "," + second);
}
sc.close();
pw.close();