Soo my program is working...but there is just one problem...null is also being printed along with the content or string from the file... is there anyway i can stop null from being printed. The code read asks a user to give a filename .txt then the user is asked a the character he/she wold like to be searched and the program searches for the that specified character in the string , prints out the content of the text file and tells how many times it was found. The problem is after it prints out the contents of the text file (string) it also prints oout the word null but null is not in the txt file.

import java.util.Scanner;
    import java.io.*;
    
    public class filefinder
     {
       public static void main(String[] args) throws IOException
       {
    
              
             //needed for scanner class
              Scanner kb = new Scanner(System.in);
              
              int charCount = 0;
        		String filename = "";
        		String str = "";
        		String line = "";
        		boolean isString = false;
        		boolean fileFound = false;
        		FileReader freader;
              
              // get users string
        		while (!fileFound)
        		{
              while (!isString)
        		{   
        		try { Thread.currentThread().sleep(500); }
                    catch ( Exception e ) { }
        
        		System.out.println("Please enter a filename: ");
        		System.out.println("");
              filename = kb.nextLine();
        		if (filename != null)
        			   isString = true;
        	   }//end inner while loop
        		
        		// open file
        		try 
        		{
        			freader = new FileReader(filename);		
        		
        		fileFound = true;
        		
        		
        		Thread.currentThread().sleep(1500);
        		}
        		catch (Exception e)
        		{
        		
        		System.out.println("");
        		System.out.println("The system cannot find the file specified.");
        		System.out.println("");
        		isString = false;
        		
        		
        		}//end try-catch
        		}//end outer while loop
        		
        		try { Thread.currentThread().sleep(1000); }
              catch ( Exception e ) { }
        
        	   freader = new FileReader(filename);
        		BufferedReader inputFile = new BufferedReader(freader);
        		
        		// Read first line from file
        		while (line !=null)
        		{
        		
        		line = inputFile.readLine();
        		
        		if (line != null)
        		
        		
        		try { Thread.currentThread().sleep(1000); }
              catch ( Exception e ) { }
        					         
        			 str = str+ "\n" + line; 
        		}//end while
        		
        		System.out.println(str);
        		
        		inputFile.close();
        		
              // get users character
        		System.out.println("");
              System.out.println("Please enter a character you want to find: ");
        		System.out.println("");
              char userChar = kb.nextLine().charAt(0);
        		
        						
        		
              
              
        				 
        				while(str.length()>0){
        				for(int i= 0;i<str.length();i++){
                                    if(str.charAt(i)==userChar)
                            charCount++;
        						  
                    }
        								  
        
              System.out.println("\n The entered character " +"\"" + userChar +
                                  "\"  inside  " + filename +
                                  "was found " + charCount +   " times.\n");         
        								  break;  
        								   
            }
        }
        }

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

1. Post your code in code tags so we can read it
2

if (line != null)
  try { Thread.currentThread().sleep(1000); }
  catch ( Exception e ) { }
str = str+ "\n" + line;

If line is null you don't sleep, but you still print it

1. Post your code in code tags so we can read it
2

if (line != null)
  try { Thread.currentThread().sleep(1000); }
  catch ( Exception e ) { }
str = str+ "\n" + line;

If line is null you don't sleep, but you still print it

how do i post in tags?

This article has been dead for over six months. Start a new discussion instead.