Question

I am very new to java code, only ever really used C++ before. I am trying to compare two string variables and if they match set a new variable to string answer else new variable would be null.

e.g.

if (ID.equals(DeviceID)){
        MobileDevice = "BB 9630";
     else
        MobileDevice = null;
     end if

But this does not seem to work when I try it, and I think the logic is correct...any help?

Was it helpful?

Solution

A simpler way to do this is to use a tri-graph, something you can do in C++ as well.

String mobileDevice = id.equals(deviceId) ? "BB 9630" : "unknown";

You should use camelCase for variables in Java.

OTHER TIPS

u need to correct your syntax.

     if (ID.equals(DeviceID)){
        MobileDevice = "BB 9630";
     }else{
        MobileDevice = null;
     }

Your syntax is just wrong for Java:

if (ID.equals(DeviceID)) {
    MobileDevice = "BB 9630";
}
else {
    MobileDevice = null;
}

Note the lack of end-if. Also you only need curly braces { } around multiple statements in an if-else block. For example, if you wanted to do two things:

if (a == 1) {
    //First action
    //Next action
    //So on..
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top