Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: bubble sort problem please help!!

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    My Mood
    Cold
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation bubble sort problem please help!!

    hi there, i have to do a school project and it's about a dvd rental system. I have the following classes ; Client, SalesPerson(both inherit from the class Person), Loan class, DVD class, Runner, DvdShop.
    Now my problem is how do I sort the DVDs when the user inputs his choice in the menu to show all dvds .
    I have tried to do the code but it gives me an error. I don't know what's wrong with it. Here is what I tried so far;
    P.S. DVDs ARE ADDED TO THE ARRAYLIST NAMED dList which I have declared at the beginning as ; ArrayList dList = new ArrayList () ;

    the following is in the DvdShop class;


    public String showAllDvds()
    {
    int temp = 0;
    int n = dList.size();
    String tmpDvd = "";

    if(n>0)
    {
    for(int i = 0; i < n; i++)
    {
    for(int j = 0; j < (n-i); j++)
    {
    if(dList[j-1] > dList[j])
    {
    //swap the elements
    temp = dList[j-1];
    dList[j-1] = dList[j];
    dList[j] = temp;
    }
    }
    }
    /* DVD tmpDVD = (DVD) dList.get(i);
    tmpDvd += "DVD " + (id+1) + ": " + tmpDVD.toString() + "\n";*/
    }


    else
    {
    tmpDvd = "No DVDs found! ";
    }

    return tmpDvd;
    }

    while the following is in the runner class ;

    case 7:

    //Show all DVDs in DvdShop

    System.out.println(myDvdShop.showAllDvds());
    for(int i=0; i < dList.size; i++)
    {
    System.out.println(dList[i] + " ");
    }
    break;

    Can you please help?? thanks.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: bubble sort problem please help!!

    I have tried to do the code but it gives me an error.
    What error? Post it in its entirety, and please wrap your code in the code tags

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: bubble sort problem please help!!

    Could you be a little more specific, please? How exactly do you want your DVD's to be sorted? And does the user input criteria for the sorting, or just request to see all the DVD's?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. #4
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: bubble sort problem please help!!

    BUBBLE SORT (Java, C++) | Algorithms and Data Structures

    Check that out and it might help you solve the problem. good luck.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    My Mood
    Cold
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bubble sort problem please help!!

    thanks for your interest
    sorry about not wrapping my code in code tags but i am relatively new to this blog...
    The error i receive are 2 ;

    1) in this line in the DvdShop class - if(dList[j-1] > dList[j]) i receive the error ' array required, but java.util.ArrayList found'
    2) in this line in the Runner class - for(int i=0; i < dList.size; i++) i receive the error ' cannot find symbol - variable dList '

    I know that the second error is because the arraylist dList is not declared in the Runner Class but I don't know how to call the arraylist from the DvdShop to the Runner class!!

    thanks again

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    My Mood
    Cold
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bubble sort problem please help!!

    thanks I actually have visited this site before but in this case, the arraylist is in the same class while mine is in another class so I have to call it to use it in the second class etc.... so it doesn't help me so much
    but thanks for the reply i appreciate it

  7. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: bubble sort problem please help!!

    Your first issue (first error): you cannot access the elements of an ArrayList like you do in an array. Instead, use ArrayList's get() method.

    The second issue: if dList is in the DvdShop class, you cannot access it another class simply by referring to its name. You must first call the class and then the variable name, which is dList in this case (if the class is static). However, if DvdShop is an Object, you must refer first to an instance of DvdShop and then the variable (dList).
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  8. The Following User Says Thank You to snowguy13 For This Useful Post:

    yanikapausini:) (January 24th, 2012)

Similar Threads

  1. bubble sort timer
    By gujinni in forum Other Programming Languages
    Replies: 1
    Last Post: October 15th, 2011, 09:30 AM
  2. Bubble Sort Int Array Problem
    By thisbeme in forum Collections and Generics
    Replies: 1
    Last Post: September 11th, 2011, 09:24 AM
  3. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 08:47 PM
  4. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM
  5. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 09:22 AM

Tags for this Thread