Ok i've understood the use of arrays and most of it.... but my problem is no matter wat i do my program still only displays the last entry i make and not all.

i've tried a for loop in the print method but no luck. Neone know what im doing wrong.
below is my new modified program::

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();

    int i = 0;
    while (menu == 1 && i < 5)
    {

      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 = i++;
      menu_choice = 
        JOptionPane.showInputDialog("Enter:\n 1: New CD entry\n 2: Print\n 3: Quit"); 
      menu = Integer.parseInt(menu_choice);

      if (menu == 2)

        array[i].printCdRecord();
    }			
    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 ()
  {
    System.out.println("Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks);
  }
}//end class cdstorage

Code reformatted and tags added. -Narue

please if someone can let me know my problem id apreciate it!!!!

Recommended Answers

All 5 Replies

you didn't increase "i" so its always the first (0) element in the array, so you need to increase it
also you only define first element of array... so it will be problem if you increase "i",
you need to reconsider your approch to the program,
here is an advice : first create an cdrecord with the user input, then assign it to the first empty place in array in order to this you need to keep your cdrecord size.
also try to change your "while" loop to "do-while" loop...

in the while loop i have increased the i just after the user input instructions
i put in ' i = i++' i know this works because when i never had in b4 i could nt add more than one entry......

my program has to have a while loop as it is part of my criteria for marking..
i tried adding a printinfo method but had no luck ne one got ne suggestions
please..... i got like untill next week to hand in....

yep, i see, but

i++; //same as i=i++;

but still your approch is wrong, i remember i write something about public and private members of classes, you need to make properties(fields) private...

in the while loop i have increased the i just after the user input instructions
i put in ' i = i++' i know this works because when i never had in b4 i could nt add more than one entry......

i = i++; does nothing in Java.
It effectively translates to something like this:

int j = i+1;
int k = i;
i = j;
i = k;

I know in SOME C and C++ dialects i=i++; is equivalent to i++; or i=i+1; but in Java this is not the case (remember that in C/C++ the behaviour of the ++ operator is undefined!).

use i++; instead of i=i++;

i = i++'

this works in a weird way
whenever you do i++ it always evaluates the expression as a whole, assigns the appropiate value, and then increments one

so if you were to say
i=0;
j=i
j=i++;
then j should equal 0
if you are confused and you coulda swore that j should equal one it's because the i++ doesn't happen until after the value is assigned to j. If you want to incremement i before assigning it to j, then use
++i

i know that seems weird, but when you say i = i++ what you reall are telling the computer to do is:

i = i;
i++;

when you say i = ++i;
that means
i++;
i = i;

but in either case dont do this!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.