i have a problem that i have been working on. i'm in need of some desperate help for someone can show me how to make this program work.

import java.util.Scanner; 
import java.io.*;

public class LettersCounter
	public static void main(String[] args) throws IOExceptions   
	//needed for scanner class      
	Scanner keyboard = new Scanner(System.in);       
	int charCount = 0;
	String filename;
	String userString;     
	// get users string      
	System.out.println("Please enter a file name: ")  
	String userString = keyboard.nextLine();      
	// get users character      
	System.out.print("Please enter a character: ");      
	char userChar = keyboard.nextLine().charAt(0);      
	// tell the user what the program does      
	System.out.println("\n****This program will return how many times" +                          
	" the character you entered showed up in" +                          
	" the string you entered.****");       
	for(int i= 0;i<userString.length();i++)      
		if (userString.charAt(i) == userChar)                  
	System.out.println("\nThe specified character " +"\"" + userChar +                          
			"\" is inside the string " + userString +                          
			" "+ charCount +   " times.\n");                

this program has been keeping me up. i need some help

4 Years
Discussion Span
Last Post by dantinkakkar
public static void main(String[] args) throws IOExceptions

should be

public static void main(String[] args) throws IOException

You're instantiating userString twice and you forgot a semicolon on your println:

String userString;     
	// get users string      
	System.out.println("Please enter a file name: ")  
	String userString = keyboard.nextLine();

Should read:

String userString;     
	// get users string      
	System.out.println("Please enter a file name: ");
	filename = keyboard.nextLine();

Oy, you probably want something like this:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Scanner; 

public class FileParser {
	public FileParser() {
	public int getCharacterCount(File file, char _char) {
		int count = 0;
		try {
			FileReader fileReader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			while(bufferedReader.ready()) {
				String line = bufferedReader.readLine();
				for (int i = 0; i < line.length(); i++) {
					if (line.charAt(i) == _char)
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		return count;
	public void display() { 
		Scanner in = new Scanner(System.in);
		System.out.print("Filename to parse: ");
		String fileName = in.nextLine();
		File file = new File(fileName);

		System.out.print("Character to parse for: ");
		char parseChar = in.next().charAt(0);

				MessageFormat.format("Found ''{0}'' a total of {1} times in {2}",
				getCharacterCount(file, parseChar), 

	public static void main(String[] args) { 
		new FileParser();
Filename to parse: C:/Users/Foo/Desktop/test.txt
Character to parse for: r
Found 'r' a total of 2 times in test.txt

It's late and I'm tired. Post if don't understand the above.

Edited by ztini: n/a

We don't do people's homework for them.

thanks for the help. there's only one problem with the code, i have to use a printwriter instead of the bufferreader. so how would i use the printwriter in this code so i can get the same outcome.


You are reading from a file not writing to a file so PrintWriter is out of the question. you are already using java.util.Scanner to read user input you can also use it to scan through the file.

@ztini this dude can barely understand his own code how in the world his he going to understand yours without any help?


@gatechie, can you be a little more clear? I really don't understand how you're gonna use a PrintWriter instead of a BufferedReader !


@dantinkakkar didn't u read my post. Java io is divided into 2 parts. Writers for writing data to disk or anyother output medium and Readers for reading data from input mediums.

you are trying to count the number of occurrences of a character in a file. This requires you to read the file from disk, for this task you have to use a Reader. A PrintWriter class is a writer so you are not going to be able to use it here. If your code required you to read the result into a file then thats where Writer (PrintWriter) would come in.


@ejosiah, you didn't understand what I said... That's what I meant! How can someone replace an input reader with an output writer! Tha's what I meant.

Edited by dantinkakkar: n/a


What? Why should I do that? You're still not understanding me - I'M SAYING: you cannot replace an input reader with an output writer!

Edited by dantinkakkar: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.