# Help with calculation - any help will be great!

Printable View

• 08-17-2011, 09:20 PM
venictus
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
• 08-17-2011, 09:35 PM
Willriker
google Java GUI, specifically JTextField, okbutton, and Jlabel

google set and get methods.

google eventHandler.

google try/catch.

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.
• 08-17-2011, 09:39 PM
venictus
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?

thanks in advanced!
• 08-17-2011, 09:42 PM
sehudson
Assuming you are getting the text from a textfield:

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.

Code:

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

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:

Code:

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

Code:

`myJTextField.setText(myString);`
• 08-17-2011, 09:46 PM
venictus
thanks sehudson

i'll give it ago and hopefully it will work :(clap):
• 08-17-2011, 10:09 PM
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 and the user is putting the data in to a number TextField and its label is editText1 ,2,3and 4
• 08-17-2011, 11:05 PM
Fubarable
Quote:

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.
• 08-18-2011, 12:31 AM
sehudson
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:
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:

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);                       double answer = ((n*v)*q)/1000;                       String myString = Double.toString(answer);                       editText4.setText(myString);                             }});```
• 08-18-2011, 12:58 AM
venictus
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);
double answer = ((n*v)*q)/1000;
String myString = Double.toString(answer);

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(){})
• 08-18-2011, 01:50 AM
venictus
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);

double answer = ((n*v)*q)/1000;
double ppl = answer/TextField4;

String myString = Double.toString(ppl);

editText4.setText(myString);
}});
• 08-18-2011, 02:25 AM
sehudson
does your class extend Activity?
• 08-18-2011, 02:36 AM
venictus
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

Thanks in advance
• 08-18-2011, 04:07 AM
sehudson
nevermind.

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

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.
• 08-19-2011, 12:23 AM
venictus
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:

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);                         double answer = (((n*v)*q)/1000)/p;                                     String myString = Double.toString(ppl);                   editText4.setText(myString);                         }});                                               }         ;                 }```