Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello. I would like help with fixing my code. The deleteCountry() doesn't delete a country from the countries.txt. My Code: import java.io.FileReader; import java.io.FileWriter; import

Hello. I would like help with fixing my code. The deleteCountry() doesn't delete a country from the countries.txt.

My Code:

import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.BufferedWriter; import java.util.ArrayList; import java.util.Scanner; import java.io.IOException;

public class AC_CountryIO { ArrayList arrList = new ArrayList<>(200); int listSize = 0; int firstReadFlag = 0; public static void main(String[] args) throws IOException, Exception { AC_CountryIO obj = new AC_CountryIO(); Scanner inp = new Scanner(System.in); String ch; String coun = new String(); System.out.println(" Country List Manager "); System.out.println("COMMAND MENU: 1 - List Countries 2 - Add a Country 3 - Delete a Country 4 - Exit"); while(true) { System.out.print(" Enter menu number: "); ch = inp.next(); switch(ch) { case "1": obj.getCountries(); System.out.println("ListSize: " + obj.listSize); for(int i=0;i System.out.println(obj.arrList.get(i)); } break; case "2": System.out.print("Enter country: "); coun = inp.next(); obj.arrList.add(coun); obj.listSize++; if(obj.saveCountries(obj.arrList)) { System.out.println("This country has been saved."); } break; case "3": System.out.println("Enter the country you want to delete: "); String deleteCountry = inp.next(); obj.deleteCountry(deleteCountry); break; case "4": System.out.println("Goodbye."); inp.close(); System.exit(0); default: System.out.println("Please enter a valid menu number. "); } } } public ArrayList getCountries() throws IOException{ FileReader readFile = null; BufferedReader readBuff = null; try { readFile = new FileReader("countries.txt"); readBuff = new BufferedReader(readFile); if(firstReadFlag == 0) { String line = new String(); int i = 0; while((line = readBuff.readLine()) != null && i<195) { line = line.trim(); this.arrList.add(line); i++; } this.listSize = this.arrList.size(); readBuff.close(); readFile.close(); firstReadFlag = 1; } } catch(IOException iexcep) { FileWriter writeFile = null; BufferedWriter writeBuff = null; try { writeFile = new FileWriter("countries.txt"); writeBuff = new BufferedWriter(writeFile); } catch(IOException oexcep) { System.out.println(" Cannot create \"countries.txt\" file. Exiting..."); System.exit(0); } firstReadFlag = 1; writeBuff.close(); writeFile.close(); System.out.println(" No existing file named \"countries.txt\" was found. Created a new file named \"countries.txt\""); } return this.arrList; } public boolean saveCountries(ArrayList countries) throws IOException { FileWriter writeFile = null; BufferedWriter writeBuff = null; try { writeFile = new FileWriter("countries.txt"); writeBuff = new BufferedWriter(writeFile); } catch(IOException oexcep) { System.out.println(" Cannot find nor create \"countries.txt\" file. Exiting..."); System.exit(0); } String line = new String(); for(int i=0;i line = this.arrList.get(i); writeBuff.write(line); writeBuff.newLine(); } writeBuff.close(); writeFile.close(); return true; } public void deleteCountry(String deleteCountry) throws IOException, Exception { for (int i=0;i if(deleteCountry.equalsIgnoreCase(this.arrList.get(i))) { this.arrList.remove("deleteCountry"); } else { throw new Exception("Country not found"); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

What is a verb?

Answered: 1 week ago

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago