Results 1 to 2 of 2
  1. #1
    kingkongjaffa is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default 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:
    Java Code:
    public int GetNextID(){
        Entity IDX = new Entity();
                I = new Scanner (new File(Kudex.IDDATA));
            } catch (Exception e){
            while (I.hasNext()){
                IDX.ID = I.nextInt();
            return ++IDX.ID; } //opens and increments the ID number
    public void savenewid(){   
                IDWriter = new Formatter (Kudex.IDDATA);
                IDWriter.format("%d",ID );
                IDWriter.close(); //update unique ID file
             catch(Exception e) {
        } // 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 (;
          fname  = firstname.nextLine();
          System.out.println("please enter your second name");
          Scanner secondname = new Scanner (;
          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;
        }//populates the next available element in the array with the user input
    public void WriteCustomersToFile(){
           /*try {
           catch (IllegalArgumentException e){
          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 );
                catch (IOException e){
       }//writes the contents of the array to customer record file
    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:
    Any Suggestions are appreciated

    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    the first time I try to write to an element I get the first elements contents written in 2 elements.
    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.
    Be sure to print out the index and the data.

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. Lo4j issue - logs are written in a wrong file
    By Dhamo in forum Advanced Java
    Replies: 0
    Last Post: 05-09-2010, 09:11 PM
  4. Replies: 4
    Last Post: 12-04-2008, 12:07 AM
  5. Text file over written
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-11-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts