好的,所以我很清楚您可以使用扫描仪读取输入和设置输出。但是,我使用的是多类包。在一个类中,我有一个具有未定义参数的抽象方法。输入无效参数时,它会抛出异常消息。
public class PersonalContact extends Contact
{
/*These are constants reserved for this class,
* they can be called in this method, but never changed
* in sub-classes.
*/
private String address;
private String city;
private String state;
private String zip;
// static limiters
private static int AGEMIN = 1;
private static int AGEMAX = 100;
private static int STATEMAX = 2;
private static int ZIPMAX = 5;
private static int CHARMAX = 30;
public PersonalContact(String name, int age, String address, String city, String state, String zip)
{
super(name, age);
//We use the CHARMAX constant to prevent idiots from holding down the spacebar for +30 characters
if (name.isEmpty() || name == null || name.length() > CHARMAX)
{
throw new NullPointerException("You must enter a name value. ");
}
else if (age > AGEMAX || age < AGEMIN)
{
throw new IllegalStateException("Please enter an age between 1 and 100");
}
else if (address.isEmpty() || address == null || address.length() > CHARMAX)
{
throw new NullPointerException("You must provide your address. ");
}
else if (city.isEmpty() || city == null || city.length() > CHARMAX)
{
throw new NullPointerException("You must provide your city name. ");
}
else if (state.isEmpty() || state == null || state.length() > STATEMAX )
{
throw new IllegalStateException("You must provide your abbreviated state name. ");
}
else if (zip.isEmpty() || zip == null || zip.length() > ZIPMAX )
{
throw new IllegalStateException("You must provide a 5 digit zipcode. ");
}
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
}
}
在我的驱动程序类(运行所有类的类)中,我有一个set方法,它有“try”和“catch”来查看错误,如果出现错误则返回(抛出)一条消息。有用!但是,我想知道如何使用Scanner在使用PersonalContact类中的方法时在驱动程序类中设置输入。我试过了,它不会让我。我已经找到了答案,但我什么都没得到。请帮帮我?
public class Driver
{
public static void personalConsole()
{
try
{
Contact personals = new PersonalContact("Robert", 22, "", "Seattle", "WA", "98144");
System.out.println(personals);
}
catch (RuntimeException ex)
{
System.out.println(ex.getMessage());
}
System.out.println("");
System.out.println("Process Complete!");
}
}
我已经找到了答案,但我什么都没得到。请帮帮我?
答案 0 :(得分:0)
无视,我想通了
我应该设置两个不同的扫描仪对象 在try&#34;方法&#34;之前防止输入重叠(扫描仪不喜欢在string和int之间交替)已定义。
public static void personalConsole()
{
Scanner scanner = new Scanner(System.in);
Scanner scanint = new Scanner(System.in);
try
{
System.out.println("Enter a name: ");
String name = scanner.nextLine();
System.out.println("Enter an age: ");
int age = scanint.nextInt();
System.out.println("Enter an address: ");
String address = scanner.nextLine();
System.out.println("Enter a city: ");
String city = scanner.next();
System.out.println("Enter an state: ");
String state = scanner.next();
System.out.println("Enter a zipcode: ");
String zip = scanner.next();
Contact personals = new PersonalContact(name, age, address, city, state, zip);
System.out.println(personals);
}
catch (RuntimeException ex)
{
System.out.println(ex.getMessage());
}
System.out.println("");
System.out.println("Process Complete!");
}