0

So I am having troubles printing to output. I understand the concept, but when it comes to this problem its kinda weird. I've tried different print lines and all of them give me different results from the console window. I'm still trying different things, but im starting to run out of ideas. Thanks and much appreciated !

This is what I want the expected output to be.

1
1, 2, 3, 4

When I try println it does this for output.println(data[0]);

1
, 2, 3, 4

when I do a regular print it does this

1, 2, 3, 4  

This is the text file print method`

    public class JavaApplication1 {

  static int[] Array(int[] data) {

      int size = 1;
      if (data !=null) {
          size = 1 +data.length;
      }
      return new int [size];
  }

  private static int[] addToArray(int[] data, int x) {
      int[] array2 = Array(data);
      if(data !=null) {
          System.arraycopy(data,0,array2,0,data.length);
      }
      array2[array2.length - 1] = x;
      return array2;
  }
  private int[] data;

  public JavaApplication1 (int[] data, int x) {
      this.data = addToArray(data, x);      
  }
  public void printall() {
      System.out.print(data[0]);
      for (int i = 1; i < data.length; i++) {
          System.out.printf(", %d", data[i]);

      }
      System.out.println();
  } public void text() {
    try {
           PrintWriter output =  new PrintWriter("test.txt");
           output.print(data[0]);
           for (int i = 1; i < data.length; i++) {
               output.printf( ", %d", data[i]);
               output.flush();
           }
    } catch (Exception ex) {

    }


  }


    public static void main(String[] args) {
        int[] in = {1,2,3};
        int[] test = {1,2,3};

        int l = 4;
        int x = 4;

        JavaApplication1 a = new JavaApplication1(null, 1);
        a.printall();
        JavaApplication1 b = new JavaApplication1(in, x);
        b.printall();


        JavaApplication1 c = new JavaApplication1(null, 1);
        c.text();
        JavaApplication1 d = new JavaApplication1(test, l);
        d.text();




    }

}
madauewo
  • 61
  • 2
  • 7

5 Answers5

0

Try this:

PrintWriter output =  new PrintWriter("test.txt");
output.println(data[0]);      //this is where you call the println
for (int i = 0; i < data.length; i++) {
    output.printf( ", %d", data[i]);
    output.flush();
}
The Well
  • 876
  • 1
  • 9
  • 22
0

try do in this way

public void text() {
    try {
       PrintWriter output =  new PrintWriter("test.txt");

       for (int i = 0; i < data.length; i++) {
           if(i > 0){
               output.print(",");
           }

           output.print(data[i]);
           output.flush();     
       }   
    } catch (Exception ex) {
    }
}
Yu Yenkan
  • 745
  • 1
  • 9
  • 32
0

Try this :

public void text() {
        try {
            PrintWriter output = new PrintWriter("test.txt");
            output.println(data[0]);
            for (int i = 0; i < data.length; i++) {
                if (i != (data.length - 1)) {
                    output.printf("%d, ", data[i]);
                } else {
                    output.printf("%d", data[i]);
                }
                output.flush();
            }
        } catch (Exception ex) {

        }

    }
Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42
  • @madauewo, what is the expected output of your code, if you replace values of test to `int[] test = {4,5,6};`? – Naman Gala May 19 '15 at 06:42
0

To make it a new line for each output loop it and add "\n" to each line you write or you can convert to string and edit String format to whatever you want. You can add a comma if you want

string.format()
0x2B
  • 91
  • 9
0

Cause of problem: In your code, everytime you call text(), you are replacing existing file, without updating or appending new data to it.

Refer this SO question. You can write printWriter as

PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("test.txt", true)));

I have tested on local, Try this code.

public void text() {
    try {
        // Adding data to existing file without replacing it.
        PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("test.txt", true)));
        output.print(data[0]);
        for (int i = 1; i < data.length; i++) {
            output.printf(", %d", data[i]);
        }
        output.print("\n"); // Added next line in the file
        output.flush();
        output.close(); // closing PrintWriter.
    } catch (Exception ex) {
        // log your exception.
    }
}
Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55