# Thread: Help with calculation - any help will be great!

1. Member
Join Date
Aug 2011
Posts
8
Rep Power
0

## Help with calculation - any help will be great!

hello,

I'm very new to java and trying to build a simple app.

can anyone explain to me how to do a calculation:

total = (n*v)*Q/1000

N,V and Q will be imputed by the user.

and the total will be displayed in a TextView (if possible)

any help will be great!

thanks

2. Member
Join Date
Aug 2011
Posts
47
Rep Power
0
google Java GUI, specifically JTextField, okbutton, and Jlabel

I know, its a hell of a learning curve to get started. But, i think that should get you going enough to do this one. That being said, having a firm grasp of what you want the program to do is a great start!

That should be all the info youll need to do this one.

3. Member
Join Date
Aug 2011
Posts
8
Rep Power
0
sorry, like i said I'm VERY new to java, I've only been learning for like 2 weeks and be my self, could you explain what each thing is and what they do?

4. Assuming you are getting the text from a textfield:

Java Code:
```String nVal = textField1.getText();
String vVal = textField2.getText();
String qVal = textField3.getText();```
These values are strings, since .getText() returns a String value. So, you have to convert them if you want to perform mathematical operations, so assuming the user can enter a decimal value, you should probably use doubles.

Java Code:
```double n = Double.parseDouble(nVal);
double v = Double.parseDouble(vVal);
double q = Double.parseDouble(qVal);```
Then, you would simply do the calculation:

Java Code:
`double answer = ((n*v)*q)/1000;`
If you are using a JTextField, setText only accepts a String value, so you'll have to convert your double back to a string:

Java Code:
`String myString = Double.toString(answer);`
Then, set the text of your JTextField:

Java Code:
`myJTextField.setText(myString);`

5. Member
Join Date
Aug 2011
Posts
8
Rep Power
0
thanks sehudson

i'll give it ago and hopefully it will work

6. Member
Join Date
Aug 2011
Posts
8
Rep Power
0
I have tried this but i cant seem to get the TextField1 2 and 3 to work i'm making a android app if it makes any difference and the user is putting the data in to a number TextField and its label is editText1 ,2,3and 4

7. Originally Posted by venictus
I have tried this but i cant seem to get the TextField1 2 and 3 to work i'm making a android app if it makes any difference...
Yes, it makes a huge difference and for future reference, needs to be told up front in your initial post.

Moving to the android forum.

8. yea, it does make a difference. I know a little something about android. Once you add the buttons to your XML layout that corresponds to your activity, you have to initialize them. Something like:
Java Code:
```public EditText editText1;
public EditText editText2;
public EditText editText3;
public EditText editText4;

editText2= (EditText)findViewById(R.id.editText2);
editText3= (EditText)findViewById(R.id.editText3);
editText4= (EditText)findViewById(R.id.editText4);```
Then, if you get the n,v, and q values after they hit a button, you want to put a listener on the button:

Java Code:
```	        myButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
String nVal = editText1.getText();
String vVal = editText2.getText();
String qVal = editText3.getText();

double n = Double.parseDouble(nVal);
double v = Double.parseDouble(vVal);
double q = Double.parseDouble(qVal);

editText4.setText(myString);
}});```
Last edited by sehudson; 08-18-2011 at 12:36 AM.

9. Member
Join Date
Aug 2011
Posts
8
Rep Power
0
Thanks that a big step in the right direction lol but i'm now having a problem with the setonclicklistener, i have:

EditText editText1 = (EditText)findViewById(R.id.editText1);
EditText editText2 = (EditText)findViewById(R.id.editText2);
EditText editText3 = (EditText)findViewById(R.id.editText3);
EditText editText4 = (EditText)findViewById(R.id.editText4);
Button MyButton1 = (Button) findViewById(R.id.button1);

MyButton1.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
String nVal = editText1.getText();
String vVal = editText2.getText();
String qVal = editText3.getText();

double n = Double.parseDouble(nVal);
double v = Double.parseDouble(vVal);
double q = Double.parseDouble(qVal);

editText4.setText(myString);;

but the error i'm getting is:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new
OnClickListener(){})

10. Member
Join Date
Aug 2011
Posts
8
Rep Power
0
i just want to recap to everyone knows what i'm trying to do:-

explain to me how to do a calculation:

total = (n*v)*Q/1000

then:

total / P

N,V ,Q and P will be imputed by the user.

and the answer to be displayed in TextView1

Current Code

textField1 = (EditText)findViewById(R.id.editText1);
textField2 = (EditText)findViewById(R.id.editText2);
textField3 = (EditText)findViewById(R.id.editText3);
TextField4 = (EditText)findViewById(R.id.editText4);
Button MyButton1 = (Button) findViewById(R.id.button1);

MyButton1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View z) {
String nVal = (String) textField1.getText();
String vVal = (String) textField2.getText();
String qVal = (String) textField3.getText();
String ppl = (String) ((EditText) TextField4).getText();

double n = Double.parseDouble(nVal);
double v = Double.parseDouble(vVal);
double q = Double.parseDouble(qVal);
double p = Double.parseDouble(ppl);

String myString = Double.toString(ppl);

editText4.setText(myString);
}});

11. does your class extend Activity?

12. Member
Join Date
Aug 2011
Posts
8
Rep Power
0
Sorry very new at this like I said. If u could tell me how that is done and I will be able to tell u if I have it or not

13. nevermind.

Make sure that you have that listener code inside of your onCreate method. So it should be inside these brackets

Java Code:
```public void onCreate(Bundle savedInstanceState) {

}```
You should have that onCreate method at the top of your class, just make sure that the listener code falls somewhere inside there, maybe right below where you have
Button MyButton1 = (Button) findViewById(R.id.button1);

Also, get rid of the @Override notation.

14. Member
Join Date
Aug 2011
Posts
8
Rep Power
0
ok i think i have done all the suggestions people have given but now i'm a bit stuck. it asking me to "Change modifier to Final" and when i do that it then gives me an error and asks me to change it back, then goes round in a loop. here's my full code so you can have a look:

Java Code:
```package com.ven.BulkBuy;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Cal extends Activity {

protected void onCreate(Bundle savedInstanceState, TextView textField1, TextView
textField2,	TextView textField3, View MyButton, final EditText TextField4, View textFeild4) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.cal);

textField1 = (TextView) findViewById(R.id.editText1);
textField2 = (TextView) findViewById(R.id.editText2);
textField3 = (TextView) findViewById(R.id.editText3);
textFeild4 = (TextView) findViewById(R.id.editText4);
Button MyButton1 = (Button) findViewById(R.id.button1);

MyButton1.setOnClickListener(new OnClickListener() {

public void onClick(View z) {
String nVal = (String) textField1.getText();
String vVal = (String) textField2.getText();
String qVal = (String) textField3.getText();
String ppl = (String)  textField4.getText();

double n = Double.parseDouble(nVal);
double v = Double.parseDouble(vVal);
double q = Double.parseDouble(qVal);
double p = Double.parseDouble(ppl);

String myString = Double.toString(ppl);

editText4.setText(myString);
}});

}
;

}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•