Results 1 to 16 of 16
  1. #1
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Letter counter homework problem

    I've read a couple of other threads about this problem but I haven't found the answer I'm looking for. My program compiles fine, but run incorrectly. Could som one look it over and let me know what I'm doing wrong?
    Here is my code:

    import java.util.Scanner;

    /** Lab #4a Programming exercise 5, Letter Counter.
    *
    */

    public class LetterCounter
    {
    public static void main(String[] args)
    {
    String input;
    char letter, lettertwo;
    int chcount = 0, ch, counter = 0 ;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter a word");
    input = keyboard.nextLine();
    ch = input.length();

    System.out.println("Which letter would you like to count?");
    letter = keyboard.nextLine().charAt(ch);

    while (chcount<= ch-1)
    { lettertwo = input.charAt(ch);
    if ( letter == lettertwo)
    {counter++;
    }
    chcount++;
    }

    System.out.println("Letter: "+letter+" appears "+counter+" times in the word " +input);

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Letter counter homework problem

    Your post is missing key information:

    • What is your program supposed to be doing?
    • Are you seeing any error messages? If so post them.
    • Is it doing anything that it's not supposed to be doing or not doing something that it should be doing?

  3. #3
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    The program is supposed to count the number of times a letter appears in a string.
    Here is my error message : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
    at java.lang.String.charAt(Unknown Source)
    at LetterCounter.main(LetterCounter.java:25)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Letter counter homework problem

    Well, look at line 25 of LetterCounter then. What is that line? What is that line supposed to do? Is that what it's doing?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    Line 25 is supposed to be assigning the variable lettertwo to the first character of the string. Then the next line test if that variable equals the letter variable. Am I missing something?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Letter counter homework problem

    I'd recommend stepping through this with a debugger, or at the very least adding some print statements to help you figure out what's going on.

    For example, what is input? What is ch? What does nextLine() return the second time? How long is it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    I'm a student who is very new to java and I'm using the jgrasp IDE. The debugging feature of jgrasp gave me the above referenced error message. To answer your question: input is the string being searched. ch is the length of that string. nextLine() should return the letter being searched for. It should only be one letter. Can you see where I'm making an error?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Letter counter homework problem

    That's what you think they are, but what are their actual values? Try printing them out, or running through it with a debugger (the compiler is not a debugger)?

    Your error could be in a few places around the variables that I'm talking about. But it doesn't really make sense to take the length of some input, then use that as a value to get a character from some other input. What if that input is shorter? What if it's the same length?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    The debugger on jGRASP gives me the same error message. I'm trying to fix me declarations. I'll post the revised code shortly.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Letter counter homework problem

    A debugger is not the standard runtime you're using to run your program. You'd use a debugger to inspect the values of everything just before the Exception is thrown.

    Hint: What is the length of the String you're calling charAt() on? What index are you trying to access?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    Here is the reworked code. It runs but always returns an answer of 0. Can you see what I'm doing wrong now?
    import java.util.Scanner;

    /** Lab #4a Programming exercise 5, Letter Counter.
    *
    */

    public class LetterCounter
    {
    public static void main(String[] args)
    {
    String input;
    char letter, lettertwo;
    int chcount, counter = 0 ;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter a word");
    input = keyboard.nextLine();
    chcount = input.charAt(0);

    System.out.println("Which letter would you like to count?");
    letter = keyboard.nextLine().charAt(0);

    while (chcount <= input.length())
    {

    lettertwo=input.charAt(chcount);
    if (letter==lettertwo)
    {counter++;
    }
    chcount++;
    }

    System.out.println("Letter: "+letter+" appears "+counter+" times in the word " +input);

    }

    }

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Letter counter homework problem

    Again, print lines are your friend. Look at what you're comparing in your while loop. What is chcount?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    I'm sorry, what do you mean by print lines? I'm very new to programming, all your help is appreciated. chcount is supposed to be the letter in the string that the character is compared to. I thought I initialized chcount after the string input at line 19. My while loop is supposed to run while the chcount is less than the string length, incrementing in each loop and comparing the letter variable to the lettertwo variable located at chcount.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Letter counter homework problem

    By print lines, I mean using System.out.println(chcount); for example, just to get an idea of what's happening in your program.

    So chcount is a char that you read in, right? So why are you comparing it to a length? Does it make sense to ask if J is less than 8? Does it make sense to add one to a letter you're searching for? You can do that because a char is a subset of int, but is that what you mean to be doing?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    I'm trying to get chcount to step through the length of the input, comparing letter to letter two. chcount is an int that is supposed to hold the position of lettertwo. chcount is increased each loop until it equals input.lenght. How can move the comparison through the string?

  16. #16
    Djgnl is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Letter counter homework problem

    Okay I got the last one figured out; but now I have to vary the program to accept input from a file. I think I'm getting lost in the input files and I not getting it right. Can some one look at this section and let me know what I'm doing wrong?

    Java Code:
    					System.out.println("Please enter the file name:");
    					filename= keyboard.nextLine();
    					
    					System.out.println("Please enter the letter you want to count:");
    					letter = keyboard.nextLine().charAt(0);
    					
    					File file = new File(filename);
    					Scanner inputFile = new Scanner (file);
    					
    					while (inputFile.hasNext())
    					{
               			 while (chcount < file.length())//loop to iterate through string
               				 {
                 				lettertwo = file.charAt(chcount);
                 				if (lettertwo==letter)  // nested loop to look for letter in string
                 				 { counter++;
                 				 }

Similar Threads

  1. Counter problem
    By Razpet22 in forum NetBeans
    Replies: 5
    Last Post: 06-14-2011, 08:27 PM
  2. Letter counter program. having trouble understanding it.
    By silverglade in forum New To Java
    Replies: 17
    Last Post: 05-21-2011, 01:06 PM
  3. Help with Homework Problem
    By Holy_Zombies in forum New To Java
    Replies: 6
    Last Post: 10-05-2010, 06:26 AM
  4. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 PM
  5. Homework PREREQUISITE Problem
    By ChrisC in forum New To Java
    Replies: 7
    Last Post: 11-27-2007, 05:36 AM

Posting Permissions

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