Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So i wrote this code for my java final. Whenever I run the program when i select

ID: 3574407 • Letter: S

Question

So i wrote this code for my java final. Whenever I run the program when i select any of the options it says null before any of my strings. So an example is if i select 1 it says "null you selected penguins" instead of just "you selected penguins." Can anyone help me figure out how to get null out of there?? Code is below. Thank you!!

package habitat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
*
*/
public class Habitat {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
       public static void main(String[] args) throws IOException {
     
       Scanner sc=new Scanner(System.in);
    
       BufferedReader brA=new BufferedReader(new FileReader("c:/animals.txt"));
     
       BufferedReader brH=new BufferedReader(new FileReader("c:/habitats.txt"));
     
       BufferedWriter bwA=new BufferedWriter(new FileWriter("c:/animals.txt",true));
       BufferedWriter bwH=new BufferedWriter(new FileWriter("c:/habitats.txt",true));

     
       int option=0;
     
       while(option!=3)
       {
        
           brA=new BufferedReader(new FileReader("c://animals.txt"));
           brH=new BufferedReader(new FileReader("c://habitats.txt"));
         
       System.out.println("Enter 1 to monitor Animals 2 for Habitats 3 to exit");
       option=sc.nextInt();
     
       String[] details=null;
       if(option==1)
       {
         
           String line;
        
           System.out.println("List of animals");
        
           int op=0;
           int blankLine=0;
           int seperateSection=0;
           int index=-1;
         
           while((line=brA.readLine())!=null)
           {
               seperateSection=0;
            
             
               if(line.equals(""))
               {
                   blankLine++;
                   if(blankLine==1)
                   {
                     
                       details=new String[op];
                   }
                   seperateSection=1;
                 
                   index++;
               }
               if(blankLine==0)
               {
                
               op++;
               System.out.println("Enter "+op+" for");
               System.out.println(" "+line);
               }
               else if(blankLine!=0 && seperateSection==0)
               {
                 
                   details[index]=details[index]+" "+line;
               }
            
            
           }
         
           brA.close();
           
         
           int choose=sc.nextInt();
           System.out.println(details[choose-1]);
         
           if(details[choose-1].contains("****"))
           {
             
               JOptionPane.showMessageDialog(null, "Zoo Keeper In Wrong");
           }
    
        
       }
     
       else if (option==2)
       {
String line;
        
           System.out.println("List of Habitats");
           int op=0;
           int blankLine=0;
           int seperateSection=0;
           int index=-1;
           while((line=brH.readLine())!=null)
           {
               seperateSection=0;
            
               if(line.equals(""))
               {
                   blankLine++;
                   if(blankLine==1)
                   {
                       details=new String[op];
                   }
                   seperateSection=1;
                   index++;
               }
               if(blankLine==0)
               {
               op++;
               System.out.println("Enter "+op+" for");
               System.out.println(" "+line);
               }
               else if(blankLine!=0 && seperateSection==0)
               {
                   details[index]=details[index]+" "+line;
               }
            
            
           }
           brH.close();
           int choose=sc.nextInt();
           System.out.println(details[choose-1]);
           if(details[choose-1].contains("****"))
           {
               JOptionPane.showMessageDialog(null, "Zookeeper In Wrong");
                     
           }
       }
     
       else if(option==3)
           System.out.println("good bye");
       else
           System.out.println("Wrong option");
       }
       System.out.println("Enter 1 to Add Animal 2 to add Habitat 3 to exit");
       int ch=sc.nextInt();
       if(ch==1)
       {
           bwA.newLine();
           System.out.println("Enter animal name:");
           bwA.write("Animal - "+sc.next());
           System.out.println("Enter keeper name");
           bwA.write(sc.next());
           System.out.println("Enter age:");
           bwA.write(sc.next());
           System.out.println("Enter health report");
           bwA.write(sc.next());
        
       }
       if(ch==2)
       {
           bwH.newLine();
           System.out.println("Enter habitat name:");
           bwA.write("Habitat - "+sc.next());
           System.out.println("Enter keeper name");
           bwA.write(sc.next());
           System.out.println("Enter age:");
           bwA.write(sc.next());
           System.out.println("Enter health report");
           bwA.write(sc.next());
        
       }
    
    
   }

}
  
   

Explanation / Answer

This can be caused by wrong information in the file animals.txt. Try removing ^M characters. Make sure the lines in the file are clear with no other extra characters. Try seeing the exact characters in Notepad ++.

Also the BufferedWriter objects are not closed. After the end of the program it should always close the object or result in corrupting the files. To do this add the following 2 lines at the end:
bwA.close();
bwH.close();

Can you also add the contents of animals.txt for a better understanding.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote