I've been working on a personal Java project which essentially is mimicking a trading common backtesting spreadsheet. Everything has been fine up until the use of multiple instanceof
operators in one line. I've had a look online and to no avail have found nothing relevant. I receive an incompatible conditional operand on the comparison of the double
type and int
however the string is fine. The editor I use is VSCode. Code is below
while (newLogBool){
Scanner newLog = new Scanner(System.in);
String nLPair = newLog.nextLine();
double nLStartingBalance = newLog.nextDouble();
int nLRisk = newLog.nextInt();
try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
if ((nLPair instanceof String) && (nLStartingBalance instanceof double) && (nLRisk instanceof int)){
Logger logger = new Logger(nLPair, nLStartingBalance, nLRisk);
newLogBool = false;
} else {
System.out.println("Inccorect Type: Pair, Starting Balance or Risk. Please try again.");
}
}
Thanks
CodePudding user response:
The instanceof
operator can only be applied to reference types (or null). double
and int
are primitive types, not reference types.
Java Language Specification, 15.20.02
In your case, for example, an int
is an instance of an int
, and cannot be otherwise. So there's no need to be able to ask that particular question.