Okay, so I have a project due in a friday and my Nana passed away this morning so I really have to work at double speed to get it in for friday seeing as I will have funeral and such in the coming days.
With my code, I was wondering how to implement a File Input and Output system with exception handling into my current program.
The current program is used for making arraylists of a Hero Character, you can add a Hero with a name, age, attack and defense points.
The main thing I need to do now is just do some fileIO to allow for the information to be saved to a file...
Thanks in advance <3
package myHero; /*The aim of this project is to allow the user to input data which will give structure to a character known as HERO. There can be several Heroes, more can be added using the create a Hero method.*/ import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; //Creates a class with the Scanner for user input and also declares the ArrayLists for each element of the Hero we are to create. public class HeroCreate { Scanner input = new Scanner(System.in); //Elements of the Hero such as Name, Attack and Defense. List<String> hName = new ArrayList<>(); List<Integer> hAge = new ArrayList<>(); List<String> hRace = new ArrayList<>(); List<Integer> hAttack = new ArrayList<>(); List<Integer> hDefense = new ArrayList<>(); //This acts as the HUB for the user to enter in a number to choose from the options in the list. public void StartPanel() { System.out.println("\n" + "WELCOME TO HERO CREATOR!"); System.out.println("1. Create a Hero Now!"); System.out.println("2. Delete An Existing Hero?"); System.out.println("3. Show Current Heroes!"); System.out.println("4. Exit Menu "); //This is where the program identifies which number the user has chosen and links them to the next method. int choice = input.nextInt(); if (choice == 1) { CreateHero(); } else if (choice == 2) { DeleteHero(); ShowTeam(); } else if (choice == 3) { ShowTeam(); StartPanel(); } else if (choice == 4) { StopPro(); } else { System.out.println("Error!"); StartPanel(); } } //This is class which allows for the user to create the HERO, giving it all of its details. //When the user enters the details it is stored in an array corresponding to its role. i.e NAME in the hName arrayList. public void CreateHero() { System.out.print("Name Your Hero: "); input.nextLine(); String newHeroName = input.nextLine(); hName.add(newHeroName); System.out.print("How Old Is Your Hero?: "); int newHeroAge = input.nextInt(); hAge.add(newHeroAge); System.out.print("Which Race is your Hero?: "); input.nextLine(); String newHeroRace = input.nextLine(); hRace.add(newHeroRace); //The following assigns random values between 1 and 100 to the Attack and Defense element of the HERO. int min = 0; int max = 100; Random r = new Random(); int newHeroAtt = r.nextInt(max - min + 1) + min; hAttack.add(newHeroAtt); int newHeroDef = r.nextInt(max - min + 1) + min; hDefense.add(newHeroDef); //A println to declare a character has been added. System.out.println("\n" + "The Hero " + hName.get(hName.size() - 1) + " Was Added!" + "\n"); //A choice to continue creating Heroes or to return to the main menu. System.out.println("Add Another Hero?"); System.out.println("1. Create Another Hero!"); System.out.println("2. Return to Main Menu."); int choice = input.nextInt(); if (choice == 1) { CreateHero(); } else if (choice == 2) { StartPanel(); } } //This method allows for a Hero to be deleted for whatever reason, by removing the elements of a Hero and deleting it from the arrayList. public void DeleteHero() { ShowTeam(); System.out.println(" Which Hero Will Be Deleted? (1 to " + hName.size() + ")"); int delete = input.nextInt(); if (delete > hName.size()) { System.out.println("That is not a choice!"); StartPanel(); } else { delete--; System.out.println("Would you like to delete the following hero?: " + hName.get(delete)); System.out.print("1 for YES or 2 for NO" + "\n"); int confirmDelete = input.nextInt(); if (confirmDelete == 1) { hName.remove(delete); hAge.remove(delete); hRace.remove(delete); hAttack.remove(delete); hDefense.remove(delete); System.out.println("Hero has been deleted!"); StartPanel(); } else { StartPanel(); } } } //This is a useful method which allows the user to view all current Hero characters in the ArrayList. public void ShowTeam() { System.out.println("\n"); for (int i = 0; i < hName.size(); i++) { int counter = i + 1; System.out.println("H E R O " + counter); System.out.println("-------------------------"); System.out.println("Name: " + hName.get(i) + "\n" + " Age: " + hAge.get(i) + "\n" + " Race: " + hRace.get(i) + "\n" + " Attack: " + hAttack.get(i) + "\n" + " Defense: " + hDefense.get(i) + "\n" + "\n"); } } //This stops the program from running. static void StopPro() { System.out.println("Ended"); System.exit(0); } } //This is the main body which envokes the program class Hero { public static void main(String[] args) { HeroCreate heroChar = new HeroCreate(); heroChar.StartPanel(); } }