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>.
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>.
Arrays are initialized to 0 in Java.
Arrays are also fixed size, so if you want to add more than 20 characters, you'll have to either create a new, bigger array and copy the old values there, or use a more suitable class like StringBuilder.
how to add char values to char array in java - Stack Overflow
appending char to char array java - Stack Overflow
Add Something to Char Array at Specific Spot (Java) - Stack Overflow
Adding Char to Char Array in Java - Stack Overflow
You just must convert the character to String and then use something like this:
int sum = Integer.parseInt(stringNum1) + Integer.parseInt(stringNum2);
If you want to convert character ASCII numeric value, just cast your char as an int
like char character = arr[0];
int asciiValue = (int) character;
There is no add method for arrays. Arrays are not resizeable. If you indeed want to use a char[] array, you need to allocate one that is large enough, e.g. by counting the uppercase letters or simply by allocating a array that is surely large enough (twice the String length minus 1).
String input = ...
String outputString;
if (input.isEmpty()) {
outputString = "";
} else {
char[] output = new char[input.length() * 2 - 1];
output[0] = input.charAt(0);
int outputIndex = 1;
for (int i = 1; i < input.length(); i++, outputIndex++) {
char c = input.charAt(i);
if (Character.isUpperCase(c)) {
output[outputIndex++] = ' ';
output[outputIndex] = Character.toLowerCase(c);
} else {
output[outputIndex] = c;
}
}
outputString = new String(output, 0, outputIndex);
}
System.out.println(outputString);
Or better still use a StringBuilder
String input = ...
String outputString;
if (input.isEmpty()) {
outputString = "";
} else {
StringBuilder sb = new StringBuilder().append(input.charAt(0));
for (int i = 1; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(' ').append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
outputString = sb.toString();
}
System.out.println(outputString);
You're approaching this the wrong way. Just add each char back to a new string but with spaces included at the right spots. Don't worry about modifying your char array at all. Here is a slight modification of your code:
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence");
String sentenceString = s.nextLine();
char[] sentenceArray = sentenceString.toCharArray();
//new string to hold the output
//starts with only the first char of the old string
string spacedString = sentenceArray[0] + "";
for(int i = 1; i < sentenceArray.length; i++)
{
if(Character.isUpperCase(sentenceArray[i]))
{
//if we find an upper case char, add a space and the lower case of that char
spacedString = spacedString + " " + Character.toLowerCase(sentenceArray[i]);
}else {
//otherwise just add the char itself
spacedString = spacedString + sentenceArray[i];
}
}
System.out.println(spacedString)
s.close();
}
If you want to optimize performance, you can use a StringBuilder object. However, for spacing out a single sentence, performance isn't going to make any real difference at all. If performance does matter to you, read more on StringBuilder here: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
Using arrays in that case is not the best choice because arrays have a fixed size and you cannot dynamically add and remove items, maybe rather choose lists.
If you need a simple introduction, here you are:
https://www.geeksforgeeks.org/list-interface-java-examples/
Others have mentioned StringBuilder and List which are a step in the right direction as they dynamic (in number of elements). However since this question is only concerned with keeping track of each letter that was guessed a set makes the most sense. One common example is HashSet. Sets do not allow duplicates. However sets will allow both the upper and lower case versions of a character to be present in the set (since they have different values). So you could normalize all the guesses and known letters to either uppercase or lowercase.
Here's how you convert a String to a char array:
String str = "someString";
char[] charArray = str.toCharArray();
I'd recommend that you use an IDE when programming, to easily see which methods a class contains (in this case you'd be able to find toCharArray()) and compile errors like the one you have above. You should also familiarize yourself with the documentation, which in this case would be this String documentation.
Also, always post which compile errors you're getting. In this case it was easy to spot, but when it isn't you won't be able to get any answers if you don't include it in the post.
you are doing it wrong, you have first split the string using space as a delimiter using String.split() and populate the char array with charcters.
or even simpler just use String.charAt() in the loop to populate array like below:
String ini="Hi there";
char[] array=new char[ini.length()];
for(int count=0;count<array.length;count++){
array[count] = ini.charAt(count);
System.out.print(" "+array[count]);
}
or one liner would be
String ini="Hi there";
char[] array=ini.toCharArray();