String manipulation

Posted on 2003-02-23
Medium Priority
Last Modified: 2010-03-31
am a beginner Java programmer so bear with me. I have a question about a project I am working on. I want to receive an
integer (max 30 digits) from the user, then count how many odd, even, and zeros was entered. I have the logic down right:
using modulus to determine if odd or even, and using the ASCII code for zero to determine if it is a zero.

My question is do I need to convert the string to an int or leave it as a string. I am a bit puzzled as how this works.
Any help in the form of example code or relative support would be greatly appreciated.

Question by:NoleBoy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 8005651
Yes, you need to convert the string to an int first:

try {
  int number = Integer.parseInt(theString);
  boolean isOdd = (number % 2) != 0;
  boolean isEven = (number % 2) == 0;
  boolean isZero = (number == 0);
} catch (NumberFormatException e) {
  System.out.println("Invalid number!");

Expert Comment

ID: 8005756
A 30 digit number is very large. Not sure if it fits into integer or long.

You can do it this way if you want without converting the input into an integer or long.
1. Read in the number from the console. It will be a string.
2. Check the last digit of the number for whether it is even or odd. The last digit determines eveness or oddness.
3. Loop through the string from beginning to end, counting evens, odds, and zeros.

Accepted Solution

fivesigmaevent earned 150 total points
ID: 8005779
A 30 digit number would not fit into an int 32 bit field. It will only up to 10 digits. And I think that only upto 19 digits would fit into a long 64 bit field. Best bet is to not try to convert it to int or long, just keep it as a string.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8005862
Sorry, I didn't notice about the 30 digits max. If it's that long, you should use a BigInteger object instead:

BigInteger number = new BigInteger("999999999999999999999999999999999999999999999999999");
BigInteger remainder = number.mod(new BigInteger("2"));
System.out.println("Odd: " + (remainder.intValue() != 0));
System.out.println("Even: " + (remainder.intValue() == 0));
System.out.println("isZero: " + (remainder.signum() == 0));

Expert Comment

ID: 8007194
The last line should be:

System.out.println("Is zero: " + (number.signum() == 0));
LVL 86

Expert Comment

ID: 8007609
If you wish to discover the properties of the individual digits, then it looks like you've got it pretty well taped. No, you don't need to convert it to a numerical value.

Author Comment

ID: 8012116
I appreciate everyone's help in this matter. I decided to leave it as a string then compare each digit in a for loop.
It is probably more coding than necessary but it worked.
Code below:

public void process(){
        String integerString = getIntegerString();
        int evenDigits = 0;
        int oddDigits = 0;
        int zeroDigits = 0;
        for (int i = 0; i < integerString.length(); i++){
            char c = integerString.charAt(i);
            if (c == '0') zeroDigits++;
            if (c == '0') evenDigits++;
            if (c == '1') oddDigits++;
            if (c == '2') evenDigits++;
            if (c == '3') oddDigits++;
            if (c == '4') evenDigits++;
            if (c == '5') oddDigits++;
            if (c == '6') evenDigits++;
            if (c == '7') oddDigits++;
            if (c == '8') evenDigits++;
            if (c == '9') oddDigits++;
        System.out.println(integerString + " contains "
            + evenDigits + " even digits, "
            + oddDigits + " odd digits, and "
            + zeroDigits + " zero digits.");
LVL 86

Expert Comment

ID: 8012223
Yes, you could do:

for (int i = 0; i < integerString.length(); i++){
           char c = integerString.charAt(i);
           if ((c % 2) == 0) {
           else {
           if (c == '0') zeroDigits++;

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question