where to break array sorting?
For an assignment ive had to have a problem read in a string and then display the string and state if the letters in the string are ascending or not. I've managed to do this however i swear no matter where i add either
if ( input.equals( "END" ){
break;
}
or a
while ( ! input.equals( "END" ){
break;
}
it won't break after i receive "letters are not in ascending order"
Heres the code with the simplistic ifs :)
Code :
class Main
{
public static void main( String args[] )
{
System.out.print("#Enter Word Here:");
String input = BIO.getString();
char[] characters = input.toCharArray();
boolean haveSwapped = true;
while ( haveSwapped )
{
if ( input.equals( "END") ){
break;
}
haveSwapped = false;
for (int i=0; i<characters.length-1; i++)
{
if ( characters[i] > characters[i+1] )
{
char tmp = characters[i];
characters[i] = characters[i+1];
characters[i+1] = tmp;
haveSwapped = true;
System.out.print( input );
System.out.println(" letters are not in ascending order");
System.out.print("#Enter Word Here:");
input = BIO.getString();
if ( input.equals( "END") ){
break;
}
characters = input.toCharArray();
i = 0;
}
}
System.out.print( input );
System.out.println(" letters are in ascending order");
System.out.print("#Enter Word Here:");
input = BIO.getString();
characters = input.toCharArray();
haveSwapped = true;
if ( input.equals( "END") ){
break;
}
}
}
}
Is it possible? where and how? :S thanks for any help.
Re: where to break array sorting?
To see what the code sees, add a prinln to print out the value of the input variable immediately after you read it in. Be sure to add delimiting Strings: println("input=" + input + "<") so you can see if input is empty or has extra spaces in it.
Re: where to break array sorting?
You have a for loop inside a while loop. Your break statement would cause execution to break only from the immediate loop (the for loop). Control will be received by the while loop at the point following the for loop. Is this as you intend?
Re: where to break array sorting?
Quote:
Originally Posted by
2by4
You have a for loop inside a while loop. Your break statement would cause execution to break only from the immediate loop (the for loop). Control will be received by the while loop at the point following the for loop. Is this as you intend?
3
No, i intended the program to end overall. :S
Re: where to break array sorting?
Quote:
Originally Posted by
BITmixit
3
No, i intended the program to end overall. :S
The java language allows you to use a label to specify the construct from which execution would break. This is necessary if you wish to break directly out of an outer loop from within a nested loop. You may wish to reference your favourite java language manual for how to use labels. :-)
Re: where to break array sorting?
Right, got it working it now exits upon entry of END. However when trying to submit it to the system which checks our code online, compiles it and runs examples mine doesn't work. Heres the code:
Code :
class Main
{
public static void main( String args[] )
{
System.out.print("#Enter Word Here:");
String input = BIO.getString();
char[] characters = input.toCharArray();
boolean haveSwapped = true;
while ( haveSwapped )
{
if ( input.equals( "END" ) ){
System.exit(0);
}
haveSwapped = false;
for (int i=0; i<characters.length-1; i++)
{
if ( characters[i] > characters[i+1] )
{
char tmp = characters[i];
characters[i] = characters[i+1];
characters[i+1] = tmp;
haveSwapped = true;
System.out.printf("%-10s letters not in ascending order", input);
System.out.println();
System.out.print("#Enter Word Here:");
input = BIO.getString();
if ( input.equals( "END") ){
System.exit(0);
}
characters = input.toCharArray();
i = 0;
}
}
System.out.printf("%-10s letters are in ascending order", input);
System.out.println();
System.out.print("#Enter Word Here:");
input = BIO.getString();
if ( input.equals( "END" ) ){
System.exit(0);
}
characters = input.toCharArray();
haveSwapped = true;
}
}
}
Heres the output from the testing system.
----------Data used as input was----------------------------------------
abcdefgh
Xabcdefg
aXbcdefg
abXcdefg
abcXdefg
abcdXefg
abcdeXfg
abcdefXg
abcdefgX
ABCDEFGH
END
----------Expected answer was-------------------------------------------
abcdefgh letters in ascending order
Xabcdefg letters not in ascending order
aXbcdefg letters not in ascending order
abXcdefg letters not in ascending order
abcXdefg letters not in ascending order
abcdXefg letters not in ascending order
abcdeXfg letters not in ascending order
abcdefXg letters not in ascending order
abcdefgX letters in ascending order
ABCDEFGH letters in ascending order
----------Your answer however was [ Excluding lines containing a # ] ---
abcdefgh letters in ascending order
Xabcdefg letters in ascending order
aXbcdefg letters not in ascending order
abXcdefg letters not in ascending order
abcXdefg letters not in ascending order
abcdXefg letters not in ascending order
abcdeXfg letters not in ascending order
abcdefXg letters not in ascending order
abcdefgX letters not in ascending order
ABCDEFGH letters in ascending order
I dont really understand why abcdefgX and Xabcdefg aren't doing what they are supposed do.
Any help?
Re: where to break array sorting?
Quote:
Xabcdefg letters not in ascending order
I'm confused why those letters are not in ascending order. The ASCII values for the letters are:
X= \u0058
a= \u0061
b= \u0062
Based on that X comes before a which comes before b.
What do the sorting rules say for ordering the letters? Are you supposed to ignore case?
Which comes first A or a?
Re: where to break array sorting?
I assumed it was a, A, b, B, c, C
My friends seems to think char[] characters = input.toCharArray();
is converting any capitals from a string into normal letters before adding to an array.
Re: where to break array sorting?
You definitely need the correct sorting sequence before you can write the code.
Do you understand my last post about 'X' being lower in value than 'a'?
That is what the ASCII values are.
If you want to change that ordering, you will need special code to handle uppercase letters.
Definite the order that you want, then you can write the code.
Re: where to break array sorting?
No i dont really understand it :S
I want the order to be like i stated:
A, a, B, b, C, c
How do i go about changing the order?
Is there no way i can tell the sorting process to ignore capitals or treat capitals as normals?
Re: where to break array sorting?
Your code will have to look at the case of each letter and consider that when comparing them.
Your code has a simple comparison:
Code :
if ( characters[i] > characters[i+1] )
This will have to be changed to consider the case of the letter.
One way will be to change both of the letters to be the same case before comparing them.
If the same case letters match then you need to test if the original versions are the same case or different case.
Re: where to break array sorting?
Finished it and it works. I basically just told the program to convert the string to lowercase before putting it in order and then just simply printed the original string so it still looked like capitals.
Thanks for the help :) completed 2 assignments in the space of an hour thanks to this :D
Re: where to break array sorting?
Did you try these two Strings to see which is in order and which is not:
Xx
xX
The testing you describe would fail with these.