This is a OnClickListener for button
name is EditText
I want it print only "hi" if nothing is entered, but "hi" + name + "!" if user inputs his/her name.
public void onClick(View view) {
if (button==view) {
String message;
if (name.getText().toString().matches("")) {
message = "hi!";
return;
}
else {
message = "hi" + name.getText().toString() + "!";
return;
}
Toast toast = Toast.makeText(this,message,Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
}
}
For some reason I got error "unreachable statement" for the line:
Toast toast =...,
And if I compile and run it, the screen will output on 2 lines instead of one, such as:
Hi
!
What did I do wrong in here?
Why do you have the return statement? You need to show the Toast and then return. Remove the return statements.
Related
I want to display the value toast in text view
Code :
public void onAnimationEnd(Animation animation) {
#SuppressLint({"WrongConstant", "ShowToast"}) Toast toast = Toast.makeText(this, " " + String.valueOf((int)(((double)this.intNumber)
- Math.floor(((double)this.lngDegrees) / (360.0d / ((double)this.intNumber))))) + " ",0);
toast.setGravity(49,0,0);
this.blnButtonRotation = true;
b_start.setVisibility(View.VISIBLE);
}
i agree with Laura. It's hard to guess what you are asking for here.
If you want to show the Toast you should add .show() at the end of your toast and change the duration to Toast.LENGHT_SHORT or Toast.LENGTH_LONG instead of "0".
e.g:
Toast.makeText(this, "yourString", Toast.LENGTH_LONG).show()
If you want to access the the value of the String inside the Toast, there is currently no way to do something like that. So the only option is to store it in its own variable:
String numberAsString = String.valueOf((int(((double)this.intNumber) - Math.floor(((double)this.lngDegrees) / (360.0d / ((double)this.intNumber)))));
textView.setText(numberAsString);
//Toast toast = Toast.makeText(this, numberAsString, Toast.LENGHT_LONG).show();
I am writing an app that takes two user inputs and matches them to data stored in a database and displays the corresponding data(row) from the user inputs in a textview.
The if statement works perfectly alone if d condition is true. It however stops working if I add the else statement.
The else statement is executed if d statement is true or false
String name = Jasonobject.getString("name");
String name1 = Jasonobject.getString("name1");
String db_detail = "";
// match user input with database and display corresponding row in
// "detail" textfield
if (et.getText().toString().equalsIgnoreCase(name)
&& et1.getText().toString().equalsIgnoreCase(name1)) {
db_detail = Jasonobject.getString("detail");
text.setText(db_detail);
break;
} else {
Context context = getApplicationContext();
CharSequence text = "NOT AVAILABLE";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
break;
}
The else statement cannot run if the if statement is true at any cost.
Try to clean your project and re-compile it. Also are you executing this code in an loop, if not then you should get a misplace break error.
I'm trying to make an app where the user enters a word into an EditText box. Then, they enter something into another box and it checks to see if they are the same word. Here's the code that I used:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
textfield2.setText(textfield2.getText().toString());
if(word == answer){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}
However, it always says that the two strings aren't the same even if they are. Is there a way to fix this?
You can't compare strings with the == operator.
Use .equals() instead:
if(word.equals(answer)) {
//do whatever
}
Use String.equalsIgnoreCase for comparing content of both string variables.:
if(word.equalsIgnoreCase(answer)){
}
Use:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(answer.contentEquals(word)){
// Do something if equals
}
else{
// Do something if not equals
}
I think the best way to do this is using TextUtils:
if(TextUtils.equals(textfield1.getText(),textfield2.getText())){
//do something
}
instead of
if(word.contentEquals(answer)){
}
Use
if(word.equals(answer))
as we cant compare strings with Equal to (==) operator
Try This::
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(word.equals(answer)){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}
I have some if statements, I do not know how to show the result on the screen.
Below are 2 things I have tried. I know the system.out goes to the log.
if (Enter == "1") {
// tv.setText("This is the display 1");
System.out.println("The 1");
}
else if (Enter == "2") {
System.out.println("The 2");
}
What is Enter? If it is an instance of an object, use lowercase names, so that would be enter.
To answer the question, you're probably comparing Strings. You should use .equals instead of ==.
So:
String enter = "1"; //your variable
if(enter.equals("1")){
System.out.println("The 1");
}else if(enter.equals("2"){
System.out.println("The 2");
}
When comparing primitive data types (like int, char, boolean) you can use ==, !=, etc.
When comparing objects (like String, Car, etc) you need to use the .equals() method.
See also this page.
Edit
Use a Toast:
Toast.makeText(this, "The 1", Toast.LENGTH_SHORT).show();
See here.
Do it with a Toast instead.
A Toast is a popup-like element in Android displaying a short message for a predefined duration on screen.
String enter = "whatever value enter has";
int duration = Toast.LENGTH_SHORT;
Context context = getApplicationContext();
String message = "The Nothing";
if (enter.equals("1")) {
message = "The 1";
} else if (enter.equals("2")){
message = "The 2";
}
Toast messageToast = Toast.makeText(context, message, duration);
messageToast.show();
Trying to confirm that an EditText tool is not empty when the user clicks a button. Whenever the EditText is not blank the code works. But whenever it is blank my program crashes. Any ideas what I'm doing wrong? Thanks.
if (et1.getText().toString() != null) {
inpt_weight = Integer.parseInt(et1.getText().toString());
Toast.makeText(getBaseContext(), "weight is not null", Toast.LENGTH_LONG).show();
} else {
inpt_weight = 0;
Toast.makeText(getBaseContext(), "weight is null", Toast.LENGTH_LONG).show();
};
I think its trying to parse an empty string("") to an integer which is not possible
try:
if (!et1.getText().toString().equalsIgnoreCase("") && et1.getText().toString() != null)
This will be a good way:
if(et1.getText().toString().trim().length()==0){
Log.d("No data","No text found in the edit text");
}
This also checks if the user had put nothing but only spaces.
In your code, I see you're converting the text to numbers.
In this case, either put correct IMEMethod for the edit text so that it takes only numbers, or check in your code that user has entered only numeric characters, else you'll end up with a NumberFormatException while conversion if the user has entered non numeric characters.
Try the following code please..
if(et1.getText().toString().equals("")){
Toast.makeText(getApplication(), "weight is null", Toast.LENGTH_SHORT).show();}
else{
inpt_weight = Integer.parseInt(et1.getText().toString());
Toast.makeText(getApplication(), "weight is not null & it is : "+inpt_weight,Toast.LENGTH_SHORT).show();
}