使用try catch语句检查一个对象等于另一个对象

时间:2018-11-29 05:06:10

标签: java try-catch

我正在编写程序;有卡车级,车辆级,人员级和驾驶员级。

卡车和车辆用于向车辆设置信息,人员用于设置人员信息,驾驶员全部运行。

我当前的问题是我正在尝试使用try catch语句来检查用户输入的数据与上一次输入的数据相同,但似乎无法正确输入。

我将在下面附加我的所有课程,虽然很冗长,但希望会有所帮助。

//driver class

import java.util.Scanner;
import java.util.InputMismatchException;

public class Driver{

    public static void main(String args[]){
        Scanner keyboard= new Scanner(System.in);
        //creates vehicles
        Vehicle car1= new Vehicle();
        Vehicle car2= new Vehicle();
        Vehicle car3= new Vehicle();

        //creates trucks
        Truck truck1= new Truck();
        Truck truck2= new Truck();
        Truck truck3= new Truck();

        boolean check1=true;
        boolean check2=true;
        boolean check3=true;
        boolean nameCheck=false;
        boolean truckCheck1=false;
        boolean truckCheck2=false;
        boolean truckCheck3=false;

        while(!nameCheck) {

            System.out.println("Enter first persons name");

            Person owner1 = new Person(keyboard.next()); // creates new person
            car1.setOwner(owner1); //makes the first person the owner of the first car
            if (car1.equals(car1)) //checking if name is null
                nameCheck=true;

        }

        System.out.println("You entered: " + car1.getOwner()); //echoes user input
        System.out.println("Enter manufacturer of car 1");
        car1.setManufacturer(keyboard.next()); //set manufacturer of car 1
        System.out.println("You entered: " + car1.getManufacturer() + " as car 1 manufacturer");

        while( check1 && nameCheck){
            try //data check
            {
                System.out.println("Enter number of cylinders for car 1");
                car1.setCylinders(keyboard.nextInt());

                if (car1.cylinders<2 || car1.cylinders>30)
                    throw new Exception("You entered an invalid amount of cylidners!");

                else
                    check1=false;
                System.out.println(car1.toString());
            }

            catch (InputMismatchException e){
                keyboard.next();
                System.out.println("Cylinders must be entered in integer form");
                System.out.println("Try again!");
            }

            catch (Exception e){
                String message = e.getMessage();
                System.out.println(message);
            }
        }


        while (!check1){
            System.out.println("Please set name, car manufacturer, and cylinders equal to car 1");

        try{
            System.out.println("Enter second persons name");
            Person owner2 = new Person (keyboard.next());
            car2.setOwner(owner2);

            if (!car2.getOwner().equals(car1.getOwner()))
                throw new Exception("Invalid name entered, please enter same name as owner 1");

                else
                check1=true;
            }

        catch(Exception e){
            keyboard.next();
            System.out.println("Please enter same owner as car 1");
            System.out.println("Try again!");
        }

            System.out.println("You entered: " + car2.getOwner());
            check1=true;

            System.out.println("Enter manufacturer of car 2");
            car2.setManufacturer(keyboard.next());
            System.out.println("You entered: " + car2.getManufacturer());

            while (nameCheck) {
                try {
                    System.out.println("Enter number of cylinders for the second vehicle");
                    car2.setCylinders(keyboard.nextInt());
                    if (car2.cylinders< 2 || car2.cylinders>30)
                        throw new Exception("You entered an invalid amount of cylinders!");

                        else
                            check2= false;

                            System.out.println(car2.toString());
                        }
                        catch(InputMismatchException e) {
                            keyboard.next();
                            System.out.println("Cylinders must be entered a as an integer between 2 and 30");
                            System.out.println("Try again!");
                        } catch (Exception e){

                            String message= e.getMessage();
                            System.out.println(message);
                        }
                    }
                }

                while (!check2) {
                    System.out.println("Enter third persons name");
                    Person owner3= new Person(keyboard.next());
                    car3.setOwner(owner3);
                    System.out.println("You entered: " + owner3);

                    System.out.println("Enter manufacturer of car 3");
                    car3.setManufacturer(keyboard.next());
                    System.out.println("You entered: " + car3.getManufacturer());
                    while (check3) {
                        try {
                            System.out.println("Enter number of cylinders for car 3");
                            car3.setCylinders(keyboard.nextInt());

                            if (car3.cylinders<2 || car3.cylinders>30)

                                throw new Exception("Invalid amount of cylinder entered!");

                                else
                                    check2=true;
                                    check3=false;
                                    check1=false;
                                    System.out.println(car3.toString());
                                } catch (InputMismatchException e) {

                                    keyboard.next();
                                    System.out.println("Cylinders must be entered a as an integer between 2 and 30");
                                    System.out.println("Try again!");
                                } catch (Exception e) {

                                    String message= e.getMessage();
                                    System.out.println(message);
                                }
                            }
                        }

                        truck1.setOwner(car1.getOwner()); //sets owner of car1 to truck1
                        System.out.println("Enter manufacturer of truck 1, Owner will be the same as car 1");
                        truck1.setManufacturer(keyboard.next());

                        System.out.println("You entered: " + truck1.getManufacturer());
                        while (!truckCheck1)
                            try {
                                System.out.println("Enter cyliners for truck 1");

                                truck1.setCylinders(keyboard.nextInt());
                                if (truck1.cylinders<2 || truck1.cylinders>30)

                                    throw new Exception("Invalid amount of cylinders entered!");

                                else
                                    truckCheck1= true;
                                } catch (InputMismatchException e) {

                                    keyboard.nextInt();
                                    System.out.println("Cylinders must be entered a as an integer between 2 and 30");
                                    System.out.println("Try again!");
                                } catch (Exception e) {

                                    String message= e.getMessage();
                                    System.out.println(message);
                                }
                            System.out.println("You entered: " + truck1.getCylinders());
                            while (truckCheck1)
                                try {
                                    System.out.println("Enter load capacity in tons for truck 1. Must be greater than zero");
                                    truck1.setLoadCapacity(keyboard.nextDouble());
                                    if (truck1.getLoadCapacity()<0)

                                        throw new Exception("You entered an invalid load capacity");

                                    else
                                        truckCheck1= false;
                                    } catch(InputMismatchException e) {

                                        keyboard.next();
                                        System.out.println("Load capacity must be in integer format");
                                        System.out.println("Try again!");
                                    } catch (Exception e) {

                                        String message= e.getMessage();
                                        System.out.println(message);
                                    }

                                    System.out.println("You entered: " + truck1.getLoadCapacity());

                                    while (!truckCheck1)
                                        try {

                                            System.out.println("Enter towing capacity in pounds of truck 1");

                                            truck1.setTowCapacity(keyboard.nextInt());
                                            if (truck1.getTowCapacity()<0)

                                                throw new Exception("You entered an invalid tow capacity, tow capacity must be entered as an integer");

                                            else
                                                truckCheck1= true;
                                            }catch (InputMismatchException e){

                                                keyboard.next();
                                                System.out.println("Towing Capacity must be in integer form");
                                                System.out.println("Try again!");

                                            } catch (Exception e){

                                                String message= e.getMessage();
                                                System.out.println(message);
                                            }
                                            System.out.println(truck1);

                                            truck2.setOwner(car2.getOwner()); //sets truck 2 owner the same as car 2 owner
                                            System.out.println("Enter manufacturer of truck 2");
                                            truck2.setManufacturer(keyboard.next());
                                            System.out.println("You entered: " + truck2.getManufacturer());

                                            while(!truckCheck2)
                                                try {
                                                    System.out.println("Enter number of cylinders for truck 2, must be a integer between 2 and 30");

                                                    truck2.setCylinders(keyboard.nextInt());
                                                    if (truck2.cylinders<2 || truck2.cylinders>30)

                                                        throw new Exception("You entered an invalid amount of cylinders");

                                                    else
                                                        truckCheck2= true;
                                                    } catch (InputMismatchException e){

                                                    keyboard.next();
                                                    System.out.println("Load capacity must be entered as an integer");
                                                    System.out.println("Try again!");
                                            } catch (Exception e){

                                                    String message= e.getMessage();
                                                    System.out.println(message);
                                                }

                                            System.out.println("You entered: " + truck2.getCylinders());

                                            while (truckCheck2)
                                                try{

                                                    System.out.println("Enter towing capacity in pounds of truck 2");
                                                    truck2.setLoadCapacity(keyboard.nextDouble());
                                                    if (truck2.getLoadCapacity()<0)

                                                        throw new Exception("Invalid Tow Capacity Entered");

                                                    else
                                                        truckCheck2=false;
                                                    }catch (InputMismatchException e){

                                                        keyboard.next();
                                                        System.out.println("Load Capacity must be entered as an integer");
                                                        System.out.println("Try again!");
                                                    } catch (Exception e) {

                                                        String message = e.getMessage();
                                                        System.out.println(message);
                                                    }
                                                System.out.println("You entered: " + truck1.getLoadCapacity());

                                                while (!truckCheck2)
                                                    try {

                                                        System.out.println("Enter Towing capacity in pounds of truck 1");

                                                        truck2.setTowCapacity(keyboard.nextInt());
                                                        if (truck2.getTowCapacity()<0)

                                                            throw new Exception("Invalid Tow Capacity Entered");

                                                        else
                                                            truckCheck2= true;
                                                        } catch (InputMismatchException e){

                                                            keyboard.next();
                                                            System.out.println("Towing Capacity must be entered as an integer");
                                                            System.out.println("Try again!");
                                                        } catch (Exception e){

                                                            String message= e.getMessage();
                                                            System.out.println(message);
                                                        }
                                                    System.out.println(truck2);

                                //collectin third truck info
                                truck3.setOwner(car3.getOwner());

                                System.out.println("Enter manufacturer of truck 3, Owner will be the same as car 3");
                                truck3.setManufacturer(keyboard.next());
                                System.out.println("You entered: " + truck3.getManufacturer());

                                while(!truckCheck3)
                                    try{
                                        System.out.println("Enter Cylinders of truck 3, must be a integet between 2 and 30");

                                        truck3.setCylinders(keyboard.nextInt());
                                        if(truck3.cylinders<2 || truck3.cylinders>30)

                                            throw new Exception("Invalid amount of cylinders entered");

                                        else
                                            truckCheck3= true;
                                    } catch (InputMismatchException e) {

                                        keyboard.next();
                                        System.out.println("Cylinders must be an integer");
                                        System.out.println("Try again!");
                                    } catch (Exception e){

                                        String message= e.getMessage();
                                        System.out.println(message);
                                    }

                                System.out.println("You entered: " + truck3.getCylinders());

                                while (truckCheck3)
                                    try {
                                        System.out.println("Enter load capacity in tons of truck 3, must be greater than 0");
                                        truck3.setLoadCapacity(keyboard.nextDouble());
                                        if (truck3.getLoadCapacity()<0)

                                            throw new Exception("Invalid amount entered");

                                        else
                                            truckCheck3= false;
                                        } catch (InputMismatchException e){

                                            keyboard.next();
                                            System.out.println("Load capacity must be in integer form");
                                            System.out.println("Try again!");
                                        } catch (Exception e) {

                                            String message= e.getMessage();
                                            System.out.println(message);
                                        }
                                    System.out.println("You entered: " + truck3.getLoadCapacity());

                                    while(!truckCheck3)
                                        try {

                                            System.out.println("Enter towing capacity in pounds of truck 3");

                                            truck3.setTowCapacity(keyboard.nextInt());
                                            if (truck3.getTowCapacity()<0)

                                                throw new Exception("Invalud amount entered");

                                            else
                                                truckCheck3= true;
                                        } catch (InputMismatchException e) {

                                            keyboard.next();
                                            System.out.println("Towing capacity must be in integer form");
                                            System.out.println("Try again!");
                                        } catch (Exception e) {

                                            String message= e.getMessage();
                                            System.out.println(message);
                                        }

                                    System.out.println();
                                    System.out.println("Car Details:" + "\n" + "Car 1:" + car1 + "\n" + "Car 2:" + "\n" + car2 + "\n" + "Car 3:" + "\n" + car3);
                                    System.out.println();
                                    System.out.println("Truck Details:" + "\n" + "Truck 1:" + truck1 + "\n" + "Truck 2:" + "\n" + truck2 + "\n" + "Truck 3:" + "\n" + truck3);
                        }
                    }

    /** vehicle class
    Carmine  */

    public class Vehicle{

    public String manufacturer;
    public int cylinders;
    public Person owner; //owner pulled from person class

    public Vehicle(){
        manufacturer="none";
        cylinders=1;
        owner=null;
    }

    public Vehicle(String theManufacturer, int numOfCylinders, Person theOwner)
    {
        theManufacturer=manufacturer;
        numOfCylinders=cylinders;
        owner=new Person(theOwner);
    }

    //sets manufacturer
    public void setManufacturer(String newManufacturer){
        manufacturer=newManufacturer;
    }

    //return manufacturer name
    public String getManufacturer(){
        return manufacturer;
    }

    //sets cylinders
    public void setCylinders(int numOfCylinders){
        cylinders=numOfCylinders;
    }

    //return cylinders
    public int getCylinders(){
        return cylinders;
    }

    //sets owner
    public void setOwner(Person newOwner)
    {
    owner = new Person(newOwner);
    }

    //returns owner
    public Person getOwner(){
        return owner;
    }

    @Override
        public String toString(){
        return ("Manufaturer: " + manufacturer + "\n" + "Cylinders: " + cylinders + "\n" + "Owner: " + owner);
    }
    }

    //Person Class
    public class Person {
    private String name;

    public Person()

    {
        name = "none";
    }

    public Person(String theName) {
        name = theName;
    }

    public Person(Person theObject) {
        name = theObject.name;
    }

    public String getName() {
        return name;
    }

    public void setName(String theName) {
        name = theName;
    }

    public String toString() {
        return name;
    }

    public boolean equals(Object other) {
        return name.equals(((Person) other).name);

    }
    }

    public class Truck extends Vehicle //entends truck from vehicle
    {
    double loadCapacity;
    int towCapacity;

    public Truck(){
        super();
        loadCapacity=0;
        towCapacity=0;
    }

    public Truck(String m, int c, Person p, double load, int capacity){
        super(m,c,p);
        loadCapacity=load;
        towCapacity=capacity;
    }

    //sets load
    public void setLoadCapacity(double newLoad){
        loadCapacity=newLoad;
    }

    //returns load
    public double getLoadCapacity(){
        return loadCapacity;
    }

    //sets towCapacity
    public void setTowCapacity(int newCapacity){
        towCapacity=newCapacity;
    }

    //return towCapacity
    public int getTowCapacity(){
        return towCapacity;
    }

    public String toString()
    {
        return super.toString() + ("Load Capacity: " + loadCapacity + "\n" + "Tow Capacity: " + towCapacity);
    }
}

我知道很多代码,但是这个try catch语句确实使我感到沮丧,我只想完成此操作。

在这种情况下,我还需要使用.equals来检查是否相等。两个人必须和两个卡车和小汽车一样。

1 个答案:

答案 0 :(得分:1)

当您的检查失败时,您的代码将抛出Exception,而不是InputMismatchException。因此,不会打印“重试”的输出。

我对使用异常一开始有些困惑。当您知道存在问题时,为什么不直接在if块中处理问题,因为所有问题都包含在while循环中,直到输入正确为止。

异常通常在非常特殊的情况下使用,例如系统错误或意外事件。在大多数情况下,如果您可以在从用户那里获得输入后立即检查输入,则代码可以像在循环中那样立即处理输入是否有效。