Binary File

Write test scores to a binary file.

Page 1 of 1

2 Replies - 992 Views - Last Post: 18 December 2009 - 02:06 PM Rate Topic: -----

#1 skittlebud   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-April 09

Binary File

Posted 18 December 2009 - 05:43 AM

I need to write validated numbers to a binary file. I use an array to gather the numbers. Numbers have to be greater than one and less than 100, as shown below. This is not a problem at all, but now I need to store them in a binary file.

 
//When user enters 0
 	 	while (array[i] > -1 && array[i] < 1)		
 	 		{
 	 		input = JOptionPane.showInputDialog("Enter a non-zero, positive number.");
 	 		array[i] = Double.parseDouble(input);
 	 		}
 	 		
 	 	//When user enters number greater than 100
 	 	while (array[i] > 100)
 	 	{
 	 		input = JOptionPane.showInputDialog("Enter a number less than 100." + 
 	 										  "\nEnter a score for test number " +(i + 1)	+ ".");
 	 		array[i]  = Double.parseDouble(input);
 	 	}
 	 	
 	 	if (array[i] < 100 && array[i] > 0 )
 	 	{
 	 	System.out.println("You have entered " + array[i] + ".");
 	 	}



This is my starting code for putting data into a binary file.
	 /**
 	  * Write the validated numbers to a binary file.
 	  */
 	  
 	 	//Create the binary output objects.
 	 	FileOutputStream fstream = new FileOutputStream("array[i]");
 	 	DataOutputStream outputFile = new DataOutputStream(fstream);
 	 		
 	 	//Write the array elements to the file.
 	 	for (int i = 2; i < array.length; i++)
 	 		outputFile.writeDouble(array[i]);
 	 	
 	 	System.out.println("Completed writing file to system.");
 	 	
 	 	//Close the file.
 	 	outputFile.close();



The one error that I have gotten is " i is already defined in getValues(double[])", and I do not know how to fix that exactly. What am I doing wrong, and how do I fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: Binary File

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3604
  • View blog
  • Posts: 16,497
  • Joined: 20-September 08

Re: Binary File

Posted 18 December 2009 - 06:02 AM

Difficult to see without more code but try a different counter variable name in the for loop
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Binary File

Posted 18 December 2009 - 02:06 PM

or do, if i has already been defined and want to use it

for (i = 2; i < array.length; i++)

instead of

for (int i = 2; i < array.length; i++)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1