bubble sorting an array of strings

gettin an error that is probably self induced but can not figure out h

Page 1 of 1

9 Replies - 5873 Views - Last Post: 21 July 2007 - 06:03 AM Rate Topic: -----

#1 snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

bubble sorting an array of strings

Posted 19 July 2007 - 08:39 AM

Here is my assingment:
Here is what I would like you to do. Create a BubbleSortTest class with a main method. Inside main, declare a simple array of type String and initialize it to some text that is not in alpha order. Loop through the array and display the contents. Then call a bubble sort method, and after the call, display the array again. Make the bubble sort method a static method, pattern it after the bblSort3 method in the Week 11 module. You will have to research the text book or the String API to determine which String method should be used to do the comparison for the swapping logic.
Here is what I have done:


public class BubbleSort
{
	public static void main(String[] args)
	{ 
	   String [] mine = {"w","i","e","d","m","o","n","t"}; 

	   for (int i = 0; i < 8; i++)
	   {
		   System.out.print(mine[i]);
	   }	   
   public static BubbleSort(String mine[])// error illegal start of expression
	   {
		   boolean isSorted;
		   String tempVar;
		   int passes = 0;
		   do
		   {
			   isSorted = true;
			   for (int i = 1; i < mine.length - passes; i++)
			   {
				   if (mine[i] < mine[i-1])
				   {tempVar = mine[i];
					   mine[i] = mine[i-1];
					   mine[i-1] = tempVar;
					   isSorted = false;
					}
				}
				passes++;
				while(!isSorted);
				return false;
			}			
		}
	public int compareToIgnoreCase(String mine)
		System.out.println(mine[i]);
		System.out.println(false);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: bubble sorting an array of strings

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 09:23 AM

You are simply missing a return type first of all. Remember the format...

public static RETURNTYPE functionname(parameters)

Secondly you are declaring a function name inside of the main function. Close your main method and call your bubblesort method from main.

That should get you started on the right track.
Was This Post Helpful? 0
  • +
  • -

#3 snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 10:34 AM

I can add the returntype, but how do I call the method from main?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 12:31 PM

In main you simply put in BubbleSort(mine);

There are a few other things that you might want to look at regarding this code you displayed. One is the actual format of your bubblesort function and where your while statement is actually located. It is inside the "do" block and I am not sure you want it there. Second thing to consider is how you compare your strings. They are string objects so you will need to use a method of class String to compare. The hint the assignment gives you is that the method you will need is part of the string object.

Interesting that this bubble sort uses a do loop, I usually see it implemented in two for loops or using a while loop. It can still sort with a do loop like this (I have done a proof of concept for myself).

If you are still stuck, let us know.
Was This Post Helpful? 0
  • +
  • -

#5 snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 01:56 PM

Here is my updated code with new error message. And I still do not understant the hint.
public class BubbleSort
{
	public static void main(String[] args)
	{ 
	   String [] mine = {"w","i","e","d","m","o","n","t"}; 

	   for (int i = 0; i < 8; i++)
	   {
		   System.out.print(mine[i]);
	   } 
	}  
	public static void main (BubbleSort(mine))//error <identifier> expected 
	   {
		   boolean isSorted;
		   String tempVar;
		   int passes = 0;
		   do
		   {
			   isSorted = true;
			   for (int i = 1; i < mine.length - passes; i++)
			   {
				   if (mine[i] < mine[i-1])
				   {tempVar = mine[i];
					   mine[i] = mine[i-1];
					   mine[i-1] = tempVar;
					   isSorted = false;
					}
				}
				passes++;
				while(!isSorted);
				return false;
			}			
		}
	public String compareToIgnoreCase(String mine)
		System.out.println(mine[i]);
		System.out.println(false);
}


Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 03:11 PM

No no, perhaps I wasn't clear enough. You need to call it from the main method, don't change the name of the function name.


public class BubbleSort
{
	public static void main(String[] args)
	{
	   String [] mine = {"w","i","e","d","m","o","n","t"};

		   // Print before Call
	   for (int i = 0; i < 8; i++)
	   {
			System.out.print(mine[i]);
	   }

	   BubbleSort(mine);  // Here we are calling the function

		   // Print after call
	   for (int i = 0; i < 8; i++)
	   {
			System.out.print(mine[i]);
	   }
	 }  

	 public static void BubbleSort(String[] mine)
	 {
		   boolean isSorted;
		   String tempVar;
		   int passes = 0;
		   do
		   {
			   isSorted = true;
			   for (int i = 1; i < mine.length - passes; i++)
			   {
				   // Hint will be something like mine[i].methodtocompare(mine[i-1])
				   if (mine[i] < mine[i-1])
				   {
					   tempVar = mine[i];
					   mine[i] = mine[i-1];
					   mine[i-1] = tempVar;
					   isSorted = false;
					}
				}
				passes++;
			} while(!isSorted); // Notice where this while condition is at now.		  
		}
}



I have made a few of the corrections but of course this still won't compile correctly yet. Read the comments I put in and you may see what you need to do next.

Hope this is making sense. :)

This post has been edited by Martyr2: 19 July 2007 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 03:43 PM

Which method should I put in and how for the methodtocompare?
I thought it should be BubbleSort, but that did not work.
I put in compareToIgnoreCase and now get a totaly different error, incompatible types found int but expected boolean.

This post has been edited by snake: 19 July 2007 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 04:34 PM

You are right to use the compareToIgnoreCase() but notice the output of that function. It returns an integer. Negative integer if greater than, zero if they are equal and positive if less than. So what do you think you have to do to check if one is greater/less than the other? Compare the output of that function against zero.

if (mine[i].compareToIgnoreCase(mine[i-1]) < 0) {
	 // mine[i] is greater than mine[i-1]
}



The error you are getting is saying that it expects a boolean because it is an if statement. Checking the integer value in an expression will give you that true and false.

Keep working at it, you are moving in the right direction. :)
Was This Post Helpful? 0
  • +
  • -

#9 bernard.assaf  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-July 07

Re: bubble sorting an array of strings

Posted 19 July 2007 - 10:08 PM

this is the complete class without little bugs in code, martyr has already put the corrections separate.

package dreamincode;


public class BubbleSort {
	public static void main(String[] args) {
		String [] mine = {"w","i","e","d","m","o","n","t"};
		
		// Print before Call
		for (int i = 0; i < 8; i++) {
			System.out.print(mine[i]);
		}
		System.out.println();
		
		BubbleSort(mine);  // Here we are calling the function
		
		// Print after call
		for (int i = 0; i < 8; i++) {
			System.out.print(mine[i]);
		}
	}
	
	public static void BubbleSort(String[] mine) {
		boolean isSorted;
		String tempVar;
		int passes = 0;
		do
		{
			isSorted = true;
			for (int i = 1; i < mine.length - passes; i++) {
				// Hint will be something like mine[i].methodtocompare(mine[i-1])
				if (mine[i].compareToIgnoreCase(mine[i-1]) < 0) {
					{
						tempVar = mine[i];
						mine[i] = mine[i-1];
						mine[i-1] = tempVar;
						isSorted = false;
					}
				}
				passes++;
			}
		} while(!isSorted); // Notice where this while condition is at now.
		
	}
}


Was This Post Helpful? 0
  • +
  • -

#10 snake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 07

Re: bubble sorting an array of strings

Posted 21 July 2007 - 06:03 AM

Thanks to all who helped. It worked after I just kept playing with it and am gratefull to all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1