Java Im having issues getting my readFile method to read a C

(Java) I’m having issues getting my readFile method to read a CSV file correctly. PetBoardingList is the class that houses the readFile method and generates reports. The requirements are as follows:

These are my class variables:

Here’s what I have written:

Here’s the file being read in:

Here’s the parent abstract class (Pet) constructor and an example of one of the child classes (Cat):

I tested the method and it was only reading in the first pet object (and ignoring everything else including the list name). Please advise on any glaring errors.

Solution

Ok, let clean the code first.

in the readPetFile(String filename) method, do the following changes:

a .) Remove if(lineNum > 1) {} condition. The reason to remove it, is \"scanFile.hasNextLine\" will make sure the while loop executes until there are lines in the csv file. So we do not have to keep a check for lineNum or any other such counters.

b .) Lets remove the check for menuInputChar == \'C\' || menuInputChar == \'D\' || and so on..... . The reason to remove that is the switch case handles that for you, if menuInputChar doesnot match any input it would go for default case where you can mention the invalid category.

c.) Lets modularize the code. Instead of writing lengthy switch case statements, we can break them in methods having fewer lines.

So, I am adding my code below, let me know if you find any issues:

// Updated Version :

So, I am going with your approach for fetching each value using next() method. So following are the changes:

a.) Now Pet and Cat constructor takes all arguments as string.

     

     

// The reason to do that is to make the code cleaner as you can see below:

b.)   // here is the readPetFile method

      

// For instantiating cat object, you can directly read values with scanLine and pass it to constructor

// Or you can store them in variable and then instantiate.

// Now coming to getServiceDog method,

// Explaining with an example , it will read the input ( i.e \"S,Jen Baker,Pepper,Sheppard,60.0,7,guide dog,sit,down,stay,come,around,forward,right,left \" ) as follows:

// Note: Extra point to bring to your notice

Lets say, service dog has following commands:

In such case we might get null value in the array, so before using commands array value, we can put a check as

       if(commands[i] != null)

      // proceed further

(Java) I’m having issues getting my readFile method to read a CSV file correctly. PetBoardingList is the class that houses the readFile method and generates rep
(Java) I’m having issues getting my readFile method to read a CSV file correctly. PetBoardingList is the class that houses the readFile method and generates rep

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site