Omar Abdelhafiz Omar Abdelhafiz - 5 months ago 7
Android Question

When I declare a double and assign a value it returns an integer value

When I declare a global variable as double and assign a value it returns an integer value.

Using debug mode, I observe that the compiler initializes the variable with an integer value as opposed to a floating point, as shown in the picture.

enter image description here

Edit:

ok here is the whole code==

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ommabdelhafiz.amathmess.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">











<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="7"
android:textSize="16sp"
android:id="@+id/first_1st"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="×"
android:textSize="16sp"
/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="7"
android:textSize="16sp"
android:id="@+id/first_2nd"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="="
android:textSize="16sp"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:inputType="number"
android:maxLength="3"
android:hint=" "
android:id="@+id/first_ans"
android:imeOptions="actionDone"
android:layout_marginRight="20dp"/>








<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:layout_gravity="center_horizontal"
android:id="@+id/first_true_ans"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:gravity="bottom"
android:layout_gravity="center_horizontal"
android:id="@+id/first_false_ans"/>

</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:layout_marginLeft="20dp"
android:onClick="check1st"
android:id="@+id/first_btn"/>

</LinearLayout>






<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="49"
android:textSize="16sp"
android:id="@+id/second_1st"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="÷"
android:textSize="16sp"
/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="7"
android:textSize="16sp"
android:id="@+id/second_2nd"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="="
android:textSize="16sp"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:inputType="number"
android:maxLength="2"
android:hint=" "
android:id="@+id/second_ans"
android:imeOptions="actionDone"
android:layout_marginRight="20dp"/>









<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:layout_gravity="center_horizontal"
android:id="@+id/second_true_ans"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:gravity="bottom"
android:layout_gravity="center_horizontal"
android:id="@+id/second_false_ans"/>

</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:layout_marginLeft="20dp"
android:onClick="check2nd"
android:id="@+id/second_btn"/>







</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="711"
android:textSize="16sp"
android:id="@+id/third_1st"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="×"
android:textSize="16sp"
/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="7"
android:textSize="16sp"
android:id="@+id/third_2nd"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="="
android:textSize="16sp"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:inputType="number"
android:maxLength="4"
android:hint=" "
android:id="@+id/third_ans"
android:imeOptions="actionDone"
android:layout_marginRight="20dp"/>








<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:layout_gravity="center_horizontal"
android:id="@+id/third_true_ans"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:gravity="bottom"
android:layout_gravity="center_horizontal"
android:id="@+id/third_false_ans"/>

</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:layout_marginLeft="20dp"
android:onClick="check3rd"
android:id="@+id/third_btn"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="41.735"
android:textSize="16sp"
android:id="@+id/fourth_1st"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="+"
android:textSize="16sp"
/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="23.836"
android:textSize="16sp"
android:id="@+id/fourth_2nd"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="="
android:textSize="16sp"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:inputType="numberDecimal"
android:hint=" "
android:maxLength="7"
android:id="@+id/fourth_ans"
android:imeOptions="actionDone"
android:layout_marginRight="20dp"/>








<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:layout_gravity="center_horizontal"
android:id="@+id/fourth_true_ans"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:gravity="bottom"
android:layout_gravity="center_horizontal"
android:id="@+id/fourth_false_ans"/>

</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:layout_marginLeft="20dp"
android:onClick="check4th"
android:id="@+id/fourth_btn"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="71.159"
android:textSize="16sp"
android:id="@+id/fifth_1st"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="-"
android:textSize="16sp"
/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="14.704"
android:textSize="16sp"
android:id="@+id/fifth_2nd"/>

<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="="
android:textSize="16sp"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:inputType="numberDecimal"
android:maxLength="7"
android:hint=" "
android:id="@+id/fifth_ans"
android:imeOptions="actionDone"
android:layout_marginRight="20dp"/>








<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:layout_gravity="center_horizontal"
android:id="@+id/fifth_true_ans"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="16dp"
android:gravity="bottom"
android:layout_gravity="center_horizontal"
android:id="@+id/fifth_false_ans"/>

</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:layout_marginLeft="20dp"
android:onClick="check5th"
android:id="@+id/fifth_btn"/>

</LinearLayout>









</LinearLayout>
</ScrollView>
</RelativeLayout>


Java:

package com.example.ommabdelhafiz.amathmess;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

int firstTrueAns = 0;
int firstFalseAns = 0;
int firstFirstValue = 7;
int firstSecondValue = 7;

int secondTrueAns = 0;
int secondFalseAns = 0;

int thirdTrueAns = 0;
int thirdFalseAns = 0;
int thirdFirstValue = 711;
int thirdSecondValue = 7;

int fourthTrueAns = 0;
int fourthFalseAns = 0;
double fourthFirstValue = 41.735;
double fourthSecondValue = 23.836;

int fifthTrueAns = 0;
int fifthFalseAns = 0;
double fifthFirstValue = 71159;
double fifthSecondValue = 14704;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}


