run: Enter 1 to monitor Animals 2 for Habitats 3 to exit 2 List of Habitats Ente
ID: 3574442 • Letter: R
Question
run:
Enter 1 to monitor Animals 2 for Habitats 3 to exit
2
List of Habitats
Enter 1 for
Details on penguin habitat
Enter 2 for
Details on bird house
Enter 3 for
Details on aquarium
3
null Habitat - Aquarium Temperature: Varies with output temperature Food source: Added daily *****Cleanliness: Needs cleaning from algae
Enter 1 to monitor Animals 2 for Habitats 3 to exit
3
good bye
Enter 1 to Add Animal 2 to add Habitat 3 to exit
3
BUILD SUCCESSFUL (total time: 24 seconds)
So I have a program that pulls info from text documents. The documents are all correct but when running the program it adds "null" to every line. For example: null Animal - Lion Name: Leo Age: 5 *****Health concerns: Cut on left front paw Feeding schedule: Twice daily instead of just "animal-lion name: leo Age:5 *****health concerncs etce etc. This is the code and the two text files.How do I get :null: to stop showing up in the front of these lines??
code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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());
}
}
}
animals text file:
Details on lions
Details on tigers
Details on bears
Details on giraffes
Animal - Lion
Name: Leo
Age: 5
*****Health concerns: Cut on left front paw
Feeding schedule: Twice daily
Animal - Tiger
Name: Maj
Age: 15
Health concerns: None
Feeding schedule: 3x daily
Animal - Bear
Name: Baloo
Age: 1
Health concerns: None
*****Feeding schedule: None on record
Animal - Giraffe
Name: Spots
Age: 12
Health concerns: None
Feeding schedule: Grazing
habitats text file :
Details on penguin habitat
Details on bird house
Details on aquarium
Habitat - Penguin
Temperature: Freezing
*****Food source: Fish in water running low
Cleanliness: Passed
Habitat - Bird
Temperature: Moderate
Food source: Natural from environment
Cleanliness: Passed
Habitat - Aquarium
Temperature: Varies with output temperature
Food source: Added daily
*****Cleanliness: Needs cleaning from algae
Explanation / Answer
Thanks for sharing the contents of the file. I believe that there is a blank line separating the list and the details in the file which is not appearing in the question here.
The problem is with this line:
details[index]=details[index]+" "+line;
On first execution, details[index] is null and doesn't have any value. Thus you have the null coming in front.
Making the following change would solve the issue:
if(blankLine==1)
{
details=new String[op];
for(int i=0;i<details.length; i++)
details[i]="";
}
This is at the place where details is being intantiated. For both animals as well as habitats file. Instead of using details=new String[op]; we add initialization to empty Strings after this using the following loop:
for(int i=0;i<details.length; i++)
details[i]="";
Hope this helps.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.