【问题标题】:User input for creating objects用于创建对象的用户输入
【发布时间】:2017-10-21 00:54:10
【问题描述】:

如果它与用户输入的值匹配,我正在尝试编写代码来创建特定对象。

例如:

假设我有 Person 类和 Car 类

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
}

要求用户输入 4 个不同的值。 Person 和 Car 的前三个字段相同,但如果第 4 个值是 String 或 double,则程序应创建匹配对象。

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

我该怎么办?

【问题讨论】:

标签: java string validation input


【解决方案1】:

您可以使用matches 来检查您的输入是双精度还是字符串,例如:

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);
}

【讨论】:

    【解决方案2】:

    您可以使用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) {
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以将 other 声明为全局字符串,然后检查它是否为双精度,您可以将其保存在双精度变量中。

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

      【讨论】:

        【解决方案4】:

        使用 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
                }
        

        【讨论】:

          【解决方案5】:

          首先创建一个简单的String 来捕获用户输入,然后尝试将其解析为double。如果它可以解析它,则意味着输入了一个有效的 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...
          
          }
          

          【讨论】:

            【解决方案6】:

            另一种验证输入的方法是使用简单的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 *******
            
            }
            

            【讨论】:

              猜你喜欢
              • 2021-12-21
              • 1970-01-01
              • 2018-03-12
              • 2015-12-21
              • 2019-10-20
              • 2015-11-11
              • 2021-11-18
              • 2017-09-11
              相关资源
              最近更新 更多