我正在尝试检查以确保我正在创建的对象不为空,目前我有
import java.util.Scanner;
import java.util.InputMismatchException;
public class Driver2 {
public static void main(String args[]){
Scanner keyboard= new Scanner(System.in);
//creates cars
Vehicle aCar= new Vehicle();
Vehicle bCar= new Vehicle();
Vehicle cCar= new Vehicle();
//creates trucks
Truck aTruck= new Truck();
Truck bTruck= new Truck();
Truck cTruck= new Truck();
//data checks
boolean aCarCheck= false;
boolean bCarCheck= false;
boolean cCarCheck= false;
boolean nameCheck1= false;
boolean nameCheck1Full= false;
boolean nameCheck2= false;
boolean nameCheck2Full = false;
boolean nameCheck3= false;
boolean nameCheck3Full= false;
boolean aTruckCheck= false;
boolean bTruckCheck= false;
boolean cTruckCheck= false;
while(!nameCheck1Full){
System.out.println("Enter first persons name");
Person owner1= new Person(keyboard.next());
aCar.setOwner(owner1);
if (owner1==null){
System.out.println("Please enter a name!");
nameCheck1Full=false;
}else{
nameCheck1Full=true;
}
System.out.println("You entered: " + aCar.getOwner());
while(!nameCheck2Full){
System.out.println("Please enter second persons name");
System.out.println("Please enter the same name for person 2 as person 1");
Person owner2= new Person(keyboard.next());
bCar.setOwner(owner2);
if (bCar.equals(bCar))
nameCheck2Full=true;
}
}
}
}
我一直在寻找,似乎找不到适合我的好的解决方案
我可能相距遥远,因为这是该项目的所有工作,我已经很接近放弃,但需要完成此工作
答案 0 :(得分:0)
您使用以下方法检查是否为空:
if (myObject == null) ...
有时候,您真正想做的就是检查空值:
String myValue = ...
if (myValue == null || "".equals(myValue)) ...