I have a character array, which is declared as char[ ] array = new char[20];
Regarding this, I have the following queries:
Is the default value at each location, '\0000' ?
How do i append characters into this array ? ( like for instance, each time i ask the user to input a alphabet and i have store the alphabets in order inside the array)
Please bear with my queries. I can easily implement these in C++, but i am new to java. Thanks in advance!
You need to read more about Reading from input streams. You can start here, but there are other wonderful tutorials out there.
Not exactly answering your question (because it's still ambiguous), but in the following I've read 5 characters from input and output the sorted character array. This will help you get started.
public static void main(String[] args) throws IOException {
char arr[] = new char[5];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter input characters and press enter: ");
for(int i=0; i<arr.length; i++) {
arr[i] = (char) br.read();
}
br.close();
Arrays.sort(arr);
System.out.println("Sorted Array: "+String.valueOf(arr));
}
Console I/O:
Enter input characters and press enter:
zeros
Sorted Array: eorsz
Further as Rohit has already mentioned you should look up the String class and its extensive utilities functions. Also, as you also mention that you don't probably know the number of elements in your array, you might want to look at it as a List, in which case you should use List<Character>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With