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)                  
		{                  
			charCount++;                  
		}      
	}      
	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

Member Avatar for ztini
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() {
		display();
	}
	
	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)
						count++;
				}
			}
			
			bufferedReader.close();
			fileReader.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		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);

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

	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.

commented: We don't do people's homework for them. -3

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.

Grab the latest edition of this book Java How to program read it then you can come to this forum with more meaningful questions

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!

sorry dude my bad; didnt read right.

No worries there :)
Lets get back to solving his problem, shall we?

@gatechie: Just give the following link a read:

http://docs.oracle.com/javase/tutorial/essential/io/index.html

(I'm not trying to be insulting or any of that stuff, I'm just saying that this will give a you a clear idea of file I/O. :) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.