I'm just starting out in java, and I am totally stumped on this assignment. I have to make a program where the user selects a single character and then a file.
The program is then supposed to check each character in the file, and output how many times that character shows up.
Here is the code I have written.
import java.util.Scanner;
import java.io.*;
public class LetterCount
{
public static void main (String[]args) throws IOException
{
char lookUp;
String input;
String fileName;
Scanner kB = new Scanner(System.in);
int tracker = 0;
/** Introduction*/
System.out.println("Welcome to the Letter Master 9000.");
System.out.println("Please enter a character you would like to count.");
/** users defined character they want to look up*/
input = kB.nextLine();
/** grabbing the file name*/
System.out.println("Please enter the location of the file you would like to use:");
fileName = kB.nextLine();
/** opening the file*/
System.out.println("I will now examine this file.");
File file= new File(fileName);
Scanner inputFile = new Scanner(file);
/** Loop created to find and track the user defined character*/
while (inputFile.hasNext())
{
String line = inputFile.nextLine();
if (line == input)
{
tracker = tracker++;
System.out.println("yes"); //this is used to verify it is working; it isn't :(
}
}
inputFile.close();
System.out.println(tracker);
}
}