User input for creating objects

I am trying to create code to create a specific object if it matches the values ​​that the user enters.

For instance:

Suppose I have a Person class and a 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 prompted to enter 4 different values. The first three fields Person and Car are the same, but if the fourth value is a String or double, the program must create the corresponding 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 can i do this?

+4
source share
6 answers

You can use matchesto check if your input is double or 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);
}
+3
source

object instanceof

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) {

}
+1

, , , .

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

instanceof.

        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
        }
0

String, userinput, double. , , , , , try. ( ), catch.

:

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...

}
0

- try-catch, :

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 *******

}
0
source

Source: https://habr.com/ru/post/1677634/


All Articles