Results 1 to 7 of 7

Thread: Arrays

  1. #1
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Question Arrays

    Hello everyone

    So i'm kinda new at java (been learning it for only 2 weeks)

    So basically, my probleme is that i want to give the user a possibility to somehow change the limitation of the array.
    Now i know that it is not possible, as an array is "final", but it's hard to believe that there is no way to change it.

    An idea came across my mind though:

    Renaming the String for the array.

    Example: i have the String abcx, where x would be an integer.

    i would then have x=JOptionPane.showInputDialog("Enter a value");
    and have abcx be renamed to whatever value the user enters
    so if he enter 1, it would be named abc1, if he enters 2, it would be named abc2 etc…

    both of my problems are array related, as i want to change the limit of the array
    thanks
    Last edited by jiffi; 10-12-2011 at 01:38 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Arrays

    as i want to change the limit of the array
    Use an implementation of the List interface, such as an ArrayList:
    ArrayList (Java Platform SE 6)

  3. #3
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Arrays

    Thank you for trying to help, but like I said this is only my second week of java and your link is far beyond my understanding

    Anyways, I might have found a way to do it that sounds easier
    Last edited by jiffi; 10-12-2011 at 05:49 PM. Reason: code removed

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Arrays

    Quote Originally Posted by jiffi View Post
    Thank you for trying to help, but like I said this is only my second week of java and your link is far beyond my understanding
    Did you make an attempt to do any further research? We shouldn't be a substitute for using a resource such as google:
    java how to use arraylist - Google Search

    Edit: and BTW, please use the code tags - the asterisks in the code above make it that much less readable that I'm not even going to try and wade through it.
    Last edited by doWhile; 10-12-2011 at 05:41 PM.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Arrays

    When posting code, please don't add asterisks to show spacing. Instead type it and format it in an editor, and then copy and paste it into code tags. [code] YOUR CODE HERE [/code]

  6. #6
    SFNYC is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default Re: Arrays

    Please go through the online tutorials to help you better understand ArrayList

    The List Interface (The Java™ Tutorials > Collections > Interfaces)

    The Java™ Tutorials

  7. #7
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Arrays

    ok thanks (and i did not put the stars, it would have taken me forever)

    Java Code:
    import javax.swing.*;
    class MieTunes
    {
        public static void main (String [] args)
        {
            System.out.print('\f');
            int choose = 0;//Choosing the option in the menu
            int x = 0;//Counting the number of songs input
            int quit = 0;//quit
            int counter = 0;//counter
            int nTotal = 0;//number of total songs
            int tTotal = 0;// saving the number of total songs
            int remove = 0;// remove songs
    
            JOptionPane.showMessageDialog(null, "Hello. Welcome to MieTunes.\nHere, you can manage your songs");//welcome message
            nTotal = Integer.parseInt(JOptionPane.showInputDialog("How many total songs would you like to enter?\n(This can be changed later)"));//total number of songs
    
            String [] song = new String [nTotal];//name of the song
            String [] artist = new String [nTotal];// name of the artist
            String [] genre = new String [nTotal];// genre of the song
    
            while(true)
            {
                String menu = "Menu:\n\n[1] Add\n[2] List\n[3] Search\n[4] Delete\n[5] Add/Remove extra space\n[6] Exit";
    
                if (x >= nTotal)
                {
                    menu = menu.replaceFirst( "Add" , "Not enough Space" ); 
                }
    
                while(true)//error loop
                {
    
                    choose = Integer.parseInt(JOptionPane.showInputDialog(menu));//menu
                    if (choose < 1 || choose > 6)
                    {
                        JOptionPane.showMessageDialog(null, "Incorrect Input");
                    }
                    else
                    {
                        break;
                    }
    
                }
    
                if (choose ==1)//add songs
                {
                    if (x >= nTotal)//array limit
                    {
                        choose = JOptionPane.showConfirmDialog(null,"You have reached the total number of songs. Would you like to add extra space?", " ", JOptionPane.YES_NO_OPTION);
                        if(choose == JOptionPane.YES_OPTION)
                        {
                            choose = 5;
                        }
                    }
                    if (x < nTotal)//adding songs...
                    {
                        System.out.print('\f');
                        song[x] = JOptionPane.showInputDialog("Enter the name of the song");
                        artist[x] = JOptionPane.showInputDialog("Enter the name of the artist");
                        genre[x] = JOptionPane.showInputDialog("Enter the genre");
                        x ++;
                    }
    
                }
                if (choose ==2)//show list
                {
                    System.out.print('\f');
                    for (counter=0;counter<x;counter++)
                    {
                        if (song[counter] != null && artist[counter] != null && genre[counter] != null)
                        {
                            String output = "\nTitle: " + song[counter] +"\nArtist: " + artist[counter] + "\nGenre: " + genre[counter];
                            
                            System.out.println(output);
                        }
                    }
                }
                if (choose == 3)
                {
                    JOptionPane.showMessageDialog(null,"Feature not yet available");
                }
                if (choose == 4)
                {
                    JOptionPane.showMessageDialog(null,"Feature not yet available");
                }
                if (choose ==5)//add more space
                {
                    remove = 0;
                    JOptionPane.showMessageDialog(null, "Here is your current limit of songs: " + song.length + ".\nYou still have space for " + (song.length - x) + " more songs.");
                    tTotal = nTotal;
                    while(true)
                    {
                        
                        nTotal = Integer.parseInt(JOptionPane.showInputDialog("How many total songs would you like to enter?"));//asking for new value of limited songs
                        if (nTotal < tTotal)
                        {
                            if(nTotal < x)
                            {
                                JOptionPane.showMessageDialog(null,"You are trying to remove space that is already in use.");
                            }
                            if (nTotal >= x)
                            {
                                remove = remove + 1;
                            }
                        }
                        if (nTotal == tTotal)
                        {
                            JOptionPane.showMessageDialog(null, "Entering the same value is useless. Please enter a bigger value");
                        }
                        if (nTotal > tTotal || remove ==1)
                        {
                            String [ ] song1 = new String [nTotal];//Transition
                            String [ ] artist1 = new String [nTotal];//Transition
                            String [ ] genre1 = new String [nTotal]; //Transition
                            for (counter=0; counter<x; counter++)//Transfering...
                            {
                                song1[counter] = song[counter];
                                artist1[counter] = artist[counter];
                                genre1[counter] =genre[counter];
                            }
                            song = song1;
                            artist = artist1;
                            genre = genre1;
                            JOptionPane.showMessageDialog(null, "Your new limit of songs is: " + song.length + ".\nYou can now add " + (song.length - x) + " songs.");
                            break;
                        }
                    }
    
                }
                if (choose == 6)//quit
                {
                    quit = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?\nThis will erase all your songs", "",JOptionPane.YES_NO_OPTION);
                    if (quit == JOptionPane.YES_OPTION)
                    {
                        JOptionPane.showMessageDialog(null,  "Thank you for using MieTunes, Goodbye!");
                        break;
                    }
                }
    
            }
        }
    }

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  2. help with arrays
    By droidus in forum New To Java
    Replies: 20
    Last Post: 04-19-2011, 06:29 AM
  3. Help with arrays!
    By corrax in forum New To Java
    Replies: 8
    Last Post: 04-06-2011, 07:36 AM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •