ok, so i have an int[][] that i need to be printed out into a file. it needs to be printed line by line with each number seperated by a comma.

how does this look? im not getting any output...

import java.io.*;
import java.util.*;

public class TestingII {
	
	
	public static void main(String[] args){
		int[][] data = {{1,2,3,4},
						{5,6,7,8},
						{4,9,1,0},
						{2,5,8,3}};
		Scanner stdIn = new Scanner(System.in);
		PrintWriter writer;
	 
		try
		{
			System.out.print("Enter a filename: ");
			writer = new PrintWriter(stdIn.nextLine());
			for (int i = 0; i<data.length; i++){
	        	      for(int j = 0; j<data.length; j++){
	        		    writer.print(data[i][j] + ",");
	        	      }writer.println();
			}	
		}
		catch (FileNotFoundException e){
			System.out.println("Error: " + e.getMessage());			
		}
	}
}

Recommended Answers

All 3 Replies

The idea is not to get output, since all it does is save to a file.
By the way you have a mistake. This is the correct one:

for (int i = 0; i<data.length; i++){
	        	      for(int j = 0; j<data[B][i][/B].length; j++){
    ....
    }
}

Also try to close the PrinterWriter at the end:

import java.io.*;
import java.util.*;

public class TestingII {
	
	
	public static void main(String[] args){
		int[][] data = {{1,2,3,4},
						{5,6,7,8},
						{4,9,1,0},
						{2,5,8,3}};
		Scanner stdIn = new Scanner(System.in);
		PrintWriter writer;
	 
		try
		{
			System.out.print("Enter a filename: ");
			writer = new PrintWriter(stdIn.nextLine());
			for (int i = 0; i<data.length; i++){
	        	      for(int j = 0; j<data[i].length; j++){
	        		    writer.print(data[i][j] + ",");
	        	      }writer.println();
			}	
		}
		catch (FileNotFoundException e){
			System.out.println("Error: " + e.getMessage());			
		} [B]finally[/B] {
                          try{if (writer!=null) writer.close(); } 
                         catch (Exception e) {System.out.println("Could not close writer");}
                }
	}
}

Also I don't know the API of the PrintWriter but I assume that it takes as argument a String, which the way you have it

commented: thanks for the help! +0

when i meant by no output was there was nothing in the destination file.

but when i put that finally statement into the end, it worked!! thanks a ton.

when i meant by no output was there was nothing in the destination file.

but when i put that finally statement into the end, it worked!! thanks a ton.

So it seems that the problem was that you weren't closing the writer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.