Q: Explain each line and what it is doing in the Java Code below... CODE: import
ID: 3595728 • Letter: Q
Question
Q: Explain each line and what it is doing in the Java Code below...
CODE:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Chegg {
public static void main(String[] args) throws IOException {
//JOptionPane.showMessageDialog(null, "Alter Zookeeper Abnormal state");
Scanner sc=new Scanner(System.in);
BufferedReader brA=new BufferedReader(new FileReader("g://files/animals.txt"));
BufferedReader brH=new BufferedReader(new FileReader("g://files/habitats.txt"));
BufferedWriter bwA=new BufferedWriter(new FileWriter("g://files/animals.txt",true));
BufferedWriter bwH=new BufferedWriter(new FileWriter("g://files/habitats.txt",true));
int option=0;
while(option!=3)
{
brA=new BufferedReader(new FileReader("g://files/animals.txt"));
brH=new BufferedReader(new FileReader("g://files/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, "Alter Zookeeper Abnormal state");
}
}
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, "Alter Zookeeper Abnormal state");
}
}
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 no");
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
//import statements
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 Chegg {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// JOptionPane.showMessageDialog(null,
// "Alter Zookeeper Abnormal state");
// scanner to read from keyboard
Scanner sc = new Scanner(System.in);
// bufferedreader to point the animals.txt file
BufferedReader brA = new BufferedReader(new FileReader(
"g://files/animals.txt"));
// bufferedreader to point the habitats.txt file
BufferedReader brH = new BufferedReader(new FileReader(
"g://files/habitats.txt"));
// bufferedwriter to point the animals.txt file
BufferedWriter bwA = new BufferedWriter(new FileWriter(
"g://files/animals.txt", true));
// bufferedwriter to point the habitats.txt file
BufferedWriter bwH = new BufferedWriter(new FileWriter(
"g://files/habitats.txt", true));
// initialize option to 0
int option = 0;
// repeat the loop upto user selects option 3
while (option != 3) {
// again bufferedreader initialize to animals
brA = new BufferedReader(new FileReader("g://files/animals.txt"));
// again bufferedreader initialize to habitats.txt
brH = new BufferedReader(new FileReader("g://files/habitats.txt"));
// prompt for choice
System.out
.println("Enter 1 to monitor Animals 2 for Habitats 3 to exit");
// read the choice
option = sc.nextInt();
String[] details = null;
// if option/choice is 1 then read the animals data from file
if (option == 1) {
String line;
System.out.println("List of animals");
int op = 0;
int blankLine = 0;
int seperateSection = 0;
int index = -1;
// read the line from animals.txt
while ((line = brA.readLine()) != null) {
seperateSection = 0;
if (line.equals("")) {
blankLine++;
if (blankLine == 1) {
details = new String[op];
}
seperateSection = 1;
index++;
}
// print the line empty
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;
}
}
// close the file
brA.close();
int choose = sc.nextInt();
System.out.println(details[choose - 1]);
if (details[choose - 1].contains("****")) {
JOptionPane.showMessageDialog(null,
"Alter Zookeeper Abnormal state");
}
} else if (option == 2) {
String line;
System.out.println("List of Habitats");
int op = 0;
int blankLine = 0;
int seperateSection = 0;
int index = -1;
// read the line from habitats.txt
while ((line = brH.readLine()) != null) {
seperateSection = 0;
if (line.equals("")) {
blankLine++;
if (blankLine == 1) {
details = new String[op];
}
seperateSection = 1;
index++;
}
// print the line is empty
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;
}
}
// close the habitats.txt file
brH.close();
int choose = sc.nextInt();
System.out.println(details[choose - 1]);
if (details[choose - 1].contains("****")) {
JOptionPane.showMessageDialog(null,
"Alter Zookeeper Abnormal state");
}
} else if (option == 3)
// print bye
System.out.println("good bye");
else
// print wrong option
System.out.println("Wrong option");
}
// select 1 or 2 to add the animal to the animals.txt or add the
// habitats to the habitats.txt file
System.out.println("Enter 1 to Add Animal 2 to add Habitat 3 to no");
// enter the choice
int ch = sc.nextInt();
if (ch == 1) {
// take new line for adding animal
bwA.newLine();
// prompt to enter animal name, keeper name, age and health report
// and write these to animals.txt file
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) {
// take new line for adding habitat
bwH.newLine();
// prompt to enter habitat name, keeper name, age and health report
// and write these to habitate.txt file
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());
}
}
}
NOTE: please check the comments for every java statement with explanation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.