2017-05-21 72 views
4

我想編寫一些東西來創建特定的對象,如果它匹配用戶輸入的值。用於創建對象的用戶輸入

例如:

假設我有Person類和轎車類

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的前三個字段是相同的,但如果第四個值是一個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 
    } 
} 

我該如何解決這個問題?

+0

檢查是否可以解析它作爲一個雙:HTTP:/ /stackoverflow.com/questions/3543729/how-to-check-that-a-string-is-parseable-to-a-double – alejandrogiron

回答

3

您可以使用matches檢查您輸入的是雙人或如String:

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

你可以聲明其他爲字符串作爲全局,然後檢查它是否加倍,你可以將它保存在一個雙變量中。

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

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

} 
0

首先創建一個簡單的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... 

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

}