Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 6th, 2005, 07:06 PM
nabil1983 nabil1983 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 36 nabil1983 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m 44 sec
Reputation Power: 14
Need help with java arrays!!

Hi all,
first timer novice doing a java program project and am stuck in places hope someone can help me out here..

Does neone know the code instruction to store an array of CD objects. Each entry in the array has to be a single object describing a CD.

i.e: Artist Name , Album Name, Number of tracks

i've written the program out but i cannot seem to get the program store each entry on its own and then display all the entries i have inputted,, instead it just displays the last entry i inputted....






import javax.swing.*;


public class CdStorage
{
public static void main (String[] args)

{
String menu_choice;
int menu;

menu_choice =
JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit");
menu = Integer.parseInt(menu_choice);

CdRecord[] array = new CdRecord[5];
array[0] = new CdRecord();

while (menu == 1)
{

array[0] . artist_name =
JOptionPane.showInputDialog("Enter Artist Name");

array[0] . album_name =
JOptionPane.showInputDialog("Enter Album Name");

array[0] . no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));

menu_choice =
JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit");
menu = Integer.parseInt(menu_choice);

// for (int i = 0; i < array.length; i++);


if (menu == 2)

array[0].printCdRecord();

//for (int i = 0; i < array.length; i++) {
//System.out.println(array[i]);}


if (menu == 3)
System.out.println();


}//end while
}//end main

} // end class CdStorage

class CdRecord
{
public String artist_name;
public String album_name;
public int no_of_tracks;


public CdRecord (String artist, String album, int tracks, int year)

{

artist_name = artist; //stores first argument passed into artist_name
album_name = album; //stores second argument passed into album_name
no_of_tracks = tracks; //stores third argument passed into no_of_tracks

}

public CdRecord()

{
artist_name = "A";
album_name = "B";
no_of_tracks = 0;
}

public void printCdRecord (array[])
{

int i = 0;

for(i=0;i<array.length;i++)
{
System.out.println(array[i]);
}

System.out.println();
}

}//end class cdstorage

Reply With Quote
  #2  
Old April 9th, 2005, 03:11 AM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m 17 sec
Reputation Power: 14
First of all, I must admit I'm just a java amateur writing java web applications.
So, Swing is a big unknown to me.
The fact that you're getting only the last CDRecord, doesn't surprise me.
Within your while loop, you're only working with the O-th element of the array.
You should keep track of the first free element of the array, using an integer
and changing its value as soon as you have another CDRecord.

Code:
 

array[0] = new CdRecord();

while (menu == 1)
{

array[0] . artist_name = 
JOptionPane.showInputDialog("Enter Artist Name");

array[0] . album_name = 
JOptionPane.showInputDialog("Enter Album Name");

array[0] . no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));

menu_choice = 
JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit"); 
menu = Integer.parseInt(menu_choice);


Reply With Quote
  #3  
Old April 9th, 2005, 09:30 PM
nabil1983 nabil1983 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 36 nabil1983 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m 44 sec
Reputation Power: 14
still cant get it

i've tried using the for loop to make the program move to the next array each time a entry
is made, but for some reason i cant seem to get the code right for this instruction..
Do you know the code for it???

Reply With Quote
  #4  
Old April 10th, 2005, 03:11 AM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m 17 sec
Reputation Power: 14
You cannot use the for loop because you don't know when the loop will stop,
or better said when the user will stop.

Therefor, stick with the while loop, but still use the integer i you were using
with the for loop. Write something like :

Code:
 CdRecord[] array = new CdRecord[5]; 
int i = 0; 

while (menu == 1 && i < 5)
{
array[i] = new CdRecord();

array[i] . artist_name = 
JOptionPane.showInputDialog("Enter Artist Name");

array[i] . album_name = 
JOptionPane.showInputDialog("Enter Album Name");

array[i] . no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));
i++;
(...)

In short, you should add the integer i you already used in the for loop.
This way , you keep track of the number of the element of the array you are
using. In the while loop you should check if the value of i is less than 5 :
the reason of this is that you've got only 5 elements in your array. And arrays
start from 0 : so element 0 till 4 is valid. Element 5 would mean the sixth
element in your array and you don't have space for that sixth element.

As soon as you have put the values of the user in your CDRecord, you add
1 to the value of i : this means you're going to the next element in your array.

Reply With Quote
  #5  
Old April 10th, 2005, 09:05 PM
nabil1983 nabil1983 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 36 nabil1983 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m 44 sec
Reputation Power: 14
Still getting error

Ok thanx for the info,, i understand wat u say.. but the
problem is after making the correct changes, i can enter
details but it still only prints out the last entry i input.....

when i pass the array into the print method like:

public void printCdRecord (array[])

i keeps saying identifier expected.. ne ideas y??

Last edited by nabil1983 : April 10th, 2005 at 09:14 PM. Reason: made changes to program

Reply With Quote
  #6  
Old April 11th, 2005, 12:29 PM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m 17 sec
Reputation Power: 14
You should add a public method to the class CDRecord.
This should be something like


Code:
 public void printInfo () 
{
 System.out.println(this.artist_name);
System.out.println(this.album_name);
...
}

You should call this method like this

Code:
 for(int i=0;i<array.length;i++)
{ 
CDRecord cdrec = (CDRecord) array[i]
cdrec.printInfo();
}

Sorry if it's not all correct (I suppose it isn't)
but I wanted to answer you today. Won't have time tomorrow.
Anyway : add a method to the class CDRecord and try to cast
every element of the array to an object of the class CDRecord
before you're displaying its info.

Reply With Quote
  #7  
Old April 11th, 2005, 05:02 PM
nabil1983 nabil1983 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 36 nabil1983 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m 44 sec
Reputation Power: 14
re:

Nope didnt work.
ne other suggestions neone please...

Reply With Quote
  #8  
Old May 3rd, 2005, 01:11 AM
tmike tmike is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 4 tmike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 46 sec
Reputation Power: 0
Quote:
Originally Posted by nabil1983

when i pass the array into the print method like:

public void printCdRecord (array[])

i keeps saying identifier expected.. ne ideas y??


because that declaration is malformed; do you mean...

public void printCdRecord(CdRecord[] array)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Need help with java arrays!!


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap