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?
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)
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?
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?
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?
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?
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?
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.
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?
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);
}
}
Re: Letter counter homework problem
Again, print lines are your friend. Look at what you're comparing in your while loop. What is chcount?
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.
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?
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?
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?
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++;
}