I'm trying to write a program that allows you to check 2 different kind of animals into a shelter (dogs and cats). When you run the program, you're asked which animal you're entering into the system and, depending on the answer, the corresponding file (.txt) is selected to read and write from.
I'm having an issue with choosing the file when ran through the console, and error catching if neither "dog" or "cat" is entered.
Here's the current code:
private KennelDemo() {
scan = new Scanner(System.in);
System.out.print("Are you checking a dog or cat into the kennel? Enter 'dog' or 'cat': ");
// Try to allocate the correct file to the String fileChosen
/*
* TODO
* kennel should not run if "cat" or "dog" hasn't been entered. While loop?
*/
try {
fileChosen = scan.next();
if(fileChosen == "dog")
fileChosen = dogsFile;
else if
(fileChosen == "cat")
fileChosen = catsFile;
}
catch (Exception IOException) {
System.out.println("Please enter either 'dog' or 'cat', nothing else.");
}
kennel = new Kennel();
}
The entry process should be:
Enter dog or cat when prompted
If "cat is entered, the system reads and writes from cats.txt
If Dog is selected "dogs.txt" is read from and written to.
If neither is entered, the system catches the error and returns the user to the selection process.
What happens is:
When you enter "dog" or "cat", the catch error prints the error line regardless and then it tries to run Kennel();.
EDIT:
Here are the variables declared:
public class KennelDemo {
private Kennel kennel; // holds the kennel
private Scanner scan; // so we can read from keyboard
private String dogsFile = "./dogs.txt"; // hardcodes file for the information regarding dogs
private String catsFile = "./cats.txt"; // hardcodes file for the information regarding cats
private String fileChosen; // String to contain the filename entered by the user and then ran with through the method KennelDemo()
Any help with this at all?
Thanks.
Aucun commentaire:
Enregistrer un commentaire