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 5 of 5

Thread: Bubble Sort errors

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bubble Sort errors

    hi, i'm a beginner java programmer and i have problems regarding my program. i can't make it work, it displays a lot of errors. i hope you can help me. this is my program:


    public class BubbleSort1
    {
    public static void main(String[]args)
    {
    int arrays[] = {5,3,2,1,51,23};

    System.out.print("Arrays Before Sorted: ");
    for(int i = 0; i < arrays.length; i++)
    {
    System.out.print(arrays[i]+ " ");

    System.out.println();
    }
    bubbleSort(arrays);
    System.out.print("Arrays Sorted: ");

    for(int i = 0; i < arrays.length; i++)
    {
    System.out.print(arrays[i]+ " ");
    }
    private static void bubbleSort(int[] arrays)
    {
    int n = arrays.length;
    int temp = 0;
    for(int i = 0; i < arrays.length; i++)
    {
    for(int j = 1; j < (n-1); j++)
    {
    if(arrays[j-1] > arrays[j])
    temp = arrays[j-1];
    arrays[j-1] = arrays[j];
    arrays[j] = temp;
    }
    }
    }
    }
    }
    Last edited by moglance; July 5th, 2013 at 05:14 PM. Reason: wrong title


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Bubble Sort errors

    Please post your code in code tags.

    Start by moving the bubbleSort() method outside the main() method. That should eliminate your compiler errors and give you code that you can test and improve.

    Then check the if() clause inside the nested for loop. I believe you want brackets around the if() clause's 3 statements. Edit: Excuse me, not brackets, "[ ]", but braces, "{ }".

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort errors

    thanks for the help. if you don't mind, will you please provide me the proper methods for me to compare and serve as a guideline for my future projects? to be honest i am not much of a fast learner. anyway, thanks again

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Bubble Sort errors

    Quote Originally Posted by moglance View Post
    if you don't mind, will you please provide me the proper methods
    Not really, but we would be happy to help you write your own.
    Please use code tags when posting code. Check out the Announcements page if you need help.
    "i can't make it work, it displays a lot of errors" ...this tells us basically nothing.
    What does the code do? What should it do instead? Provide actual output and desired output (with sample input if applicable)
    If there are error messages, always include the full text of the error messages with the code and your question.
    If you do not understand the error message, then say that is the question.

  5. #5
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Bubble Sort errors

    It seems like you've made a few errors in your code. Here's a corrected version:

    public class BubbleSort1 {
    public static void main(String[] args) {
    int arrays[] = {5, 3, 2, 1, 51, 23};

    System.out.print("Arrays Before Sorted: ");
    for (int i = 0; i < arrays.length; i++) {
    System.out.print(arrays[i] + " ");
    }
    System.out.println(); // Move this line outside the loop to print on a new line

    bubbleSort(arrays);
    System.out.print("Arrays Sorted: ");

    for (int i = 0; i < arrays.length; i++) {
    System.out.print(arrays[i] + " ");
    }
    }

    private static void bubbleSort(int[] arrays) {
    int n = arrays.length;
    int temp = 0;
    for (int i = 0; i < n; i++) { // Change the loop condition to n
    for (int j = 1; j < (n - i); j++) { // Change the loop condition to (n - i)
    if (arrays[j - 1] > arrays[j]) {
    temp = arrays[j - 1];
    arrays[j - 1] = arrays[j];
    arrays[j] = temp;
    }
    }
    }
    }
    }

    if you ever need additional support or assistance with your programming homework, you can check out https://www.programminghomeworkhelp....va-assignment/ for expert guidance and solutions.

Similar Threads

  1. Error:Cannot find Symbol
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2013, 06:40 PM
  2. [SOLVED] cannot find symbol error
    By Topflyt in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 5th, 2011, 08:57 AM
  3. Cannot find symbol ERROR
    By yacek in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 21st, 2011, 11:39 PM
  4. error :cannot find symbol
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 1st, 2011, 08:26 AM
  5. Cannot find symbol error
    By AnuR in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 23rd, 2011, 02:50 PM