简体   繁体   中英

User input for creating objects

I'm trying to code something to create a specific object if it matches the values that the user Inputs.

For example:

suppose I have Person class and Car class

public class Person
{
    private int x, y ,z;
    private String str;

    //Constructors etc
} 
public class Car
{
    private int x,y,z;
    private double doub;

    //Constructors etc
}

The user is asked to input 4 different values. The first three fields of Person and Car are the same but if the 4th value is a String or double, the program should create the matching object.

public class Input
{
     public static void main (String args[])
     {
         int x,y,z;
         ?? other; //how do i declare if its either a double or string

         Scanner input = new SCanner(System.in);
         x = input.nextInt();
         // y, z input
         other = input.??? //String or double

         //Create matching object
     }
}

How do i go about this?

You can use matches to check if your input is double or a String for example :

Scanner input = new Scanner(System.in);
String other = input.nextLine();
if (other.matches("\\d+\\.\\d+")) {
    //your input is double
    Double d = Double.parseDouble(other);
    System.out.println("double = " + d);

} else {
    //your intput is a Strings
    System.out.println(other);
}

You can use object for type and instanceof for checking

Scanner input = new SCanner(System.in);
x = input.nextInt();
// y, z input
string other = input.next();
Double d = null;
try {
  d = Double.parseDouble(other);
} catch (NumberFormatException e) {

}

you can declare other as string as a global and then check it if it double, you can save it in a double variable.

     Scanner input = new SCanner(System.in);
     x = input.nextInt();
     // y, z input
     string other = input.next()

Try this using instanceof keyword.

        System.out.println("Enter value");
        String userIn = new Scanner(System.in).nextLine();

        try {
            if ((Double) Double.parseDouble(userIn) instanceof Double) {
                System.out.println("Double value is entered.");
                //Write your code here if it is double
            } else if (userIn instanceof String) {
                System.out.println("String is entered");
                //Write your code here if it is String
            }
        } catch (NumberFormatException ex) {
            System.out.println("String is entered");
            //Write your code here if it is String
        }

First create a simple String to capture userinput, after that try to parse it into a double . If it can parse it it means that a valid double was entered and no exception will be thrown, in other words, it will execute the code in the try block. If it can't parse (throws an Exception) it will execute the code in the catch block.

Example:

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);
    System.out.println("Enter something...");
    String userInput = kbd.next();
    Car car = null; // create empty Car object to be able to use it outside of the try/catch block
    Person person = null; // create empty Person object to be able to use it outside of the try/catch block

    try {
        double d = Double.parseDouble(userInput);
        car = new Car();
    } catch (NumberFormatException e) {
        person = new Person();
    }

    // Ask user for new input and assign it to either Car or Person object as you develop your program...

}

Another way to validate your input is to use a simple try-catch , for example:

Scanner input = new Scanner(System.in);
String other = input.nextLine();
Car car = null;
Person person = null;

try {
    // Is your input double
    Double d = Double.parseDouble(other);
    System.out.println("double = " + d);

    car = new Car();
    // ******* Write your statements to handle Car here *******

} catch (NumberFormatExaception nfe) {
    //your intput is a String
    System.out.println(other);

    person = new Person();
    // ******* Write your statements to handle Person here *******

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM