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

Below is the java code, pls make sure that the program keeps looping if choice 1

ID: 3710496 • Letter: B

Question

Below is the java code, pls make sure that the program keeps looping if choice 1,2,3 are used and add option 4 to it for closing the program and pls use while loop.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Scanner;

public class Flora {

public static void main(String[] args) throws FileNotFoundException
{
Hashtable < String, String > hash = new Hashtable < String, String > ();
BufferedReader rd = new BufferedReader(new FileReader("flora.csv"));
String line;

int i = 0;
try {
while ((line = rd.readLine()) != null)
{
String[] lineArray = line.split(",");
hash.put(lineArray[0], lineArray[1]);
i++;
}
} catch (IOException e)
{

e.printStackTrace();
}

System.out.println(hash.size() + " flora read into a hashtable");

System.out.println("1 = Search for a flora");
System.out.println("2 = Add a new flora");
System.out.println("3 = Delete a flora");
Scanner input = new Scanner(System.in);
System.out.print("Your choice:");
int choice = input.nextInt();
if (choice == 1)
{  
Scanner floraInput = new Scanner(System.in);
System.out.print("Flora Name:");
String floraName = floraInput.nextLine();
if (!hash.get(floraName).isEmpty())
System.out.println("This flora was found. Family = " + hash.get(floraName));
else
System.out.println("This flora was not found");
} else if (choice == 2)
{  
Scanner floraInput = new Scanner(System.in);
System.out.print("Flora Name:");
String floraName = floraInput.nextLine();
Scanner floraFamilyInput = new Scanner(System.in);
System.out.print("Flora Family:");
String floraFamilyName = floraFamilyInput.nextLine();
hash.put(floraName, floraFamilyName);
System.out.println("flora added successfully");
}
else if (choice == 3)
{
Scanner floraInput = new Scanner(System.in);
System.out.print("Flora Name:");
String floraName = floraInput.nextLine();
hash.remove(floraName);
System.out.println("flora deleted successfully");
}
else
{
System.out.println("Enter a valid Option");
}

}

}

Explanation / Answer

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Hashtable; import java.util.Scanner; public class Flora { public static void main(String[] args) throws FileNotFoundException { Hashtable hash = new Hashtable (); BufferedReader rd = new BufferedReader(new FileReader("flora.csv")); String line; int i = 0; try { while ((line = rd.readLine()) != null) { String[] lineArray = line.split(","); hash.put(lineArray[0], lineArray[1]); i++; } } catch (IOException e) { e.printStackTrace(); } System.out.println(hash.size() + " flora read into a hashtable"); while (true) { System.out.println("1 = Search for a flora"); System.out.println("2 = Add a new flora"); System.out.println("3 = Delete a flora"); System.out.println("4 = Close program"); Scanner input = new Scanner(System.in); System.out.print("Your choice:"); int choice = input.nextInt(); if (choice == 1) { Scanner floraInput = new Scanner(System.in); System.out.print("Flora Name:"); String floraName = floraInput.nextLine(); if (!hash.get(floraName).isEmpty()) System.out.println("This flora was found. Family = " + hash.get(floraName)); else System.out.println("This flora was not found"); } else if (choice == 2) { Scanner floraInput = new Scanner(System.in); System.out.print("Flora Name:"); String floraName = floraInput.nextLine(); Scanner floraFamilyInput = new Scanner(System.in); System.out.print("Flora Family:"); String floraFamilyName = floraFamilyInput.nextLine(); hash.put(floraName, floraFamilyName); System.out.println("flora added successfully"); } else if (choice == 3) { Scanner floraInput = new Scanner(System.in); System.out.print("Flora Name:"); String floraName = floraInput.nextLine(); hash.remove(floraName); System.out.println("flora deleted successfully"); } else if(choice == 4) { break; } else { System.out.println("Enter a valid Option"); } System.out.println(); } } }
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