I have the following code that reads in an array of characters and then prints them out in alphabetical order (without sorting the array). The "tio.*" is a package that contains the readChar method. I've used it before without problems.
The problem with this code seems to be with the writing of characters to the array in the for statement. Whenever I try to debug the code by printing the contents of the array, all I get is the first three characters of the array. I'm not sure why.
I would appreciate any help. THANKS!
The problem with this code seems to be with the writing of characters to the array in the for statement. Whenever I try to debug the code by printing the contents of the array, all I get is the first three characters of the array. I'm not sure why.
I would appreciate any help. THANKS!
Code:
import tio.*;
class CharacterArray {
public static void main(String[] args) {
System.out.println("How many characters will you input? ");
int charnumber = Console.in.readInt();
char[] charray = new char[charnumber];
System.out.println("Input the characters: ");
for (int i = 0; i < charray.length; i++)
charray[i] = (char)Console.in.readChar();
printOrderedChars(charray);
}
static void printOrderedChars(char[] array) {
char smallest;
for (int i = 0; i < array.length - 1; i++) {
smallest = array[i];
for (int j = i + 1; j < array.length; j++)
if (array[j] < smallest)
smallest = array[j];
System.out.println(smallest);
}
}
}