0

I'm trying to create a program that takes each number typed in by the user and sort them out as even, odd and the number zero values.

The result should look like something like this:

User Input: 14005
Output:
Even Numbers: 4
Odd Numbers: 1, 5
Zero's: 0, 0

This is the code I've written, I thought of using string concatination in order to add a new value each time the loop checks for the next character, don't know whether I'm thinking right or not though, would appriciate if someone could tell me where I'm thinking in the wrong way.

package com.craydesign;
import javax.swing.JOptionPane;
public class Main {
 public static void main(String[] args) {
 String number = JOptionPane.showInputDialog("Please enter a number: ");
 String evenNumbers = "";
 String oddNumbers = "";
 String numberZero = "";
 for(int i = 0; i < number.length(); i++) {
 if(number.charAt(i) % 2 == 0) {
 evenNumbers.concat(Integer.toString(i) + ", ");
 } else if(number.charAt(i) % 2 != 0) {
 oddNumbers.concat(Integer.toString(i) + ", ");
 } else if (number.charAt(i) == 0){
 numberZero.concat(Integer.toString(i) + ", ");
 }
 }
 JOptionPane.showMessageDialog(null, "Even numbers: " + evenNumbers + "\n" + "Odd numbers: " + oddNumbers + "\n" + "Zero's: " + numberZero);
 }
}
camickr
325k21 gold badges174 silver badges293 bronze badges
asked Sep 19, 2015 at 11:09
7
  • 2
    use StringBuilder or StringBuffer instead of String Commented Sep 19, 2015 at 11:12
  • If you told the exact problem/output of your current code, it would be easier to help. Commented Sep 19, 2015 at 11:13
  • 2
    String is immutable. This means that none of its methods change it. They simply return a new value. So String.concat() doesn't add anything to the string, it just returns a concatenated string. So you shouldn't use that, you should use StringBuilder instead. Commented Sep 19, 2015 at 11:13
  • @kemkoi see the @TAsk answer. You are dividing the ASCII value. Instead of String, use StringBuilder. Commented Sep 19, 2015 at 11:20
  • use ArrayList<String> instead of String. we dont need to concate. We can easily add the values using add() function Commented Sep 19, 2015 at 11:21

2 Answers 2

1

use Character.getNumericValue() instead of charAt(i)

public static void main(String[] args) throws IOException
 {
 String number = JOptionPane.showInputDialog("Please enter a number: ");
 StringBuffer evenNumbers = new StringBuffer();
 StringBuffer oddNumbers =new StringBuffer();
 StringBuffer numberZero =new StringBuffer();
 for(int i = 0; i < number.length(); i++) {
 int value=Character.getNumericValue(number.charAt(i));
 if(value!=0 && value % 2 == 0) {
 evenNumbers.append(value).append(',');
 } else if(value % 2 != 0) {
 oddNumbers.append(value).append(',');
 } else if (value == 0){
 numberZero.append(value).append(',');
 }
 }
 JOptionPane.showMessageDialog(null, "Even numbers: " + evenNumbers + "\n" + "Odd numbers: " + oddNumbers + "\n" + "Zero's: " + numberZero);
 }

EDIT:(displaying numbers in sorted order)

 String evenNo[]=evenNumbers.toString().split(",");
 String oddNo[]=oddNumbers.toString().split(",");
 Arrays.sort(evenNo);
 Arrays.sort(oddNo);
 JOptionPane.showMessageDialog(null, "Even numbers: " + Arrays.toString(evenNo) + "\n" + "Odd numbers: " + Arrays.toString(oddNo) + "\n" + "Zero's: " + Arrays.toString(numberZero.toString().substring(0, 
numberZero.length()-1).split(",")));
answered Sep 19, 2015 at 11:21

5 Comments

Change evenNumbers.append(value+","); to evenNumbers.append(value).append(","); - in sense of SB
This solved it, apparently I could've used either StringBuffer or StringBuilder. Didn't know String's were immutable, thanks for the answer!
@kemkoi If you want to display numbers in sorted order you can try my Edit.
@JacekCz yes can be done but that would be unnecessary append() method call. yet I updated.
better is one call (highly optimized in JVN ) that construction of 2-3 objects
0

You are using String for output and appending the output in the string.

Its a bad idea. as String class is immutable so if you do change anything in String it will create a new Object in memory.

So your solution will take extra memory.

Accroding to me you can solve this problem in two ways

  1. Use StringBuffer class instead of String class for appending
  2. Use ArrayList to Store your result. and iterate over the arraylist and show the output as you want.

Thanks, Aman

answered Sep 19, 2015 at 11:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.