读取文件时忽略大小写的问题

时间:2018-04-11 06:00:51

标签: java

所以我在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,对吗?

1 个答案:

答案 0 :(得分:2)

这里的问题是,您对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