Results 1 to 16 of 16
Thread: Letter counter homework problem
- 09-22-2011, 06:42 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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);
}
}
-
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?
- 09-22-2011, 07:02 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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)
- 09-22-2011, 07:04 PM #4
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!
- 09-22-2011, 07:13 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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?
- 09-22-2011, 07:21 PM #6
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!
- 09-22-2011, 07:34 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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?
- 09-22-2011, 07:38 PM #8
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!
- 09-22-2011, 07:48 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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.
- 09-22-2011, 07:56 PM #10
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!
- 09-22-2011, 08:07 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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);
}
}
- 09-22-2011, 08:13 PM #12
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!
- 09-22-2011, 08:25 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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.
- 09-22-2011, 08:27 PM #14
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!
- 09-22-2011, 08:40 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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?
- 09-24-2011, 02:37 AM #16
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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
-
Counter problem
By Razpet22 in forum NetBeansReplies: 5Last Post: 06-14-2011, 08:27 PM -
Letter counter program. having trouble understanding it.
By silverglade in forum New To JavaReplies: 17Last Post: 05-21-2011, 01:06 PM -
Help with Homework Problem
By Holy_Zombies in forum New To JavaReplies: 6Last Post: 10-05-2010, 06:26 AM -
Problem using thread +rmi in my homework
By IbrahimAbbas in forum Threads and SynchronizationReplies: 10Last Post: 04-14-2008, 09:24 PM -
Homework PREREQUISITE Problem
By ChrisC in forum New To JavaReplies: 7Last Post: 11-27-2007, 05:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks