Reputation: 141
The below is the question:
Get comma separated String of numbers from user and print the set of odd numbers and even numbers.
sample input:
1,2,3,4,5,6,7,8,9,10
Sample output:
Odd Numbers:
1,3,5,7,9
Even Numbers:
2,4,6,8,10
Sample input:
20,30,40
Sample output:
Even Numbers:
20,30,40
My code:
class OddEv{
public static void main(String args[]){
String s;
Scanner in=new Scanner(System.in);
s=in.nextLine();
for(int i=0;i<s.length();i++){
if(s.charAt(i)%2==0){
System.out.print(s.charAt(i));
}
if(s.charAt(i)%2!=0){
System.out.print(s.charAt(i));
}
}
but I am not getting the right answer. What changes should I bring to get the right output according to the question
actually I don't know java very well so I don't know what to do here
Upvotes: 2
Views: 8740
Reputation: 3728
a lambda with partitioningBy
does the separation
it depends only on the last digit whether a string represents an even or odd number
a conversion into a numerical value is not necessary
String inp = "1,2,3,4,5,6,7,8,9,10";
String[] nums = inp.split( "," );
Map<Boolean, List<String>> map = Arrays.stream( nums ).collect(
partitioningBy(s -> "02468".indexOf(s.charAt(s.length() - 1)) >= 0));
System.err.println( "even: " + map.get( true ) );
System.err.println( "odd: " + map.get( false ) );
even: [2, 4, 6, 8, 10]
odd: [1, 3, 5, 7, 9]
Upvotes: 0
Reputation: 2624
Following is a more functional solution with less moving parts:
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(",");
String evens = Arrays.stream(split)
.filter(number -> Integer.parseInt(number) % 2 == 0)
.collect(Collectors.joining(","));
String odds = Arrays.stream(split)
.filter(number -> Integer.parseInt(number) % 2 != 0)
.collect(Collectors.joining(","));
System.out.println("Even Numbers:\n" + evens);
System.out.println("Odd Numbers:\n" + odds);
}
Yeah, it's quite inefficient, I know. There's probably a better way to do it, but I just wanted the OP to get a hint of how descriptive and simple a code can be.
Upvotes: 0
Reputation: 5557
You can try this simple solution, without using any other concepts like regex
. For this, you can split your string and store it in a string array, and than iterating over an array you can check whether the number is odd or even. Following code will store all the even and odd numbers from your string into the array named even
and odd
.
String s = "1,2,3,4,5,6,7,8,9,10";
int even[] = new int[10];
int odd[] = new int[10];
String ar[] = s.split(",");
int j=0,k=0,oddChecker=0,evenChecker=0;
for(int i=0;i<ar.length;i++){
if(Integer.parseInt(ar[i])%2 == 0){
even[j] = Integer.parseInt(ar[i]);
++j;
evenChecker = 1;
}
else{
odd[k] = Integer.parseInt(ar[i]);
++k;
oddChecker = 1;
}
}
if(oddChecker == 0){
System.out.println("even");
System.exit(0);
}
if(evenChecker == 0){
System.out.println("odd");
System.exit(0);
}
System.out.println("Even numbers:");
for(int i=0;i<j;i++){
if(i!=j-1){
System.out.print(even[i]+",");
}
else{
System.out.print(even[i]);
}
}
System.out.println();
System.out.println("Odd numbers:");
for(int i=0;i<k;i++){
if(i!=k-1){
System.out.print(odd[i]+",");
}
else{
System.out.print(odd[i]);
}
}
Output:
Even numbers:
2,4,6,8,10Odd numbers:
1,3,5,7,9
Don't forget to convert String to Integer when checking the condition and adding numbers to arrays. For that I've used Integer.parseInt(your_string)
.
Upvotes: 3
Reputation: 1330
use two ArrayList
of Integer
for odds and evens, and change String s
to String []s
then use userInput.split(",")
to separate numbers. parse strings in s to integer using Integer.parseInt(str)
method then use if statement to determine number is odd or even and add them to arraylists.
public static void main(String args[]) {
String s[];
Scanner in = new Scanner(System.in);
s = in.nextLine().split(",");
ArrayList<Integer> odds = new ArrayList<>();
ArrayList<Integer> evens = new ArrayList<>();
for (String item : s) {
int number = Integer.parseInt(item);
if (number % 2 == 0) {
evens.add(number);
} else {
odds.add(number);
}
}
}
System.out.println("Even Numbers:");
System.out.println(Arrays.toString(evens.toArray()).replaceAll("[\\p{Ps}\\p{Pe}]", ""));
System.out.println("Odd Numbers:");
System.out.println(Arrays.toString(odds.toArray()).replaceAll("[\\p{Ps}\\p{Pe}]", ""));
10,11,12,13,14,15,16,17,18,19,20
sample output:
Even Numbers:
10, 12, 14, 16, 18, 20
Odd Numbers:
11, 13, 15, 17, 19
Upvotes: 2
Reputation: 717
hope this solves your problem
public class CommaSeperatedNum {
public static void main(String args[]){
String s;
Scanner in=new Scanner(System.in);
s=in.nextLine();
String numarray[] = s.split(",");
int odd[] = new int[20];
int oddcnt=0;
int even[] = new int[20];
int evencnt =0;
for(int i=0;i<numarray.length;i++)
{
if( Integer.parseInt((numarray[i]).trim())%2 ==0){
odd[oddcnt] = Integer.parseInt(numarray[i].trim());
oddcnt++;
}
{
even[evencnt] = Integer.parseInt(numarray[i].trim());
evencnt++;
}
}
System.out.print("Odd Numbers : " );
for (int i = 0; i < odd.length && odd[i] != 0; i++) {
System.out.print(odd[i]+" " );
}
System.out.println();
System.out.print("Even Numbers : " );
for (int i = 0; i < even.length && even[i] != 0; i++) {
System.out.print(even[i]+" " );
}
}
Upvotes: 0