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

Thread: Bubble Sort help

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bubble Sort help

    I have to creat a bubble sort program for homework in my programming 1 class. My requirements are to write a program that allows the user to enter in the number of students, the students name, and the students test score. The output will be the students name and matching score in decreasing order. It says I must use an array, a method, and a bubble sort. I will attach the code that I have written. I keep getting two errors; one being a ']' expected and the other being an illegal start of expression error. I have tried changing tons of stuff within the code but I am too inexperienced to come up with the solution. Can someone please help me? Attached is the code I have written and what cmd is giving me when I try to compile the code.
    Attached Files Attached Files


  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 help

    Please post the code directly to the forums (and please use the code tags), preferentially as an SSCCE. I and many others will not try an download the attachments to view your code. The errors you mention sound like compile time errors, probably have an extra/missing a bracket, semicolon, or any number of things that could be a syntax error.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort help

    mport java.util.Scanner;
    public class Homework5{
    public static void main(String[] args){
    Scanner input= new Scanner(System.in);
     
    	System.out.println("Enter number of students: ");
     
    	int numberofstudents=input.nextInt();
     
    	double[] myList= new Double [num];
     
    	System.out.println("Enter " + myList.length + " Values");
     
    	for(int i=0; i < myList.length; i++){
     
    	myList[i]=input.nextDouble();
     
    	System.out.println("Enter student's name: ");
     
    	String[] studentsname= new String [num];
     
    	students name [i] = input.next();
     
    	System.out.println("Enter student's score: ");
     
    	double studentsscore=input.nextDouble();
     
    	String[] studentScores= new String [num];
     
    	}
     
    	int i;
     
    	int array[]= {i};
     
    	System.out.println("Values before the sort");
     
      for(i = 0; i < array.length; i++)
     
    	System.out.print( array[i]+" ");
     
    	System.out.println();
     
    	bubble_sort(array, array.length);
     
    	System.out.println("Values after the sort");
     
    	for(i = 0; i < array.length; i++)
     
    	System.out.print(array[i]+" ");
     
    	System.out.println();
     
    	}
     
    	public static void bubble_sort( int a[], int n ){
     
    	int i, j, t=0;
     
    	for(i = 0; i < n; i++){
     
    		for(j = 1; j < (n-i); j++){
     
    		if(a[j-1] > a[j]){
     
    		t = a[j-1];
     
    		a[j-1]=a[j];
     
    		a[j]=t;
    		}
    	}
    	}
    	}
    }
    Last edited by helloworld922; June 5th, 2011 at 01:22 AM.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort help

    Does anyone have any idea what is wrong with my code? I stayed up till 1:30am last night searching on the internet and looking in my school book and I just can't figure the problem out. Please help!

  5. #5
    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 help

    You need to define methods and place your code withing that/those methods...
    See Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort help

    Ok when I compile my program now it is giving me 8 errors within the [B]public static void bubble_sort( int a[], int n){ line. I am confused as to why this is doing this. All the errors are illegal start of expression or ';' expected. This is the exact line my teacher gave me. She said this would work. Help please! Here is my full code:

    import java.util.Scanner;
    public class Homework5{
    public static void main(String[] args){
    Scanner input= new Scanner(System.in);
     
      System.out.println("Enter number of students: ");
     
    	int numberofstudents=input.nextInt();
     
    	double[] myList= new Double [num];
     
    	System.out.println("Enter " + myList.length + " Values");
     
    	for(int i=0; i < myList.length; i++){
     
    	myList[i]=input.nextDouble();
     
    	System.out.println("Enter student's name: ");
     
    	String[] studentsname = new String [num];
     
    	studentsname [i] = input.next();
     
    	System.out.println("Enter student's score: ");
     
    	String[] studentsscore = new String [num];
     
    	studentsscore [i] = input.next();
     
    	double[] myList= new double [num];
     
    	System.out.println("Enter " + myList.length + " Values");
     
    	for(int i=0; i < myList.length; i++){
     
    	myList[i]=input.nextDouble();
     
    	}
     
    	int i;
     
    	int array[]= {i};
     
    	System.out.println("Values before the sort");
     
      for(i = 0; i < array.length; i++)
     
    	System.out.print( array[i]+" ");
     
    	System.out.println();
     
    	bubble_sort(array, array.length);
     
    	System.out.println("Values after the sort");
     
    	for(i = 0; i < array.length; i++)
     
    	System.out.print(array[i]+" ");
     
    	System.out.println();
     
    	}
     
    	public static void bubble_sort( int a[], int n ){
     
    	int i, j, t=0;
     
    	for(i = 0; i < n; i++){
     
    		for(j = 1; j < (n-i); j++){
     
    		if(a[j-1] > a[j]){
     
    		t = a[j-1];
     
    		a[j-1]=a[j];
     
    		a[j]=t;
    		}
    	}
    	}
    	}
      }
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bubble Sort help

    Please copy and paste here the FULL text of the error messages.

    Please enclose your code in code tags to preserve the formatting. Left adjusted code is hard to follow.
    See: Java Forums BB Code List

  8. #8
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bubble Sort help

    Ok I hope and think I solved the previous 8 errors but now I am getting 1 error. It is a class, interface, enum expected error. Here is my code that I have so far and I will follow it with what cmd is giving me when I try and compile it.

    import java.util.Scanner;
    public class Homework5{
    public static void main(String[] args){
    Scanner input= new Scanner(System.in);
     
      System.out.println("Enter number of students: ");
     
    	int numberofstudents=input.nextInt();
     
    	double[] myList= new Double [num];
     
    	System.out.println("Enter " + myList.length + " Values");
     
    	for(int i=0; i < myList.length; i++){
     
    	myList[i]=input.nextDouble();
     
    	System.out.println("Enter student's name: ");
     
    	String[] studentsname = new String [num];
     
    	studentsname [i] = input.next();
     
    	System.out.println("Enter student's score: ");
     
    	String[] studentsscore = new String [num];
     
    	studentsscore [i] = input.next();
     
    	double[] myList= new double [num];
     
    	System.out.println("Enter " + myList.length + " Values");
     
    	for(int i=0; i < myList.length; i++){
     
    	myList[i]=input.nextDouble();
     
    	}
     
    	int i;
     
    	int array[]= {i};
     
    	System.out.println("Values before the sort");
     
      for(i = 0; i < array.length; i++)
     
    	System.out.print( array[i]+" ");
     
    	System.out.println();
     
    	bubble_sort(array, array.length);
     
    	System.out.println("Values after the sort");
     
    	for(i = 0; i < array.length; i++)
     
    	System.out.print(array[i]+" ");
     
    	System.out.println();
     
    	}
    	}
     
    		public static void bubble_sort( int a[], int n ){
     
    	int i, j, t=0;
     
    	for(i = 0; i < n; i++){
     
    		for(j = 1; j < (n-i); j++){
     
    		if(a[j-1] > a[j]){
     
    		t = a[j-1];
     
    		a[j-1]=a[j];
     
    		a[j]=t;
    		}
    	}
    	}
    	}
    	}
     }

    well it won't let me copy and paste the cmd screen. But it is saying Homework5.java :05:class, interface, enum expected }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bubble Sort help

    Please enclose your code in code tags to preserve the formatting. Left adjusted code is hard to follow.

    copy and paste the cmd screen
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Bubble Sort help

    You code is unreadable. Check the Code Tag link in copeg's signature.

    I suspect your problem is that you have the bubblesort method inside the main method. Make sure your opening and closing braces { } match up correctly.

Similar Threads

  1. Arrays.Sort Help
    By Brandon Seale in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 20th, 2011, 02:32 AM
  2. 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
  3. Javascript Bubble Sort
    By Fidelacchius in forum What's Wrong With My Code?
    Replies: 17
    Last Post: May 24th, 2010, 04:43 PM
  4. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 09:22 AM
  5. Help with Sort arrays
    By drk in forum Collections and Generics
    Replies: 5
    Last Post: September 6th, 2009, 02:48 AM