Hi all,
I am trying to work out how to use the try and catch methods in Java. I am new if you havent guessed and I am wondering how I can change my code which searches for a particular user id which is inputted and when found it opens the file corresponding to the name. I am having trouble working out how when the person is not found it throws the file not found exception. I am hoping to sidestep this and loop the code back to the add customer section.

Thanks in advance
Greg

import java.io.*;
 import java.util.*;
   
   public class AddProperty {
    
    public static void main(String[] args)throws Exception {
   
      String check2 = "NEW";
      // this is a section creatd to implement in larger assignment
      
      Scanner addScan = new Scanner(System.in);
      System.out.println("Please Enter the Landlords ID Number?");
      System.out.println("If a new Landlord type   NEW ");
   
      String check = addScan.nextLine(); 
      check = check.toUpperCase();
      //possible to loop here if file not found 
      if (check.equals(check2)) {
         System.out.println("Taking you to the new landlord section");
         // Will be adding code when completed the lower section
         }
         else
         /* Search for landlord which will be loaded in the initial setup
          * I am looking to have the user write a code i.e. ll001
          * which is linked to its own file. This will then load the information 
          * to be edited or updated.
          */  
          
         {System.out.println("Starting search for Landlord");
         String line = ""; 
         ArrayList data = new ArrayList();
  
         try { 
            FileReader fr = new FileReader(check + ".txt"); 
            BufferedReader br = new BufferedReader(fr);
   
         while((line = br.readLine()) != null) { 
            data.add(line); 
            } 
         } 
         catch(FileNotFoundException fN) 
         { 
         fN.printStackTrace(); 
         } 
      catch(IOException e) { 
      System.out.println(e); 
      } 
 
      System.out.println(data);
      }
    }
}

having trouble working out how when the person is not found it throws the file not found exception.

Have you looked at the File exists() method?

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