0

so I've been having an issue with equalsIgnoreCase() in java where it is given two strings that should be entirely identical, but not returning my expected result.

I'll just start with my code here:

   public void readBunnyFile(String fileIn) throws FileNotFoundException { 
      Scanner fileScanner = new Scanner(new File(fileIn));
      listName = fileScanner.nextLine().trim();
      while (fileScanner.hasNext()) {
         String type = fileScanner.next().trim() + " " + fileScanner.next().trim();
         if (type.equalsIgnoreCase("pet bunny")) {
            Scanner lineScanner = new Scanner(fileScanner.nextLine());
            lineScanner.useDelimiter(",");
            String name = lineScanner.next().trim();
            String breed = lineScanner.next().trim();
            double weight = Double.parseDouble(lineScanner.next());
            PetBunny pB = new PetBunny(name, breed, weight);
            addBunny(pB);
         }
         if (type.equalsIgnoreCase("house bunny")) {
            Scanner lineScanner = new Scanner(fileScanner.nextLine());
            lineScanner.useDelimiter(",");
            String name = lineScanner.next().trim();
            String breed = lineScanner.next().trim();
            double weight = Double.parseDouble(lineScanner.next());
            double wearAndTear = Double.parseDouble(lineScanner.next());
            HouseBunny hB = new HouseBunny(name, breed, weight, wearAndTear); 
            addBunny(hB);
         }
         if (type.equalsIgnoreCase("jumping bunny")) {
            Scanner lineScanner = new Scanner(fileScanner.nextLine());
            lineScanner.useDelimiter(",");
            String name = lineScanner.next().trim();
            String breed = lineScanner.next().trim();
            double weight = Double.parseDouble(lineScanner.next());
            double trainingCost = Double.parseDouble(lineScanner.next());
            JumpingBunny jB = new JumpingBunny(name, breed, weight, trainingCost); 
            addBunny(jB);
         }
         if (type.equalsIgnoreCase("show bunny")) {
            Scanner lineScanner = new Scanner(fileScanner.nextLine());
            lineScanner.useDelimiter(",");
            String name = lineScanner.next().trim();
            String breed = lineScanner.next().trim();
            double weight = Double.parseDouble(lineScanner.next());
            double groomingCost = Double.parseDouble(lineScanner.next());
            ShowBunny sB = new ShowBunny(name, breed, weight, groomingCost);
            addBunny(sB);
         }
         else {
            Scanner lineScanner = new Scanner(fileScanner.nextLine());
            lineScanner.useDelimiter(",");
            addExcludedRecord(type + lineScanner.nextLine());
         }       
      }

The file that this is reading looks like the following:

Bunny Collection
Pet bunny, Floppy, Holland Lop, 3.5
house Bunny, Spot, Really Mixed, 5.8, 0.15
mouse Bunny, Spots, Mixed, 0.8, 0.15
Jumping Bunny, Speedy, English, 6.3, 25.0 
fighting bunny, Slugger, Big Mixed, 16.5, 21.0
Show bunny, Bigun, Flemish Giant, 14.6, 22.0

I've watched my debugger go down countless times with a type value of "Pet bunny" yet it never steps into the if statement, and only goes to the else statement. Is there something I'm logically doing wrong here? "Pet bunny" should be equal to "pet bunny" since I am using .equalsIgnoreCase, right?

Carcanken
  • 49
  • 6
  • there might be a hidden char in your input. why do you need two scanner statements for one input? try with changing this: String type = fileScanner.next().trim() + " " + fileScanner.next().trim(); to String type = "pet bunny"; and see if it works then. Further recommendations: avoid duplicate code and use a switch statement instead of a number of if-statements (especially if you don't even nest the if's) – Stultuske Apr 11 '18 at 06:06
  • Your last `else` is only tied to the immediately preceding `if` - and you call `nextLine()` mixing your `next()` and `nextLine()` calls as in the duplicate. – Elliott Frisch Apr 11 '18 at 06:09

1 Answers1

2

The problem here is that your second call to Scanner#next() is picking everything up to, and including, the comma after bunny. So I am suggesting that you are comparing pet bunny against pet bunny,.

Given that you already have CSV data, a much safer (and easier) approach would be to just use Scanner#nextLine() and read in the entire line. Then, split by comma:

String line = fileScanner.nextLine();
String[] parts = line.split(",\\s+");
if (parts[0].equalsIgnoreCase("pet bunny")) {
    String name = parts[1];
    String breed = parts[2];
    double weight = Double.parseDouble(parts[3]);
    PetBunny pB = new PetBunny(name, breed, weight);
    addBunny(pB);
}
// and other cases
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263