asked on # String manipulation

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.

Thanks

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.

Thanks

Java

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.

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.

Log in or sign up to see answer

Become an EE member today7-DAY FREE TRIAL

Members can start a 7-Day Free trial then enjoy unlimited access to the platform

or

Learn why we charge membership fees

We get it - no one likes a content blocker. Take one extra minute and find out why we block content.

Not exactly the question you had in mind?

Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.

ask a question
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));

BigInteger number = new BigInteger("99999999999999

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

rwheeler23

The last line should be:

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

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

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.

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.");

}

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(integer

+ evenDigits + " even digits, "

+ oddDigits + " odd digits, and "

+ zeroDigits + " zero digits.");

}

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

Yes, you could do:

for (int i = 0; i < integerString.length(); i++){

char c = integerString.charAt(i);

if ((c % 2) == 0) {

evenDigits++;

}

else {

oddDigits++

}

if (c == '0') zeroDigits++;

}

for (int i = 0; i < integerString.length(); i++){

char c = integerString.charAt(i);

if ((c % 2) == 0) {

evenDigits++;

}

else {

oddDigits++

}

if (c == '0') zeroDigits++;

}

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("Invali

}