Reputation: 33
I am building my first android app after watching some videos. I am just building a basic app that takes input of grade from user and notifies them using Toast what their letter grade is. I have implemented both the Java and the Xml file correctly and the simulator runs perfect, but the logic of the Java is wrong. Every number entered results in the return of the if-statement "Please Enter a Valid Grade" instead of the actual grade.
Here is my Xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".GradeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to SJU Grading App"
android:paddingTop="30sp"
android:paddingBottom="20sp"
android:textSize="25sp"
android:layout_gravity="center" />
<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Numeric Grade Received out of 100"
android:layout_gravity="center"
android:paddingBottom="20sp"/>
<EditText
android:id="@+id/directions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0"
android:background="#CCCCCC"
android:ems="8"
android:inputType="number" />
<Button
android:id="@+id/sumbitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:text="Sumbit"
android:background="#0099FF"/>
</LinearLayout >
and here is my java file:
package com.example.yberh703.gradecalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class GradeActivity extends AppCompatActivity {
Button myButton;
EditText input;
public void getGrade(EditText input){
input = (EditText)findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if(grade>90){
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
}
if(grade>80&& grade<90){
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
}
if(grade>70&& grade<80){
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
}
if(grade>60&&grade<70){
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
}
if(grade>0&&grade<60){
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
myButton =(Button)findViewById(R.id.sumbitButton);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getGrade((EditText)findViewById(R.id.directions));
}
});
}
}
I appreciate all the help I can get. I have a feeling this is stupid simple to solve but I have been trying to figure it out for a minute now.
Thanks
Upvotes: 0
Views: 77
Reputation: 1275
public void getGrade(EditText input) {
input = (EditText) findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if (grade > 90) {
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
} else if (grade >= 80 && grade < 90) {
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
} else if (grade >= 70 && grade < 80) {
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
} else if (grade >= 60 && grade < 70) {
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
} else if (grade > 0 && grade < 60) {
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Reputation: 1225
Use else if
public void getGrade(EditText input){
input = (EditText)findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if(grade>90){
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
} else if (grade>80&& grade<90) {
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
} else if(grade>70&& grade<80){
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
} else if(grade>60&&grade<70){
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
} else if(grade>0&&grade<60){
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 3
Reputation: 922
This happens because your last If Condition(if(grade>0&&grade<60)) is always getting checked, irrespective of whether any of the above If Conditions were true or not, and since the grade input you might be giving was failing that last if condition making it enter the ELSE part, Hence you were getting "Please Enter A Valid Grade" everytime.
Change your IF construct to below format.
public void getGrade(EditText input){
input = (EditText)findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if(grade>90){
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
}
else if(grade>80&& grade<90){
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
}
else if(grade>70&& grade<80){
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
}
else if(grade>60&&grade<70){
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
}
else if(grade>0&&grade<60){
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}
Now, at any point of time, only single IF/ELSE condition would be evaluated if found to be true.
Upvotes: 3