• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble printing a filled array  RSS feed

 
Steve Parsons
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steve Parsons
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use an arraylist and try it out. Also in the gui are you going to the next line to get the next text?
 
salvin francis
Bartender
Posts: 1663
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
salvin francis
Bartender
Posts: 1663
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!