2017-04-08 128 views
0

我想在java中編寫一個應用程序,該應用程序創建一個類的對象並接受用戶輸入來填充對象的屬性,並且如果用戶輸入無效值可以引發異常。我將類的異常條件放在名爲Stuff.java的類中,並創建了異常類。我的問題是在包含主要方法的類中,我如何對它進行編碼以提示用戶爲一個接一個的屬性輸入值,並且它必須不斷提示用戶輸入,直到用戶填入值三個屬性。這裏是我的代碼如下,具有主要方法的類並不真正給我想要的東西。我必須做些什麼改變?Java異常和掃描器輸入

東西類:

public class Stuff { 
private int howMany; 
private String name; 
private boolean answer; 

public void setHowMany(int howMany) throws InappropriateIntegerValue { 
    if (howMany <= 0 || howMany >= 10000) { 
     throw new InappropriateIntegerValue("in appropriate value"); 
    } else { 
     this.howMany = howMany; 
    } 
} 

public void setName(String name) throws IllegalStringValue { 
    CharSequence car = "[email protected]#%&*"; 
    if (name.length() < 6 || name.contains(car) != true 
      || Character.isAlphabetic(name.charAt(0)) == false) { 
     throw new IllegalStringValue("illegal string value"); 
    } else { 
     this.name = name; 
    } 

} 

public void setAnswer(String answer) throws InvalidBooleanValue { 
    if (answer == "yes") { 
     this.answer = true; 
    } else if (answer == "no") { 
     this.answer = false; 
    } else { 
     throw new InvalidBooleanValue("invalid respose"); 
    } 
} 

@Override 
public String toString() { 
    return "Stuff [howMany=" + howMany + ", name=" + name + ", answer=" 
      + answer + "]"; 
} 

}

爲非法字符串值的異常類:

public class IllegalStringValue extends RuntimeException { 

public IllegalStringValue() { 
    // TODO Auto-generated constructor stub 
} 

public IllegalStringValue(String message) { 
    super(message); 
    // TODO Auto-generated constructor stub 
} 

public IllegalStringValue(Throwable cause) { 
    super(cause); 
    // TODO Auto-generated constructor stub 
} 

public IllegalStringValue(String message, Throwable cause) { 
    super(message, cause); 
    // TODO Auto-generated constructor stub 
} 

public IllegalStringValue(String message, Throwable cause, 
     boolean enableSuppression, boolean writableStackTrace) { 
    super(message, cause, enableSuppression, writableStackTrace); 
    // TODO Auto-generated constructor stub 
} 

}

爲非法整數值

異常類
public class InappropriateIntegerValue extends RuntimeException { 

public InappropriateIntegerValue() { 
    // TODO Auto-generated constructor stub 
} 

public InappropriateIntegerValue(String message) { 
    super(message); 
    // TODO Auto-generated constructor stub 
} 

public InappropriateIntegerValue(Throwable cause) { 
    super(cause); 
    // TODO Auto-generated constructor stub 
} 

public InappropriateIntegerValue(String message, Throwable cause) { 
    super(message, cause); 
    // TODO Auto-generated constructor stub 
} 

public InappropriateIntegerValue(String message, Throwable cause, 
     boolean enableSuppression, boolean writableStackTrace) { 
    super(message, cause, enableSuppression, writableStackTrace); 
    // TODO Auto-generated constructor stub 
} 

}

用於非法布爾值異常類:

public class InvalidBooleanValue extends RuntimeException { 

public InvalidBooleanValue() { 
    // TODO Auto-generated constructor stub 
} 

public InvalidBooleanValue(String message) { 
    super(message); 
    // TODO Auto-generated constructor stub 
} 

public InvalidBooleanValue(Throwable cause) { 
    super(cause); 
    // TODO Auto-generated constructor stub 
} 

public InvalidBooleanValue(String message, Throwable cause) { 
    super(message, cause); 
    // TODO Auto-generated constructor stub 
} 

public InvalidBooleanValue(String message, Throwable cause, 
     boolean enableSuppression, boolean writableStackTrace) { 
    super(message, cause, enableSuppression, writableStackTrace); 
    // TODO Auto-generated constructor stub 
} 

}

與主方法的類,這是我覺得我得到一些錯誤:

import java.util.Scanner; 

public class Application { 

public static void main(String[] args) { 
    Stuff stf = new Stuff(); 
    Scanner userIn = new Scanner(System.in); 
    int howMany; 
    String name; 
    String answer; 

    System.out.println("enter number"); 
    howMany = userIn.nextInt(); 

    System.out.println("enter name"); 
    name = userIn.nextLine(); 

    System.out.println("enter answer"); 
    answer = userIn.nextLine(); 

    stf.setHowMany(howMany); 
    stf.setAnswer(answer); 
    stf.setName(name); 

} 

}

回答

0

問題在你的nextInt()函數裏面只讀取整數值,當你按回車鍵時會跳過nextLine()

嘗試userIn.nextLine();這樣的主要方法。

Scanner userIn = new Scanner(System.in); 
int howMany; 
String name; 
String answer; 

System.out.println("enter number"); 
howMany = userIn.nextInt(); 

System.out.println("enter name"); 
userIn.nextLine(); 
name = userIn.nextLine(); 

System.out.println("enter answer"); 
answer = userIn.nextLine(); 
+0

非常感謝@Omore。這是我所需要的 – kezie

+0

@ kezie如果這是正確的答案,請將此答案標記爲正確,以便它可以幫助未來的訪問者。謝謝 – Omore