Results 1 to 10 of 10
- 08-18-2011, 08:20 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
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(); } }
- 08-18-2011, 08:30 PM #2
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
BUMPPPPPPP PLOX
- 08-18-2011, 09:17 PM #3
Bumped in 10 minutes? I wish you luck getting help -- you're going to need it.
db
- 08-18-2011, 09:30 PM #4How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-18-2011, 09:34 PM #5
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]);
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]);
- 08-18-2011, 10:23 PM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 9
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.
- 08-18-2011, 10:36 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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();
- 08-18-2011, 10:55 PM #8
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
- 08-19-2011, 01:57 AM #9
- 08-19-2011, 01:59 AM #10
Similar Threads
-
About printing Arraylist on JSP page....
By vaibhavspawar in forum Advanced JavaReplies: 0Last Post: 08-13-2010, 07:52 AM -
Printing contents of Arraylist -- strange issue
By YeeP in forum New To JavaReplies: 19Last Post: 08-10-2010, 09:36 AM -
ArrayList printing
By tommyyyy in forum New To JavaReplies: 4Last Post: 03-20-2009, 05:33 AM -
Help printing specific ArrayList elements
By CirKuT in forum New To JavaReplies: 5Last Post: 02-03-2009, 01:24 AM
Bookmarks