java—在具有多种输入类型的scanner中正确处理异常

0sgqnhkj  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(823)

如何正确处理扫描器中的异常?我有不同类型的输入,不知道如何避免,例如输入不匹配异常,而不使用try-catch块和不结束程序。有很多输入的方法。如何解决此问题并获得干净的代码?我还在学习,我是初学者。有没有办法缩短我的代码?这种方法似乎过于广泛。我没有重构的经验。例如,我的addperson方法:

private static void addPerson(Scanner scanner) {
        System.out.println("name:");
        String name = scanner.next();

        System.out.println("surname:");
        String surname = scanner.next();

        System.out.println("age:");
        int age = scanner.nextInt();

        System.out.println("height (in CM):");
        int height = scanner.nextInt();

        System.out.println("weight:");
        double weight = scanner.nextDouble();

        System.out.println("ADDRESS - city:");
        String city = scanner.next();

        System.out.println("ADDRESS - zipCode:");
        String zipCode = scanner.next();

        System.out.println("ADDRESS - street:");
        String street = scanner.next();

        System.out.println("ADDRESS - home number:");
        int homeNumber = scanner.nextInt();

        Person person = new Person();
        person.setName(name);
        person.setSurname(surname);
        person.setAge(age);
        person.setWeight(weight);
        person.setHeight(height);
        Address address = new Address(city, zipCode, street, homeNumber);

        person.setAddress(address);
        personService.add(person);
        System.out.println("Person added to the base with id: " + person.getId());

    }
mzmfm0qo

mzmfm0qo1#

可以将throws关键字添加到方法中

public static void addPerson(Scanner scanner) thorws InputMismatchException {
// your code
}

或者您可以添加try-catch块,这不会结束您的程序。

private static void addPerson(Scanner scanner) {
   try{
        System.out.println("name:");
        String name = scanner.next();

        System.out.println("surname:");
        String surname = scanner.next();

        System.out.println("age:");
        int age = scanner.nextInt();

        System.out.println("height (in CM):");
        int height = scanner.nextInt();

        System.out.println("weight:");
        double weight = scanner.nextDouble();

        System.out.println("ADDRESS - city:");
        String city = scanner.next();

        System.out.println("ADDRESS - zipCode:");
        String zipCode = scanner.next();

        System.out.println("ADDRESS - street:");
        String street = scanner.next();

        System.out.println("ADDRESS - home number:");
        int homeNumber = scanner.nextInt();
   }
   catch(InputMismatchException inputMismatchException){
      // you can debug, see what caused issue by adding the code below.
      inputMismatchException.printStackTrace();
      // or you can add your code to make it user friendly
      System.out.println("Uh oh! Your input is not the correct type!");
   }

如果你想多做一点改进,你可以为每个输入创建一个while循环,并放置try-catch块,这样如果用户输入了错误的内容而你得到了一个异常,你就可以让用户再试一次。

boolean userTypedCorrect = false;
while(userTypedCorrect == false){
   try{
      // your code
      // user typed it correct and didn't get an exception
      // change boolean so while loop exits
      userTpedCorrect = true;
   }
   catch(InputMismatchExecption iOrAnyNameIsFine){
      System.out.println("Try again!");
      // user didn't type it correctly
      // boolean doesn't change so it loops
   }
}

如果有可能出现异常,即使代码中没有任何错误或bug,我也会选择try-catch(inputmistmatch是一个很好的例子),但是如果一些代码需要异常,并且您知道它在完成编码时不会抛出异常,那么您可以使用throws(例如,当你从一个.txt文件中读取时,它需要ioexecption,但是你总是有一个.txt文件,所以它永远不会抛出异常)
小贴士:不是通过方法来设定一切,

Person person = new Person();
person.setName(name);
person.setSurname(surname);
person.setAge(age);
person.setWeight(weight);
person.setHeight(height);

您可以在person.java文件或person类所在的位置创建自己的person构造函数(我假设person类中有变量)

public class Person{
   String name; // this.name
   String surname;
   int age;
   // more variables

   public Person(String name, String surname, int age){ // name passed in
      // this.name refers to variable we made at top
      // name refers to name we passed in
      this.name = name;      
      this.surname = surname;
      this.age = age;
   }
}

你就这样创造了一个新人

Person John = new Person("John", "Doe", 300);

this.variablename=variablename可能会让人困惑,但这是一种常见的做法,一旦您理解了就很容易了。

相关问题