| Author |
Trouble printing a filled array
|
Steve Parsons
Greenhorn
Joined: Aug 17, 2009
Posts: 2
|
|
Hello,
I'm having trouble printing filled elements of the array. The user fills in CD information in text fields and every time they click "Update" it is supposed to record the input to the console. I am using a GUI which is predefined with 100 elements. But in case I only fill out so many, like 10, I only want to print those elements. As it stands now, it just prints out what the user inputs 100 times in the console window!
public void actionPerformed(ActionEvent e)
{
CD[] band = new CD[100];
for (int i = 0; i < band.length; i++)
{
String bandName = bandName.getText();
String albumName = albumName.getText();
band[i] = new CD(bandName, albumName);
System.out.println(band[i]);
}
}
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
Please put your code between the code tags!
You are iterating over the band array, which has 100 elements. With each iteration you are getting the values of the same variable?
Is there a flaw in your design?
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Steve Parsons
Greenhorn
Joined: Aug 17, 2009
Posts: 2
|
|
Sorry, this is my first time on the site.
Is there a way to print only the elements that were added to the array?
|
 |
Harry Graham
Greenhorn
Joined: Feb 18, 2009
Posts: 10
|
|
instead of using 'for' have you tried using 'while'. I think I read that for is used when you know how many times you want something done and while is used when you do not.
Take what I have to say with a grain of salt though, I am on my 4th attempt to complete my first assignment.
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
use an arraylist and try it out. Also in the gui are you going to the next line to get the next text?
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
|
|
Steve Parsons, welcome to coderanch
First of all the program you have written is in complete.
next i feel that in the iterations, you are just assigning the same value over and over again to all elements in your array:
its Equivalent to:
which is equivalent to:
using for loop or while loop has nothing to do here.
shouldnt bandName and albumName be changed every time the loop is executed ?
|
My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
|
 |
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
|
|
If you are indeed trying to use a sparsely filled array,
1. trying null check
2. use a collection such as an ArrayList
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
Also there is no concrete specification that how from the textarea in the gui, the nextline is read. As far as this code goes, it doesn't advance to the nextline just taking the same line again and again and storing it in the array. Also while advancing only, we have to check for null.
|
 |
 |
|
|
subject: Trouble printing a filled array
|
|
|