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();
}
}