Results 1 to 10 of 10
  1. #1
    kingkongjaffa is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default Help with printing an arrayList. printing the first element twice

    So basicly guys whats happening is:
    1) i'm populating some elements with user input
    2)adding these elements to an ArrayList
    3)making the arrayList an array with the .toArray() method
    4) writing every element of the array to file.

    the problem: when I add one element at a time it works just fine

    when I add multiple elements it prints the first element twice in the text file then the next element

    content after run -add - close - run- add - close (one at a time)

    IdA , FirstnameA, SecondnameA
    IdB, FirstnameB, secondnameB

    content after run- add - add -close (adding multiple)
    IdA , FirstnameA, SecondnameA
    IdA , FirstnameA, SecondnameA
    IdB, FirstnameB, secondnameB

    here is my code
    Java Code:
    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();
            BList.add(ID+", "+fname+", "+sname);
            
            WriteCustomerstoFileFromArrayList();
    Java Code:
    private void WriteCustomerstoFileFromArrayList(){
           Object[] Obj =new Object[1];
           try {
             StartFile(Kudex.CUSTDATA);
             FileWriter outFile = new FileWriter(Kudex.CUSTDATA,true);
             PrintWriter out = new PrintWriter(outFile);
             Obj = BList.toArray();
             System.out.println(Obj.length);
             System.out.println(BList);
           
             
             for (int j=0; j<Obj.length; j++){
               out.println(Obj[j]);
               if (j<Obj.length-1) { out.println();}
           }
           Obj = null;
           out.close();
          }
          catch (IOException e){
             e.printStackTrace();
          }
       }
    Please help.

  2. #2
    kingkongjaffa is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    BUMPPPPPPP PLOX

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Bumped in 10 minutes? I wish you luck getting help -- you're going to need it.

    db

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by kingkongjaffa View Post
    BUMPPPPPPP PLOX
    Bumpy plox? I recommend going to the doctor for that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Your code has several printlns:
    Java Code:
     System.out.println(Obj.length);
             System.out.println(BList);
           
             
             for (int j=0; j<Obj.length; j++){
               out.println(Obj[j]);
    Which one(s) is printing what you posted?

    Add some id strings to show what line is printed by which println:
    Java Code:
     System.out.println("len=" + Obj.length);
             System.out.println("Blise=" + BList);
           
             
             for (int j=0; j<Obj.length; j++){
               out.println(j + " obj=" +  Obj[j]);

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Let me just start off with saying that converting your ArrayList to an array is not necessary at all. Look up enhanced for loops.

    You're not showing enough of your code so I can't help past there.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Java Code:
            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();
    You also don't need multiple Scanners. One will do the job.

  8. #8
    kingkongjaffa is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    Let me just start off with saying that converting your ArrayList to an array is not necessary at all. Look up enhanced for loops.

    You're not showing enough of your code so I can't help past there.
    How do I set up the enhanced for loop ?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by kingkongjaffa View Post
    How do I set up the enhanced for loop ?
    Oh no! Google is broken again.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    Object[] Obj =new Object[1];
    An array to hold one element is totally pointless.

Similar Threads

  1. About printing Arraylist on JSP page....
    By vaibhavspawar in forum Advanced Java
    Replies: 0
    Last Post: 08-13-2010, 06:52 AM
  2. Replies: 19
    Last Post: 08-10-2010, 08:36 AM
  3. ArrayList printing
    By tommyyyy in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 04:33 AM
  4. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM

Posting Permissions

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