Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

count a certain letter in a type statement.

Thread view: 
jcsencsits@yahoo.com - 23 Jan 2007 19:39 GMT
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);
 
 }
}
Oliver Wong - 23 Jan 2007 23:51 GMT
>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
jcsencsits@yahoo.com - 24 Jan 2007 00:28 GMT
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.

> <jcsencs...@yahoo.com> wrote in messagenews:1169581171.928377.257750@l53g2000cwa.googlegroups.com...>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
[quoted text clipped - 3 lines]
>
>     - Oliver
Gordon Beaton - 24 Jan 2007 07:15 GMT
> 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

Signature

[ 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

jcsencsits@yahoo.com - 24 Jan 2007 14:40 GMT
it works for one word but when i do a sentence is when it fails

> > Enter a string: jason is jack
>
[quoted text clipped - 8 lines]
> [ 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 - 24 Jan 2007 14:48 GMT
> 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

Signature

[ 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.