我正在尝试在java中编写一个创建类的对象的应用程序,并接受用户输入以填充对象的属性,并且如果用户输入无效值,则可以抛出异常。我将异常的条件放在名为Stuff.java的类中的setter方法中,并创建了异常类。我的问题是在包含main方法的类中,我如何对其进行编码以提示用户一个接一个地输入属性值,并且必须继续提示用户输入,直到用户填写了值为三个属性。这是我下面的代码,具有main方法的类并没有真正给我我想要的东西。我必须做些什么改变?
Stuff类:
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 = "!@#%&*";
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
}
}
使用main方法的类,这是我认为我遇到错误的地方:
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 :(得分: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();