public void check1st(View v) {
int firstRealAns ;
TextView first1st = (TextView) findViewById(R.id.first_1st);
TextView first2nd = (TextView) findViewById(R.id.first_2nd);
TextView firstTrueAnsText = (TextView) findViewById(R.id.first_true_ans);
TextView firstFalseAnsText = (TextView) findViewById(R.id.first_false_ans);
EditText firstAns = (EditText) findViewById(R.id.first_ans);

firstRealAns = firstFirstValue * firstSecondValue;


if (firstAns.getText().toString().equals(String.valueOf(firstRealAns))) {

firstTrueAns += 1;
firstTrueAnsText.setText(String.valueOf(firstTrueAns));

} else {

firstFalseAns += 1;
firstFalseAnsText.setText(String.valueOf(firstFalseAns));
Toast.makeText(this, "Wrong answer, The true answer is " + firstRealAns, Toast.LENGTH_LONG).show();
}

Random rand = new Random();
firstFirstValue = rand.nextInt(13) + 1;
firstSecondValue = rand.nextInt(13) + 1;
first1st.setText(String.valueOf(firstFirstValue));
first2nd.setText(String.valueOf(firstSecondValue));
firstAns.setText("");
}

public void check2nd() {

}


public void check3rd(View v) {
int thirdRealAns = 0;
TextView third1st = (TextView) findViewById(R.id.third_1st);
TextView third2nd = (TextView) findViewById(R.id.third_2nd);
EditText thirdAns = (EditText) findViewById(R.id.third_ans);
TextView thirdTrueAnsText = (TextView) findViewById(R.id.third_true_ans);
TextView thirdFalseAnsText = (TextView) findViewById(R.id.third_false_ans);


thirdRealAns = thirdFirstValue * thirdSecondValue;


String ans = thirdAns.getText().toString();
if (ans.equals(String.valueOf(thirdRealAns))) {

thirdTrueAns += 1;
thirdTrueAnsText.setText(String.valueOf(thirdTrueAns));

} else {

thirdFalseAns += 1;
thirdFalseAnsText.setText(String.valueOf(thirdFalseAns));
Toast.makeText(this, "Wrong answer, The true answer is " + thirdRealAns, Toast.LENGTH_LONG).show();
}

Random rand = new Random();
thirdFirstValue = rand.nextInt(10) + 1;
thirdSecondValue = rand.nextInt(1000) + 1;
third1st.setText(String.valueOf(thirdFirstValue));
third2nd.setText(String.valueOf(thirdSecondValue));
thirdAns.setText("");
}

public void check4th(View v) {
double fourthRealAns ;
TextView fourth1st = (TextView) findViewById(R.id.fourth_1st);
TextView fourth2nd = (TextView) findViewById(R.id.fourth_2nd);
TextView fourthTrueAnsText = (TextView) findViewById(R.id.fourth_true_ans);
TextView fourthFalseAnsText = (TextView) findViewById(R.id.fourth_false_ans);
EditText fourthAns = (EditText) findViewById(R.id.fourth_ans);

fourthRealAns = (fourthFirstValue + fourthSecondValue) / 1000;


if (fourthAns.getText().toString().equals(String.valueOf(fourthRealAns))) {

fourthTrueAns += 1;
fourthTrueAnsText.setText(String.valueOf(fourthTrueAns));

} else {

fourthFalseAns += 1;
fourthFalseAnsText.setText(String.valueOf(fourthFalseAns));
Toast.makeText(this, "Wrong answer, The true answer is " + fourthRealAns, Toast.LENGTH_LONG).show();
}

Random rand = new Random();
fourthFirstValue = (rand.nextInt(100000) + 1) / 1000;
fourthSecondValue = (rand.nextInt(100000) + 1) /1000;
fourth1st.setText(String.valueOf(firstFirstValue));
fourth2nd.setText(String.valueOf(firstSecondValue));
fourthAns.setText("");
}

public void check5th(View v) {
double fifthRealAns ;
TextView fifth1st = (TextView) findViewById(R.id.fifth_1st);
TextView fifth2nd = (TextView) findViewById(R.id.fifth_2nd);
TextView fifthTrueAnsText = (TextView) findViewById(R.id.fifth_true_ans);
TextView fifthFalseAnsText = (TextView) findViewById(R.id.fifth_false_ans);
EditText fifthAns = (EditText) findViewById(R.id.fifth_ans);

fifthRealAns = fifthFirstValue - fifthSecondValue;


if (fifthAns.getText().toString().equals(String.valueOf(fifthRealAns))) {

fifthTrueAns += 1;
fifthTrueAnsText.setText(String.valueOf(fifthTrueAns));

} else {

firstFalseAns += 1;
fifthFalseAnsText.setText(String.valueOf(fifthFalseAns));
Toast.makeText(this, "Wrong answer, The true answer is " + fifthRealAns, Toast.LENGTH_LONG).show();
}

Random rand = new Random();
fifthFirstValue = rand.nextInt(100000) + 1;
fifthSecondValue = rand.nextInt((int) fifthSecondValue) + 1;
fifth1st.setText(String.valueOf(fifthFirstValue / 1000));
fifth2nd.setText(String.valueOf(fifthSecondValue / 1000));
fifthAns.setText("");
}
}

Answer

You should add a (d) after the value before the (;) like this example:

fourthFirstValue = 41.735d ;