i am trying to develop an app that scans the existing WiFi signal surrounded my deceive and if specific WiFi signal is existed, the program will do specific instructions as follows in the program bellow :
for (ScanResult result: results) {
int strength1 = info.getRssi ();
float strength_in_pre11=2*(strength1+100);
float Distance1 =30*(1-(strength_in_pre11/100));
tv = (TextView) findViewById (R.id.textView1);
if (result.SSID.equals("WiFi1")) {
String text="We are connecting to "+SSID+" Distance:"+Distance1+" " ;
tv.setText (text);
}
else {
String otherwifi1="The existing network is:\n\n";
otherwifi1+=result.SSID+":"+result.level+"\n\n";
otherwifi1+=result.SSID+":"+result.level+":"+Distance1+"\n";
tv.setText (otherwifi1);
}
}
well , the if statement is not satisfied even though the router name that i put in the if statement (WiFi1) is existed , only else condition is satisfied ?!
anyone can help
thank you
Check what's the value of result.SSID :
System.out.println(result.SSID.toString());
if (result.SSID.toString().equals("WiFi1")) {
String text="We are connecting to "+SSID+" Distance:"+Distance1+" " ;
tv.setText (text);
}
Can you try and put a log or syso to see what the ssid is ? and whether it has whitespaces or quotes around the actual name?
i current did something similar and i had to change my if to the following as the ssids i encountered had quotes
if (result.SSID.equals('"' + "WiFi1" + '"'))
Related
I want to compare a xml string with a string from an edittext oder button.
first I set the text of the button:
button1.setText(getString(R.string.okey));
and now I want to check if the text from the button is the same as R.string.okey from the xml file. Like this I can leave out a new variable.
Is it possible to check if the strings are the same with something like this?
if (button1.getText().toString().equals(getString(R.string.okey))){
}
But that doesn't work for me.
Thank you in advance.
this must work, its just to simple. you must change somehow text on button or maybe getString returns different text (Locale changed?). use logging or debugger to check what is button1.getText().toString() and getString(R.string.okey) at the moment of comparison (equals call)
boolean areEqual = button1.getText().toString().equals(getString(R.string.okey));
Log.i("justChecking", "getString:" + getString(R.string.okey) +
", button1.getText:" + button1.getText().toString() +
", are equal:" + areEqual);
if (areEqual){
}
How to Write and View Logs with Logcat
Store them in variables
String a = button1.getText()+"";
String b = getString(R.string.str)+"";
if(a.equals(b)){ }
I wrote a code in java that should get a number from an EditText by clicking on a button and print it in console.that's so simple but when I click on the edittext in order to write a number in it I see this message in logcat that
:
09-16 23:57:02.121: W/Editor(27812): GetLabel fail! Do framework orig behavior
and after clicking on the button nothing happens!
this is a part of my code :
EditText txt1=(EditText)findViewById(R.id.editText1);
Editable t1;
t1=txt1.getText();
if(!(t1.toString().equalsIgnoreCase("0") || t1.toString().equalsIgnoreCase("null"))){
int n1 = Integer.parseInt(t1.toString());
int m1 = n1/100000;
System.out.print(n1);
System.out.print(m1);
}
I'll be happy if anyone can help me by that :)
When you compare Strings you should use the equals() on the String Class.
if anyone faced to the same problem as mine , just be sure that there's no problem with this warning :"
09-16 23:57:02.121: W/Editor(27812): GetLabel fail! Do framework orig behavior
just pay attention more ! maybe you have done something funny like me :
System.out.print(n1);
System.out.print(m1);
I solved my problem by changing print with println ....
so simple ;-)
Try this;
EditText editText=(EditText) findViewById(R.id.editText1);
String text = editText.getText().toString();
if( text!=null && !text.equalsIgnoreCase("0") ) {
int num1 = Integer.parseInt(text);
int num2 = num1/100000;
Log.d(TAG, "Num1 " + num1);
Log.d(TAG, "Num2 " + num2);
}
If we have a phone number like 358541321 without a country code, sometimes when phone rings it says (+56 - 358541321) or +56358541321.
How to detect whether the ringed number is first number?
The number is not saved in phone memory in order to phone lookup.
https://developer.android.com/reference/android/telephony/PhoneNumberUtils.html
provides a neat solution:
import android.telephony.PhoneNumberUtils;
...
String one = "+51 - 3245678";
String two = "+513245678";
boolean isSame = PhoneNumberUtils.compare(one, two);
The usual solution to this problem is just to compare the last X (e.g. 7 or 8, depending on your country) digits of the number. In rare cases, this can lead to false positives, but usually it's a good approximation and it avoids the problem of different or missing country or area codes.
Java regular expression and String function replaceAll can do this easily.
this way,
String one = "+51 - 3245678";
String two = "+513245678";
one = one.replaceAll("[^0-9]","");
two = two.replaceAll("[^0-9]","");
Toast.makeText(this, one+" -- "+two, Toast.LENGTH_LONG).show();
if(one.equalsIgnoreCase(two))
{
Toast.makeText(this, "Both Are Equal", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Different", Toast.LENGTH_LONG).show();
}
I need help with this function.
I know that the if statement recognizes my input because it affects the program elsewhere, but I'm not sure what's going on because this particular Log doesn't display anything even in adb logcat.
Other Log statements in the same class file that this function is from display just fine, and the value update does seem to be changing ("show all" blanks it for some reason but I can figure that out after I get the log to work.)
I am unsure how to search for this problem because it is very specific and I have no idea what causes it (probably something simple that I didn't think of, though.)
void command(String input)
{
//do stuff here
//update = whatever
if(input.equalsIgnoreCase("show all"))
{
update=printAllRooms();
Log.i(input, update);
}
else update=input; //just for testing, will delete later
}
the printAllRooms function:
public String printAllRooms() //for debug purposes
{
String result = "";
for (Iterator<Room> iterator = rooms.iterator(); iterator.hasNext();) {
Room current = iterator.next();
result = result + current.toString()+"\n";
Log.i("printallrooms", current.toString());
}
return result;
}
A note on using Log.
The first argument sent to Log is typically a fixed string indicating the name of the class you are in.
So at the top of your class you might define:
private static final String TAG = "MyClassName";
Then you would use TAG for your log statements in that class.
Log.i(TAG, "My input was: " + input + " Update was: " + update;
To put it mildly, your function looks quite odd. Set a breakpoint at your Log statement, run the debugger and then inspect the variable value contained in update. Most likely, printAllRooms() is not doing what you think.
If the iterator doesn't work for you, try using the For-Each loop:
for (Room r : rooms) {
result = result + r.toString()+"\n";
Log.i("printallrooms", r.toString());
}
My app has a number of numeric user input fields which need sanity checks before proceeding to the next intent.
I read viewText fields, convert them to double and then do the (numeric) tests but odd things happen and I find that while the code runs on my HTC in debug, it falls over if I publish then download the published version. My code is sumarised as;
String sFy;
double mFy=0;D
sFy=(txtFy.getText().toString());
mFy=Double.parseDouble(sFy);
if sFy is null the .parsedouble crashes. If I use;
sFy=(txtFy.getText().toString());
mFy=getDouble(sFy);
private double getDouble(String string){
double temp=0.0;
try {
temp = Double.parseDouble(string.trim());
} catch(NumberFormatException nfe) {
System.out.println("getDouble, Could not parse " + nfe);
}
return temp;
}
it works, even if sFy is empty.
Can anyone tell my why, or suggest a 'correct' method?
Maybe like that :
String sFy;
double mFy=0;
sFy = txtFy.getText();
if ((sFy != ""){
mFy=Double.parseDouble(sFy);
}
Or maybe I haven't really understood your problem...
Your getDouble is returning 0.0 in case there is NumberFormatException.
Do you see debugger coming to System.out.println("getDouble, Could not parse " + nfe);