patternjavaMinor
Reading a sequence of characters and dynamically splitting into multiple arrays
Viewed 0 times
readingarraysdynamicallyintoandsplittingsequencemultiplecharacters
Problem
I am trying to read a sequence of random characters, integers and symbols from the standard inputstream, split it into 3 separate arrays (uppercase, lowercase and numeric) and print them in that order using standard Java API and JDK7.
Is there any other(better) way of dong the same?
Is there any other(better) way of dong the same?
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String characters = bufferedReader.readLine();
char[] chars = characters.toCharArray();
List characterList = new ArrayList<>(chars.length);
for (char c : chars) {
if (Character.isUpperCase(c)) {
characterList.add(c);
} else if (Character.isLowerCase(c)) {
characterList.add(c);
} else if (Character.isDigit(c)) {
characterList.add(c);
}
}
StringBuilder uppercaseStringBuilder = new StringBuilder();
StringBuilder lowercaseStringBuilder = new StringBuilder();
StringBuilder numericStringBuilder = new StringBuilder();
for (Character character : characterList) {
if (Character.isUpperCase(character)) {
uppercaseStringBuilder.append(character);
} else if (Character.isLowerCase(character)) {
lowercaseStringBuilder.append(character);
} else if (Character.isDigit(character)) {
numericStringBuilder.append(character);
}
}
char[] uppercaseChars = uppercaseStringBuilder.toString().toCharArray();
char[] lowercaseChars = lowercaseStringBuilder.toString().toCharArray();
char[] numericChars = numericStringBuilder.toString().toCharArray();
for (char uc : uppercaseChars) {
System.out.print(uc);
}
System.out.println();
for (char lc : lowercaseChars) {
System.out.print(lc);
}
System.out.println();
for (char nos : numericChars) {
System.out.print(nos);
}
}Solution
First obvious thing: Why do you do the almost same thing twice?
...and...
You are going through an array, check if you want the character and add it to a list. Then you go through that list and make the same check again to put the character in different lists / StringBuilders. You can simply remove the last of these loops and add the characters directly from the array to the stringbuilders.
Also your output would be pretty much the same if you simple wrote, for example...
I guess, with Java8, your problem could be solved nicely (or not so nicely, since char primitives are a little bit tricky there) via Streams.
for (char c : chars) {
if (Character.isUpperCase(c)) {
characterList.add(c);
} else if (Character.isLowerCase(c)) {
characterList.add(c);
} else if (Character.isDigit(c)) {
characterList.add(c);
}
}...and...
for (Character character : characterList) {
if (Character.isUpperCase(character)) {
uppercaseStringBuilder.append(character);
} else if (Character.isLowerCase(character)) {
lowercaseStringBuilder.append(character);
} else if (Character.isDigit(character)) {
numericStringBuilder.append(character);
}
}You are going through an array, check if you want the character and add it to a list. Then you go through that list and make the same check again to put the character in different lists / StringBuilders. You can simply remove the last of these loops and add the characters directly from the array to the stringbuilders.
Also your output would be pretty much the same if you simple wrote, for example...
System.out.println(uppercaseStringBuilder.toString());I guess, with Java8, your problem could be solved nicely (or not so nicely, since char primitives are a little bit tricky there) via Streams.
Code Snippets
for (char c : chars) {
if (Character.isUpperCase(c)) {
characterList.add(c);
} else if (Character.isLowerCase(c)) {
characterList.add(c);
} else if (Character.isDigit(c)) {
characterList.add(c);
}
}for (Character character : characterList) {
if (Character.isUpperCase(character)) {
uppercaseStringBuilder.append(character);
} else if (Character.isLowerCase(character)) {
lowercaseStringBuilder.append(character);
} else if (Character.isDigit(character)) {
numericStringBuilder.append(character);
}
}System.out.println(uppercaseStringBuilder.toString());Context
StackExchange Code Review Q#104490, answer score: 8
Revisions (0)
No revisions yet.