This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
                                
                                    (19个回答)
                                
                        
                2年前关闭。
            
        

所以我一直在java中的equalsIgnoreCase()遇到问题,在其中给了两个应该完全相同的字符串,但没有返回我的预期结果。

我将从这里的代码开始:

   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());
         }
      }


正在读取的文件如下所示:

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


我已经看到调试器使用类型值“ Pet bunny”关闭了无数次,但它从未进入if语句,而仅进入else语句。我在逻辑上在做错什么吗?因为我正在使用.equalsIgnoreCase,所以“宠物兔子”应该等于“宠物兔子”,对吗?

最佳答案

这里的问题是您对Scanner#next()的第二次调用正在提取bunny之前的所有内容,包括逗号。因此,我建议您将pet bunnypet bunny,进行比较。

鉴于您已经拥有CSV数据,一种更安全(更简单)的方法是只使用Scanner#nextLine()并读取整行。然后,以逗号分隔:

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

09-30 17:14