Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Strings

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Strings

    Write a Java program which reads in a string from the keyboard and then reports how many times each letter of the alphabet occurs within that string (ignoring case). It should also report how many non-letters are included in the string.

    Import B102.*;  
     
    class_prog1
    {
    public static void UserInput()
    {
    public String Data = new String()
    Screen.out.println("Please enter some characters values:");
    String.toLowerCase()
    Data=keybd.in.readchar();
    return Data;
    }
     
    public static void Calculate()
    {
    private char calcArray[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    private int index;
    for(index=0; i<?; index++)
    }

    Just wondering If I'm on the right track for this program? Not to sure what else I should do
    Last edited by helloworld922; November 3rd, 2009 at 09:56 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Strings

    You can use the Character.isLetter() method:

    Also, fyi don't initialize strings by creating a new String. Strings are immutable, so doing so is just wasting time and memory.

    String data = "test string";
    int letterCount = 0;
    for (int i = 0; i < data.length(); i++)
    {
        if (Character.isLetter(data.charAt(i))
        {
              letterCount++;
         }
    }

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings

    thank you, I will attempt to start it

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Strings

    Oh, haha I didn't read the the first part of the question. For that portion, you can "cheat" by taking advantage of the underlying Unicode representation of characters. You will have to convert the string to all lower-case or all upper-case, though.

    int[] lettersCount = new int[26];
    char letter = 'a';
    lettersCount[(int)letter-'a']++;

Similar Threads

  1. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM
  2. Help me this code! Someone please explain strings!
    By helpthiscode in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2009, 03:13 AM
  3. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  4. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM