Sorting letters in a string

Hi, we have as part of an assignment in school to sort letters in a String, it's specifically supposed to be sorted in the String, I tried to do a bubble sort with charAt() but I couldn't make that work,

any tips or points in the right direction?

here is the entire code its the part marked ///////// problem

that I need hints on.


regards nite



[color=Blue][code]package w40o9;
import java.util.Scanner;

public class Meow
{
public static void main( String[] args )
{
// create Scanner object
Scanner input = new Scanner( System.in );

char[] array = new char[10];

String a = ""; // holds the String value

for ( int i = 0; i < 10; i++ )
{
System.out.print( "Please enter your char: " ); // prompt
array[i] = input.next().charAt(0); // takes in a char and places it in the right spot in the char array
} // end for loop

// for loop puts the char values into the String variable
for( int b = 0; b < 10; b++ )
{
a += Character.toString( array[b] );
} // end for


///./////// problem

for( int c = 0; c < a.length()-1; c++ )
{
for( int d = 0; d < a.length()-1; c++ )
{
if( a.charAt(d) > a.charAt(d+1) )
{
char temp = a.charAt(d+1);
a.charAt(d+1) = a.charAt(d);
a.charAt(d) = Character.toString(temp);
} //end if
} // end nested for
} // end outer for

System.out.println( a );
} // end method main
} // end class Meow[/code][/color]



Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories