Wednesday, April 28, 2010

[Java] Facing issue with BufferedWriter

 

Hello,

I'm facing an issue while playing with BufferedWriter: got a loop with ten
iterations. Debugging through the console via System.out.println, I can see
the 10 lines it was supposed to display. But when I call the
BufferedWriter's write() method, the last line isn't written.

I'm not sure where the problem is. Can anyone help me with a workaround?

Current Output:

> Case #1: 2 3

Case #2: 1 4

Case #3: 4 5

Case #4: 29 46

Case #5: 11 56

Case #6: 4 5

Case #7: 40 46

Case #8: 16 35

Case #9: 55 74

Desired Output:

> Case #1: 2 3

Case #2: 1 4

Case #3: 4 5

Case #4: 29 46

Case #5: 11 56

Case #6: 4 5

Case #7: 40 46

Case #8: 16 35

Case #9: 55 74

Case #10: 7 9

import java.io.BufferedReader;
>
> import java.io.BufferedWriter;
>
> import java.io.File;
>
> import java.io.FileReader;
>
> import java.io.FileWriter;
>
> import java.io.IOException;
>
> import java.util.ArrayList;
>
>
>>
>> public class A1 {
>
> static int count=1;
>
> public static void main(String[] args) {
>
> new A1().start(args[0]);
>
> }
>
> public void start(String s){
>
> try {
>
> File f = new File(s);
>
> FileReader fr;
>
> fr = new FileReader(f);
>
> BufferedReader br = new BufferedReader(fr);
>
> int cases = Integer.parseInt(br.readLine());
>
> File fw = new File("output.txt");
>
> f.createNewFile();
>
> FileWriter fwr = new FileWriter(fw);
>
> BufferedWriter bw = new BufferedWriter(fwr);
>
> ArrayList<Integer> items = new ArrayList<Integer>();
>
> ArrayList<String> result = new ArrayList<String>();
>
> for (int i = 0; i<cases; i++){
>
> items.clear();
>
> int money = Integer.parseInt(br.readLine());
>
> Integer.parseInt(br.readLine()); // itemNum
>
> for (String r: br.readLine().split("\\s+")) {
>
> try{
>
> items.add(Integer.parseInt(r));
>
> } catch (Exception e) {;
>
> e.printStackTrace();
>
> }
>
> }
>
> result.add((doCalc(money,items)));
>
> }
>
> fr.close();
>
> for (String x: result){
>
> bw.flush();
>
> bw.write(x);
>
> }
>
>
>> } catch (IOException e) {
>
> e.printStackTrace();
>
> }
>
> }
>
>
>> public String doCalc(int c, ArrayList<Integer> a){
>
> String msg = "";
>
> for(int i=0;i<a.size();i++) {
>
> for(int j=i+1;j<a.size();j++){
>
> if ((a.get(i) + a.get(j))== c){
>
> msg += "Case #" + count + ": " + (i+1) + " " + (j+1) + "\n";
>
> // System.out.println("Case #" + count + ": " + (i+1) + " " + (j+1));
>
> count++;
>
> break;
>
> }
>
> }
>
> if (!msg.isEmpty())
>
> break;
>
> }
>
> return msg;
>
> }
>
> }
>
>
>
First lines of the input file:

10
>
> 100
>
> 3
>
> 5 75 25
>
> 200
>
> 7
>
> 150 24 79 50 88 345 3
>
> 8
>
> 8
>
> 2 1 9 4 4 56 90 3
>
>

Chris

[Non-text portions of this message have been removed]

__._,_.___
Recent Activity:
Visit http://aiaiai.com or http://jgame.org for more groups to join.
Java Official Group is created for the following topics: Java 2 Enterprise Edition - J2EE, Java 2 Standard Edition - J2SE, Java 2 Micro Edition - J2ME, XML, XSL, XSD, XPATH, Web Services, Jini, JXTA for all type of Java Geeks.
Whoever posts spam / ads / job related message will be BANNED IMMEDIATELY
.

__,_._,___

No comments:

Post a Comment