The Artima Developer Community
Sponsored Link

Java Answers Forum
help~!!!

17 replies on 2 pages. Most recent reply: Sep 11, 2003 3:09 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 17 replies on 2 pages [ 1 2 | » ]
Anthony

Posts: 3
Nickname: kingka91
Registered: Sep, 2003

help~!!! Posted: Sep 8, 2003 11:20 AM
Reply to this message Reply
Advertisement
I have an assignment due next week and I have no idea how to work this out..can anyone please help me with this?

prints the number of odd, even, and zero digits in an integer value read from the keyboard.


Alex S

Posts: 27
Nickname: andu
Registered: Mar, 2003

Re: help~!!! Posted: Sep 8, 2003 11:50 PM
Reply to this message Reply
Get the number, repeatedly divide it by 10, take the reminder. If reminder is odd then odd++; if reminder is even then even++; if reminder is zero then zero++. Do this until you have nothing left to divide.

E.g. number is 1203.
1. 1203 / 10 = 120*10 + 3 => odd = 1
2. 120 / 10 = 12*10 + 0 => zero = 1
3. 12 / 10 = 1*10 + 2 => even = 1
4. 1 / 10 = 0*10 + 1 = > odd = 2

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: help~!!! Posted: Sep 9, 2003 1:55 AM
Reply to this message Reply
Can't you just see if it's divisible by 2?

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: help~!!! Posted: Sep 9, 2003 6:55 AM
Reply to this message Reply
That's what I was thinking, too. I think that I like the divide-by-ten solution better, though. You don't have to work with Strings at all, going through the whole for...charAt(i) routine. Just simple integer arithmetic.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: help~!!! Posted: Sep 9, 2003 6:58 AM
Reply to this message Reply
Now I feel silly. You still have to do a modulo 2, to see if the remainer is odd. You also have to specifically check for zero, as that increments a different counter.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: help~!!! Posted: Sep 9, 2003 6:59 AM
Reply to this message Reply
Huh? What's wrong with:

int number = Integer.parseInt("1234");
if (number == 0) {
System.out.println("Zero");
}
else if (number % 2 == 0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: help~!!! Posted: Sep 9, 2003 7:00 AM
Reply to this message Reply
:)

Paris Deligiannakis

Posts: 2
Nickname: customiser
Registered: May, 2003

Re: help~!!! Posted: Sep 9, 2003 7:22 AM
Reply to this message Reply
erm... basically it doesn't answer the question :)

You need to check every digit, not the whole number

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: help~!!! Posted: Sep 9, 2003 7:26 AM
Reply to this message Reply
Ah...each *digit* within the number.

The /10 method is clever, but you'll have to remember to comment it 'cos it's not the most intuative method.

(BTW, I don't think anything is particularly nasty by examining each character in the String).

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help~!!! Posted: Sep 9, 2003 3:39 PM
Reply to this message Reply
> Ah...each *digit* within the number.
>
> The /10 method is clever, but you'll have to remember to
> comment it 'cos it's not the most intuative method.
>
> (BTW, I don't think anything is particularly nasty by
> examining each character in the String).

I prefer checking each character for two reasons:

1) Why do an unnecessary conversion to int? It was a string to begin with, if it is coming from user input.

2) What if you want to check something interesting like how many of the first 50,000 digits of pi are odd/even/zero? In this case, converting to an int is not likely to be a successful strategy (in Java, at least).

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: help~!!! Posted: Sep 9, 2003 11:41 PM
Reply to this message Reply
(2)

Good point.

gratiartis

Posts: 8
Nickname: gratiartis
Registered: Sep, 2003

Re: help~!!! Posted: Sep 10, 2003 7:50 AM
Reply to this message Reply
Given that you're receiving this as a String, you should be able to do the following:
public static void processNumber( String numberString ) {
    char[] chars = numberString.toCharArray() ;
    int digit ;
    int zeroCount = 0 ;
    int oddCount = 0 ;
    int evenCount = 0 ;
 
    for ( int i = 0 ; i < chars.length ; i++ ) {
	digit = Character.getNumericValue( chars[ i ] ) ;
	if ( digit == 0 ) {
	    zeroCount++ ;
	} else if ( i % 2 == 0  ) {
	    evenCount++ ;
	} else {
	    oddCount++ ;
	}
    }
 
    System.out.println( "zeroCount=" + zeroCount 
        + ", evenCount=" + evenCount 
        + ", oddCount=" + oddCount  ) ;
}
I think most of that is self-explanatory, so I haven't bothered commenting it. Basically, I converted the String to an array of char and then just iterated through the array checking whether the numeric value of the char was odd, even or zero. If you're provided with an int to start with, you can easily convert that to a String to pass to this method using Integer.toString( theInt )

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: help~!!! Posted: Sep 10, 2003 8:52 AM
Reply to this message Reply
Correction boss :

Use

} else if ( digit % 2 == 0 ) {

instead of

} else if ( i % 2 == 0 ) {

Moreover this can take any string like "abc" and calculate the even,odd and zeros of in that String. as getNumericValue(char ch) returns the Unicode numeric value of the character as a nonnegative integer.

Try passing value ""0.3" to ur method and see the result ...


We can get rid of this error by first checking if the string passed can form an Integer or not.(Do u think it is costly??)

try
{
Integer integer = new Integer(numberString);
} catch (Exception e)
{
System.out.println("Not an integer");
return;
}

Clark D Richey, Jr.

Posts: 4
Nickname: cdrichey
Registered: Jun, 2003

Re: help~!!! Posted: Sep 10, 2003 10:15 AM
Reply to this message Reply
Nice....you just did someone's programming assignment for them.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help~!!! Posted: Sep 10, 2003 7:24 PM
Reply to this message Reply
> Nice....you just did someone's programming assignment for
> them.

Yeah. If you are going to do people's homework, you should at least present in a form that will provide ample amusement for the instructor. For example:
class SomeoneElsesAssignment
{
   public static void p(String s) {System.out.println(s);}
   public static void main(String args[])
   {
      SomeoneElsesAssignment ass = new SomeoneElsesAssignment();
      for( int i = 0; i < args.length; i++ ) ass.u(args[i]);
      String __ = " digits.";
      String [] ___ = {" zero"," odd"," even"," other"};
      for( int _ = 0; _ < ___.length; _++ ) p( ass._[_] + ___[_] + __ );
   }
   private int [] _ = {0,0,0,0};
   public int [] u(String ___)
   {
      for( int __ = 0; __ < ___.length(); __++ )
         switch( ___.charAt(__) )
         {
            case '0':                                         _[0]++; break;
            case '1': case '3': case '5': case '7': case '9': _[1]++; break;
            case '2': case '4': case '6': case '8':           _[2]++; break;
            default:                                          _[3]++; break;
         }
      return _;
   }
}

Flat View: This topic has 17 replies on 2 pages [ 1  2 | » ]
Topic: HI what is wrong with this code??? Previous Topic   Next Topic Topic: Directory Encryption-file list problem and error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use