如何解决java中的java.util.MismatchException?

如何解决java中的java.util.MismatchException?,java,Java,我的讲师给了我们一个简单的作业,从文本文件(作为数据库)中读取数据,并根据年龄和性别计算津贴。当我从txt文件中读取一行时,我读取每个单词并存储在字符串中。但这对我来说有点困惑,如果我的文件中有一行内容说- bucky roberts M 23 现在我有了firstname=buckylastname=robertssex=M和age=23 现在,基于以上字段,如果我进行计算,它工作得很好 但是如果我的讲师说把同一行改写成- bucky M 23firstname=buckylastname=

我的讲师给了我们一个简单的作业,从文本文件(作为数据库)中读取数据,并根据年龄和性别计算津贴。当我从txt文件中读取一行时,我读取每个单词并存储在字符串中。但这对我来说有点困惑,如果我的文件中有一行内容说-

bucky roberts M 23
现在我有了
firstname=bucky
lastname=roberts
sex=M
age=23

现在,基于以上字段,如果我进行计算,它工作得很好

但是如果我的讲师说把同一行改写成-
bucky M 23
firstname=bucky
lastname=M
sex=23
age=null

这就是程序从文件中读取行的方式。现在,如果我根据年龄生成if-else语句,我将得到一个错误
java.util.MismatchException
。我如何解决这个问题,同时找到一种方法,即使我的讲师对数据库有意见,程序也能正常运行。以下是代码的一部分:-

AdultMale.java类:-

public class AdultMale extends Male {

    public void getAllowance() {
        readfromFile();
        while(in.hasNext())
        {
            firstname = in.next();
            lastname = in.next();
            gender = in.next();
            age = in.nextInt();

            if(gender.equals("M") || gender.equals("Male") || gender.equals("male") ||gender.equals("m"))
            {
                AdultAllowance();

            }

         }
        in.close();

    }       
}
Male.java类:-

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

 public class Male extends Members{


        Scanner in;
        public void getAllowance() {

        }

        public void readfromFile()
        {
            try {
                in = new Scanner(new File("Dbase.txt"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        public void AdultAllowance()
        {
            if(age>=18)
            {
                total = 500;
                total = total + 1000;
                System.out.println(firstname + "\t" +lastname+"\t"+ age + "\t"+ gender+"\t"+total);
                total = 0;
            }

        }

        public void MinorAllowance()
        {
            if(age <18)
            {
                total = 500;
                total = total + 200;
                System.out.println(firstname + "\t"+lastname+"\t" + age + "\t"+ gender+"\t"+total);
                total = 0;
            }
        }
    }
我的txt数据库名为“Dbase.txt”,其中包含以下内容:-

Sam     Bell        M   17
Becky   roberts     F   23
Bucky   taylor      M   21
Sammy   lyson       F   16
Terry   gibson      F   23

阅读整行,然后使用它。所以,类似的东西应该可以工作(未测试的代码,用textpad编写)。这将处理所有情况

while(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] arr = line.split("\\s+");
 for(String str : arr) {
  if(str.equalsIgnoreCase("M") || str.equalsIgnoreCase("Male")); // check for gender for each line in file.
    gender = "M";
    }
  else if(str.matches("\\d+")) { // check for age.
    age = Integer.parseInt(str);
    }
  else if(firstName!=null || !firstName.isEmpty()) // for string that is not age or gender.
   {
    firstName = str;
   }
  else{
      lastname = str;
     }
}
}

你能告诉我们这个文件是什么样子吗?一个问题中有这么多的
东西
s:PIs输入是一个空格分隔还是一个制表符分隔?问题是
null
代替
age
@MissyZewdie-我只希望他现在不在这里:P。。无论如何。。你可以做两件事。。1.使用
Scanner
hasNext()
hasNextInt()
等方法检查您是否真的有号码和姓名、性别。。对于读取的每个字符串,检查它是否等于
M
F
,以确保它只是性别。。因此,行中剩下的是您的名字:)。这有意义吗?:-非常感谢。真的从心底里谢谢你。:-是的。尽管我仍将要实现整个代码。如果我被困在任何地方,我能给你回电话吗?@missyewdie:-)当然。。快乐编码:)
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] arr = line.split("\\s+");
 for(String str : arr) {
  if(str.equalsIgnoreCase("M") || str.equalsIgnoreCase("Male")); // check for gender for each line in file.
    gender = "M";
    }
  else if(str.matches("\\d+")) { // check for age.
    age = Integer.parseInt(str);
    }
  else if(firstName!=null || !firstName.isEmpty()) // for string that is not age or gender.
   {
    firstName = str;
   }
  else{
      lastname = str;
     }
}
}