I have the following code:
public void click(View v)
{
Button b = (Button) v;
TextView viewEditText = (TextView) findViewById(R.id.functionTextView);
String editText = viewEditText.getText().toString();
if (b.getId() == R.id.backspace)
{
if (viewEditText.length() > 0)
{
if (editText.substring(editText.length() - 1, editText.length()) == ".")
{
periodTrue = false;
}
viewEditText.setText(viewEditText.getText().toString().substring(0, viewEditText.length() - 1));
}
}
}
In this code I check to see if the character being backspaced is a period, if the previous condition is true. It sets the variable periodTrue to false. This tells the app that another period can be added.
Everything works with the backspace key as normal, except it never makes it inside the if statement where the variable is set. It throws no errors and I have checked with
viewEditText.append(editText.subString(editText.length()-1, editText.length()); to verify that the returned character is ".".
I don't know why it's not working, and it throws no errors or warnings either, on compile or run time. So I've come to you to see if a different perspective can show what I'm obviously doing wrong.
if (editText.substring(editText.length() - 1, editText.length()) == ".")
This won't work as you're comparing the references of the two Strings, which will be different. (More in-depth explanation in this question: How do I compare strings in Java?)
Use the following instead:
if (".".equals(editText.substring(editText.length() - 1, editText.length())))
Related
This would be the first time I post a question since I couldn't find the answer to this. Something really weird is happening with my if/else statements. My code was working perfectly for the past week, but recently it kept on telling me that a statement is always true?
My code is the following:
int checking = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checking != 1) {
speaker.setChecked(true);
} else if (checking == 1) {
speaker.setChecked(false);
}
}
This says checking == 1 is always true which makes sense, but if I switch it around to:
if (checking == 1) {
speaker.setChecked(true);
} else if (checking != 1) {
speaker.setChecked(false);
}
This says that checking != 1 is always true as well. Can someone help?
It's just a logic of editor. In first if statement, you check checking == 1 and the second if statement, you negative the statement. The editor will understand the second if statement alway true
I think you should replace else if to else. Sorry my bad English.
The reason why you are seeing this warning is:
You have set int checking = 1 and you are either checking:
if (checking != 1) which is always true
Or checking == 1 which is also always true
Due to this other else if condition will not be executed at all.
The warning will be only removed if you try to change value of checking either at runtime or using some conditions at compile time.
If your intention is just to set the speaker.setChecked() you can do following:
speaker.setChecked(checking == 1)
Note: This will not remove the warning
Try this
int checking = 1;
boolean isChecked = false;
isChecked = ((checking == 1) ? true : false)
speaker.setChecked(isChecked)
Here the Text Area is constantly changing in terms of number and I want to trigger an event when the Text Area gets a particular number example I have tried this -
public void myfunction45(Canvas Panel)
{
if (Indicator = 45) {
Panel.enabled = false;.
}
} //(indicator- www.progress).
But it does not work(it does not read it nothing happens). how do I match the condition as the number is to be specific. please give an example for explanation. Thanks in advance.
That if statement would cause you problems.
You would want:
if(Indicator == 5)
instead. At the moment you're assigning the value without checking it, this would cause a compiler error. If it's just a typo, then update your answer, slightly confusing otherwise.
With regards to checking the text value. You'd have to grab the text value, for that you need a reference to the Text area. This approach assumes that the text area has it's value set by a user. Currently you're not grabbing any text values to compare, as a result, the if statement won't know what to compare.
Here's one approach:
public void myfunction5(Canvas Panel)
{
float result;
string textValue = yourTextArea.text;
if(Single.TryParse(textValue, out result))
{
if(result == Indicator)
{
Panel.enabled = false;
}
}
}
You use TryParse to avoid any potential exceptions that would be thrown if the user entered something that wasn't a number. This method will take the value from your text area, how you get your text area is up to you, and try to parse the text value into a float. The method will return true if the parse was a success, false otherwise.
Here's the reference for the TryParse stuff:
https://msdn.microsoft.com/en-us/library/26sxas5t(v=vs.110).aspx
If you wanted to parse it to an int, then you'd be using the Int32's version of TryParse, https://msdn.microsoft.com/en-us/library/system.int32_methods(v=vs.110).aspx
I'd also recommend having a peak at the Input Field documentation: https://docs.unity3d.com/Manual/script-InputField.html
You can subscribe your method to the Input-fields On Value Changed event, your function will need to tweaked slightly though:
public void myfunction5(string text)
{
float result;
if(Single.TryParse(text, out result))
{
if(result == Indicator)
{
CachedPanel.enabled = false;
}
}
}
Don't forget to store a reference to the panel you want to disable.
Hopefully this is what you're after.
Panel is already a Canvas type, it doesn't make any sense to GetComponent<Canvas> on the same type.
Try using Panel.enabled = false;.
For the rest, we don't know how you get the Indicator reference, or how you built the UI hierarchy, so we can't assess if the problem is there.
Edit: I could I miss the single = baffles me lol. I should avoid answering questions when I'm tired.
public void click(View view) {
String one = "one";
EditText et = (EditText)findViewById(R.id.editText1);
String entered_text = et.getText().toString();
if(et.getText().toString() == one){
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Correct!");
}
else {
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(one+entered_text); }
}
This is a code snippet extracted from my program, I didn't post the whole program because it wasn't necessary, as the program runs fine without any runtime exceptions.
So, the program when executed on Eclipse doesn't show any errors and runs fine, but when run the, if condition et.getText().toString() == "one" always returns false even when the entered_text is one i.e.; it never prints "correct!" and the code always prints "one+entered_text" that is the statement in the else clause. And the interesting point is, if you enter one the output will be oneone, that is the else statement.
Please help me where I went wrong.
Thanks in advance.
Try this
if(et.getText().toString().equals(one)){
instead of
if(et.getText().toString() == one){
the better way is to compare string like et.getText().toString().equals("one");
also do et.getText().toString().trim().equals("one"); to avoid spaces error
I have a button that sends message - however the onClick feature in the onClickListener seems to give me a few errors when I tell it to disable the button when click, AND when I set toast to it. Oh, and the error underlines 'else' saying syntax error. (Other than that the code works fine and sends the SMS)
public void onClick(View v)
{
messageinfo mi = new messageinfo();
String message = txtMessage.getText().toString();
if (message.length()>0)
sendSMS(mi.SMSNO(), smsmessage);
Toast.makeText(getBaseContext(), "sending", Toast.LENGTH_SHORT).show();
myButton.setEnabled(false);
else
Toast.makeText(getBaseContext(), "enter your message", Toast.LENGTH_SHORT).show();
}
Is there an easy solution to this?
Through formatting your code I've noticed that it would appear that your if statement is failing.
You need to have { }s around the resulting code from an if statement, eg:
if(true){
line 1;
line 2;
line 3;
}
else {
line 1;
line 2;
line 3;
}
The only time you can miss out the { }s is when you've only got ONE statement following an if. This is always a bad idea however, its not much effort just to put in some { }s, and means that issues like this won't ever happen.
Clarification
The code in your question is running the if and if it matches it runs THE NEXT LINE. Then the code carries on OUTSIDE of the if clause (because of missing brackets)so then will run the next two lines REGARDLESS of the outcome of the if statement.
Then it encounters the else which its not expecting. Its supposed to be after either { .. } or directly after the FIRST statement after the if, so that's why it fails at the else.
I have a program that relates to a database. I start out my program by inserting a row using the following command.
long id = db.insertMajor(null, null, null, null);
it works perfect. I than alter the information but lets ignore that since for the time being I commented out all those lines. Now I want to check and see if it is null which is should be. If it is not null than I would like to check a checkbox. if it is null than I want to leave the box unchecked. So far I have this line to test.
String change = db.getMajorTitle(value).getString(1);
if (change.equals(null)) {
filled = false;
}
the filled boolean will check the box or not. If the database field is filled or not I always get a checkmark. Whats wrong?
UPDATE:
Since no one has figure it out I will post most of my code to try and help figure it out. I have also reset my emulator to ensure there is not problems with corrupted data.
chk1.setChecked(checker(1));
chk2.setChecked(checker(2));
chk3.setChecked(checker(3));
chk4.setChecked(checker(4));
}
boolean checker(int value){
DBAdapter db = new DBAdapter(this);
boolean filled = false;
db.open();
for (int i = 1; i <= 4; i++) {
String change = db.getMajorTitle(value).getString(1);
if (change == null) {
filled = false;
}else{
filled = true;
}
}
db.close();
return filled;
}
If someone copies this and it works can you please tell me what might be wrong with my emulator or eclipse or whatever. Thanks everyone for there input.
FINISHED:
I noticed that at another part in my program I changed null to "". I would like to thank everyone for being right and showing me different ways to code it.
You really don't need if statements for this.
filled = (change != null);
if (change.equals(null)) {
If change instance is not null, the above condition always returns false. Read Object equality constraints for more info.
If you want to check against null use
if( change == null) {
// change is null, update filled field
} else {
// change is not null, update filled field
}
If the database field is filled or not
I always get a checkmark. Whats wrong?
I guess that you would have initialized the filled field as true and never set it as false since your if condition always returns false or throws NullPointerException.
User ExceptionHandling mechanism. then you will succeed. Like
try {
if(change!=null)
{
//do ur work,if not null
}
else
{
// do some thing
}
}catch(NullPointerException e)
{
//do ur work,if null.
}