Results 1 to 2 of 2
- 08-16-2011, 12:04 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
First Element of Array being written to file twice Help
So I have a few methods: one for retrieving the and incrementing the ID, one for saving the incremented ID , one for Adding the users input to properties and assigning the object to an element of the array, and one for writing the array to a File.
now my problem lies in that the first time i run the program and write to file it writes the first objects properties to 2 elements of the array
This is my code:
And this is the order the methods run in : these are running in another class hence the cust. (object) this is not the error this merely illustrates the order they are running in if that is of any relevance.Java Code:public int GetNextID(){ Entity IDX = new Entity(); try{ I = new Scanner (new File(Kudex.IDDATA)); } catch (Exception e){ e.printStackTrace(); } while (I.hasNext()){ IDX.ID = I.nextInt(); } return ++IDX.ID; } //opens and increments the ID number public void savenewid(){ try{ IDWriter = new Formatter (Kudex.IDDATA); IDWriter.format("%d",ID ); IDWriter.close(); //update unique ID file } catch(Exception e) { e.printStackTrace(); } } // saves next unique ID public void AddingCustomer(){ System.out.println("Please enter your customer details:"); System.out.println("Please enter your first name:"); Scanner firstname = new Scanner (System.in); fname = firstname.nextLine(); System.out.println("please enter your second name"); Scanner secondname = new Scanner (System.in); sname = secondname.nextLine(); ID = GetNextID(); //populate object properties for (int i = 0 ; i<custArray.length; i++){ if(custArray[i]== null){ custArray[i]= new Customer(); custArray[i].ID = ID; custArray[i].fname = fname; custArray[i].sname = sname; break; } } }//populates the next available element in the array with the user input public void WriteCustomersToFile(){ /*try { ClearFile(Kudex.CUSTDATA); } catch (IllegalArgumentException e){ e.printStackTrace(); } */ for (int i = 0 ; i<custArray.length; i++){ try { FileWriter outFile = new FileWriter(Kudex.CUSTDATA,true); PrintWriter out = new PrintWriter(outFile); if (custArray[i] != null){ String s = String.format("%d, %s, %s", custArray[i].ID , custArray[i].fname , custArray[i].sname ); out.println(s); } out.close(); } catch (IOException e){ e.printStackTrace(); } } }//writes the contents of the array to customer record file
Any Suggestions are appreciatedJava Code:cust.GetNextID(); cust.AddingCustomer(); cust.WriteCustomersToFile(); cust.savenewid();
note: the program runs fine and generates the unique ID as expected and multiple customers can be added however the first time I try to write to an element I get the first elements contents written in 2 elements.
- 08-16-2011, 12:10 AM #2
Try adding some debugging code to see where it is happening. Print out the contents of what is added each time it is added to the array to see where the problem is.the first time I try to write to an element I get the first elements contents written in 2 elements.
Be sure to print out the index and the data.
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Lo4j issue - logs are written in a wrong file
By Dhamo in forum Advanced JavaReplies: 0Last Post: 05-09-2010, 09:11 PM -
Sequential Access Problem - Nothing is being written to file.
By shelzmike in forum New To JavaReplies: 4Last Post: 12-03-2008, 11:07 PM -
Text file over written
By bugger in forum New To JavaReplies: 2Last Post: 11-11-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks