count a certain letter in a type statement.

Discussion in 'Java' started by jcsencsits@yahoo.com, Jan 23, 2007.

  1. Guest

    I am trying to get this to print the number of J's there are in the
    users input. and then ask if they want to try again. can someone help
    me ecause it doesn't count all lettr J's


    import java.util.Scanner;

    class countletter
    {
    public static void main(String[] args) {
    int numOfJ = 0;
    int control = 1;
    int i = 0;
    String randomString = " ";
    char charLetter;

    do {
    numOfJ = 0; //j,J

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a string: ");
    randomString = scan.next();

    for (int letter = 0; letter < randomString.length(); letter++) {
    charLetter = randomString.charAt(letter);

    if (charLetter == 'j' || charLetter == 'J') {
    numOfJ++;
    } }


    System.out.println("\nNo. of 'J' / 'j' = " + numOfJ);

    System.out.println(
    "If you would like to run the program for another string
    please enter 1.");
    control = scan.nextInt();
    }
    while (control == 1);

    }
    }
    , Jan 23, 2007
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    >I am trying to get this to print the number of J's there are in the
    > users input. and then ask if they want to try again. can someone help
    > me ecause it doesn't count all lettr J's
    >

    [code snipped]

    It works when I test it.

    - Oliver
    Oliver Wong, Jan 23, 2007
    #2
    1. Advertising

  3. Guest

    Enter a string: jason is jack

    No. of 'J' / 'j' = 1
    If you would like to run the program for another string please enter 1.
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at countletter.main(Project5B.java:36)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    On Jan 23, 6:51 pm, "Oliver Wong" <> wrote:
    > <> wrote in messagenews:...>I am trying to get this to print the number of J's there are in the
    > > users input. and then ask if they want to try again. can someone help
    > > me ecause it doesn't count all lettr J's[code snipped]

    >
    > It works when I test it.
    >
    > - Oliver
    , Jan 24, 2007
    #3
  4. On 23 Jan 2007 16:28:06 -0800, wrote:
    > Enter a string: jason is jack
    >
    > No. of 'J' / 'j' = 1


    You need to read the documentation for Scanner.next().

    Doing so might also help you understand why you get the
    following exception:

    > Exception in thread "main" java.util.InputMismatchException


    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jan 24, 2007
    #4
  5. Guest

    it works for one word but when i do a sentence is when it fails

    On Jan 24, 2:15 am, Gordon Beaton <> wrote:
    > On 23 Jan 2007 16:28:06 -0800, wrote:
    >
    > > Enter a string: jason is jack

    >
    > > No. of 'J' / 'j' = 1You need to read the documentation for Scanner.next().

    >
    > Doing so might also help you understand why you get the
    > following exception:
    >
    > > Exception in thread "main" java.util.InputMismatchException/gordon

    >
    > --
    > [ don't email me support questions or followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
    , Jan 24, 2007
    #5
  6. On 24 Jan 2007 06:40:56 -0800, wrote:
    > it works for one word but when i do a sentence is when it fails


    That was clear from your earlier example. Scanner.next() does not
    return the entire line entered by the user, as you seem to expect. Did
    you read its documentation as I suggested?

    Try displaying the contents of the randomString along with the
    corresponding J count.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jan 24, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    750
    Reinhold Birkenfeld
    Jul 6, 2004
  2. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    501
    Tony Meyer
    Jul 6, 2004
  3. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    749
    Scott David Daniels
    Jul 6, 2004
  4. Paolo Capriotti
    Replies:
    1
    Views:
    133
    Josef 'Jupp' SCHUGT
    Nov 20, 2005
  5. Dung Ping

    Can Javascript count letter frequency?

    Dung Ping, Aug 31, 2005, in forum: Javascript
    Replies:
    7
    Views:
    186
    Dung Ping
    Aug 31, 2005
Loading...

Share This Page