Results 1 to 20 of 20
  1. #1
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default how would i do this?

    a program using the terminal that determines the number of odd even and zero digits in an inputted value

  2. #2
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    public class integervaluereader
    {
        public static void main (String[] args)
        {
            int place=0;
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter a number..");
            String input = scan.nextLine();
            int length = input.length();
            
            
            
            //char charAt(int index)
            
            for(int f=0; f<length; f++)
            {
                char charat = input.charAt(f);
                int charvalue = parseInt(charat);//error? this is where i need help.
                
            }
    
            
        }
    }
    Last edited by carlos123; 02-03-2008 at 10:23 PM. Reason: comment

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    What class is the method parseInt(char) apart of? Once you find that out, you should be able to write the correct code.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    there isnt a parseInt(char) method

  5. #5
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    if there was i would just use that.

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Then why are you trying to use it?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  7. #7
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    is there any method that does the same thing except for a string or int

  8. #8
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Have you viewed the API ? You still have not understood what I've said - once again:
    What class is the method parseInt(char) apart of? Once you find that out, you should be able to write the correct code.
    To which you replied,
    there isnt a parseInt(char) method, if there was I would just use that
    And yet you appear to already know this - YET you are trying to use it(see the irony there?). Where is parseInt a method of - what class does it belong to? - Answer this with your next post in this thread.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  9. #9
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    do you even understand what im trying to do?

  10. #10
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Carlos - I understand completely, based on what you've written. What you'll learn about me after being here for awhile is that I'm not going to hand you answers - if you want that, please look elsewhere. I'm here to assist you in learning the concepts - which you have failed to do after multiple attempts.

    Answer my question above if you want a resolution to your problem.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  11. #11
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    ive looked all over the API, i dont think there is a parseint(char) method. is there any other ways around this?

  12. #12
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    can you tell me what im doing wrong?

  13. #13
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by carlos123 View Post
    ive looked all over the API, i dont think there is a parseint(char) method. is there any other ways around this?
    That's the point of my previous post - there isn't. With that logic now known, how can you use a method that doesn't exist??

    You still have not answered my question. What class does parseInt belong to? Answer - Integer class. Now - find the Integer class within the API. But a char is not what it takes - does it? What possible arguments does it take carlos? Read the API. It's there.
    Last edited by CaptainMorgan; 02-04-2008 at 04:51 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  14. #14
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by carlos123 View Post
    can you tell me what im doing wrong?
    Indeed. You're not taking the initiative to read the API and further - you're not answering questions of those that are trying to help you - me.

    And, I've already explained possible solutions - of which you should be able to infer, you're trying to use a method that doesn't exist!
    Last edited by CaptainMorgan; 02-04-2008 at 04:52 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  15. #15
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    The parseInt method takes a String, is there any way of NOT getting a char , but instead getting a String.. or converting a char to a string, am i on the right path?

  16. #16
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by carlos123 View Post
    The parseInt method takes a String, is there any way of NOT getting a char , but instead getting a String.. or converting a char to a string, am i on the right path?
    Now I'll confess, I don't understand completely what you just said. I do know based on your original post that this was your problem:
    Java Code:
                int charvalue = parseInt(charat);//[B]error? this is where i need help.
    [/B]
    actually, it's pretty obvious - you said this was your error.

    The parseInt method takes a String
    Finally, you read the API..

    is there any way of NOT getting a char , but instead getting a String..
    NOT getting a char? You will not get a char, because the method returns a String. What's not clear here?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  17. #17
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    import java.util.Scanner;
    public class integervaluereader
    {
    public static void main (String[] args)
    {
    int zero = 0;
    int odd = 0;
    int even = 0;
    int place=0;
    double remain=0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a number..");
    String input = scan.nextLine();
    int length = input.length();

    // int n = Integer.parseInt(input);

    //char charAt(int index)

    for(int f=0; f<length; f++)
    {
    int charat = input.charAt(f);
    remain = charat%2;
    if (charat==0)
    {
    zero++;
    }
    else if (remain!=0)
    {
    odd++;
    }
    else if (remain==0)
    {
    even++;
    }


    }
    System.out.println("zero: "+zero);
    System.out.println("even: "+even);
    System.out.println("odd: "+odd);



    }
    }
    why do the evens get posted as zeros?

  18. #18
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    import java.util.Scanner;
    public class integervaluereader
    {
    public static void main (String[] args)
    {
    int zero = 0;
    int odd = 0;
    int even = 0;
    int place=0;
    double remain=0;
    boolean zb=false;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a number..");
    String input = scan.nextLine();
    int length = input.length();

    // int n = Integer.parseInt(input);

    //char charAt(int index)

    for(int f=0; f<length; f++)
    {
    zb=false;
    int charat = input.charAt(f);
    remain = charat&#37;2;
    if (remain!=0)
    {
    odd++;
    }
    else if ((charat == 0) && (remain==0))
    {
    zb = true;
    zero++;
    }

    else if ((zb == false) &&(remain==0))
    {
    even++;
    }


    }
    System.out.println("zero: "+zero);
    System.out.println("even: "+even);
    System.out.println("odd: "+odd);



    }
    }
    Input:444400005555
    Output:
    Zero:0
    Even:8
    odd:4

    whats wrong?
    Last edited by carlos123; 02-04-2008 at 05:56 AM. Reason: code

  19. #19
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by carlos123 View Post
    why do the evens get posted as zeros?
    You're not even getting the numeric values - remember charAt returns the char values. Add the bold line:
    Java Code:
          ... 
          remain = charat % 2;
    [B]      charat = Character.getNumericValue(charat);      [/B]
          ...
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  20. #20
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    The end result looks like this with my formatting(since it wasn't properly formatted - use code tags, not quote tags).

    Java Code:
    ...
    public class integervaluereader {
      public static void main (String[] args) {
        int zero = 0;
        int odd = 0;
        int even = 0;
        double remain = 0;
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a number..");
        
        String input = scan.nextLine();    
    
        for (int i = 0; i < input.length(); i++) {
          int ch = input.charAt(i);   // returns the char value of the input,
                                      //  thus for the input 34025, you get 
                                      //  char values of 51, 52, 48, 50, 53
                                      //  respectively
          remain = ch &#37; 2;
          ch = Character.getNumericValue(ch);      
          
          if (ch == 0) {
            zero++;
          } else if (remain != 0) {
            odd++;
          } else if (remain == 0) {
            even++;
          }
        }
        
        System.out.println("zero: " + zero);
        System.out.println("even: " + even);
        System.out.println("odd: " + odd); 
      }
    }
    Last edited by CaptainMorgan; 02-04-2008 at 06:52 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •