Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

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
0
NoleBoy
Asked:
NoleBoy
  • 3
  • 2
  • 2
  • +1
1 Solution
 
yongsingCommented:
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!");
}
0
 
fivesigmaeventCommented:
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.
0
 
fivesigmaeventCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
yongsingCommented:
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));
0
 
yongsingCommented:
The last line should be:

System.out.println("Is zero: " + (number.signum() == 0));
0
 
CEHJCommented:
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.
0
 
NoleBoyAuthor Commented:
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.");
    }
0
 
CEHJCommented:
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++;
}
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now