Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.PrintWriter;
- public class fileLab {
- public static void main(String[] args) throws Exception {
- //lab in progress
- File file;
- Scanner fileScanner;
- //names scanner to search "file"
- file = new File("Cool.txt");
- fileScanner = new Scanner(file);
- //prompts user to choose a character
- Scanner sc = new Scanner(System.in);
- System.out.println("Choose a charater to search for");
- int userChar = sc.next().charAt(0);
- //count variable for how many times the character appears
- int count = 0;
- //this loop will read each int until the file has no more ints
- while(fileScanner.hasNext()){
- //this will get the next char in the file
- int c = fileScanner.findInLine(".").charAt(0);
- //this makes sure its the same char as the user put in
- if (c == userChar){
- count = count + 1;
- }
- }
- System.out.println("There are "+ count +" of that letter in the file");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement