i would like to display letters ether they are in ascending order or not in ascending order, i’m having problem to compare the array’s index. in this case i’m using bubble sort technic and modify them.
public class Main
{
public static void main(String[] args)
{
System.out.print( "#Enter text : " );
String text = BIO.getString();
while ( ! text.equals( "END" ) )
{
boolean inorder = false;
// Convert the above string to a char array.
char[] arr = text.toCharArray();
for (int i=0; i<arr.length-1; i++)
{ //Check pair
if ( arr[i] > arr[i+1] )
{
System.out.print("letters in ascending order");
inorder = true;
}
}
if ( ! inorder )
{
System.out.print("letters not in ascending order");
}
break;
}
}
}
Since this looks like homework, I’ll limit my answer to a couple of hints.
Your logic is the wrong way round. You should assume that the array is in the ascending order until you see evidence to the contrary.
Also, that
printinside the loop can get called multiple times. You probably don’t want that.Other than this, you’re almost there.