Advertisement
spmwilson

Program

Apr 28th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.PrintWriter;
  5.  
  6. public class fileLab {
  7.         public static void main(String[] args) throws Exception {
  8.  
  9.                 //lab in progress
  10.                 File file;
  11.                         Scanner fileScanner;
  12.                 //names scanner to search "file"
  13.                                 file = new File("Cool.txt");
  14.                                 fileScanner = new Scanner(file);
  15.                
  16.                 //prompts user to choose a character
  17.                 Scanner sc = new Scanner(System.in);
  18.                 System.out.println("Choose a charater to search for");
  19.                 int userChar = sc.next().charAt(0);
  20.  
  21.                 //count variable for how many times the character appears
  22.                 int count = 0;
  23.                
  24.                 //this loop will read each int until the file has no more ints
  25.                 while(fileScanner.hasNext()){
  26.                
  27.                         //this will get the next char in the file
  28.                         int c = fileScanner.findInLine(".").charAt(0);
  29.  
  30.                         //this makes sure its the same char as the user put in
  31.                         if (c == userChar){
  32.                                 count = count + 1;
  33.                      
  34.                         }
  35.                 }
  36.                
  37.                 System.out.println("There are "+ count +" of that letter in the file");
  38.                
  39.                
  40.         }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement