I'm trying to create 10 files with random names and random data.
Each line in the file consists of two integers and represents one "record".
Naming the files and generating random data didn't turn out very difficult.
But for some reason I fail to recognize, while I create 100 records for each file, instead of that each file ends up with 100, or 400, or 1000, or I don't know how many records..I would really appreciate any help!Thanks in advance
here's the code:

public class Comparisons {
	public static void main(String args[]){
		Random dice = new Random();
		String data = "";
		int size [] = new int [100];
		int [] time = new int[100];
		for(int i = 0; i < 100; i++){
			size[i] = time[i] = 0;
		}
		ArrayList<Integer> fileNames = new ArrayList<Integer>();
		
		for (int i = 0; i < 50; i++) {
	             fileNames.add(i);
	        }
		Collections.shuffle(fileNames);

		for(int j = 0; j < 10; j++){
			String name = Integer.toString(fileNames.get(j));
			
			Formatter file = null;
			try{
				file = new Formatter("%s.txt".format("%s.txt",name) );
			}
			catch(Exception e){
				e.toString();
			}
			
			for(int i = 0; i < 100;i++){
				if(i == 0){
					time[i] = dice.nextInt(2);
					size[i] = dice.nextInt(20);
				}
				else{
					time[i] = time[i-1] + dice.nextInt(2);
					size[i] = dice.nextInt(20);
				}
				data += time[i] + " " + size[i] +"\n";
			}//end for 
			try{
				file.format("%s", data);
			}
			catch(Exception e){
				e.toString();
			}
			try{
				file.close();
			}
			catch(Exception e){
				e.toString();
			}
		}//end for 
		

	}//end of main
}

I fail to recognize, while I create 100 records for each file,

How are you keeping track of the number of records written to a file?
I don't see any comments in the code describing what it is supposed to do.

Some suggestions for debugging your code.
Replace the hardcoded magic numbers 100 and 10 with variables and define them at the top.
Use reasonable values for testing like 3 and 5.

final static int NbrFiles = 3;  
   final static int NbrRecords = 5;

   ...
		int size [] = new int [NbrRecords];
		int [] time = new int[NbrRecords];
                   ...
	for(int i = 0; i < NbrRecords; i++){
                      ...
		for(int j = 0; j < NbrFiles; j++){

Add some printlns to print out the names of the files as they are printed and print out the variable that is written to the file.

each record is created in the for loop on line 28 and stored temporarily in the data variable(line 37).

omg it was a really stupid mistake, i didn't clear the data variable after each loop so it maintained all the previous records..:/

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.