Mohsen Mirshekari Mohsen Mirshekari - 14 days ago 6
Android Question

Operator '<' cannot be applied to 'java.lang.String'

i got the system time in a string for example something like "1240".
then i wanted to do something like if the system time was < than 1240,then close the application.
but it gives me the "Operator '<' cannot be applied to java.lang.String!" Error!

My code is :

runOnUiThread(new Runnable() {
public void run() {
try{
TextView txtCurrentTime= (TextView)findViewById(R.id.showtime);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int mynum = 1240;
String curTime = hours + "" + minutes;
txtCurrentTime.setText(curTime);
if(curTime < mynum ){
System.exit(0);
}
}catch (Exception e) {}
}
});


What's the problem?

Answer

< is not defined for a string and an int of course . So you can't use it .

your current time can be calculated like this :

int curTime = 100*hours + minutes;

then you can use < between two integers.

I believe though you must use System Milliseconds which is more usual.

Comments