Results 1 to 14 of 14
- 08-17-2011, 09:20 PM #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
- 08-17-2011, 09:35 PM #2
Member
- Join Date
- Aug 2011
- Posts
- 40
- Rep Power
- 0
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 #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?
thanks in advanced!
- 08-17-2011, 09:42 PM #4
Assuming you are getting the text from a textfield:
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:String nVal = textField1.getText(); String vVal = textField2.getText(); String qVal = textField3.getText();
Then, you would simply do the calculation:Java Code:double n = Double.parseDouble(nVal); double v = Double.parseDouble(vVal); double q = Double.parseDouble(qVal);
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:double answer = ((n*v)*q)/1000;
Then, set the text of your JTextField:Java Code:String myString = Double.toString(answer);
Java Code:myJTextField.setText(myString);
- 08-17-2011, 09:46 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
thanks sehudson
i'll give it ago and hopefully it will work
- 08-17-2011, 10:09 PM #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
-
- 08-18-2011, 12:31 AM #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:
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: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);
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); double answer = ((n*v)*q)/1000; String myString = Double.toString(answer); editText4.setText(myString); }});Last edited by sehudson; 08-18-2011 at 12:36 AM.
- 08-18-2011, 12:58 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);
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 #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);
double answer = ((n*v)*q)/1000;
double ppl = answer/TextField4;
String myString = Double.toString(ppl);
editText4.setText(myString);
}});
- 08-18-2011, 02:25 AM #11
does your class extend Activity?
- 08-18-2011, 02:36 AM #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
Thanks in advance
- 08-18-2011, 04:07 AM #13
nevermind.
Make sure that you have that listener code inside of your onCreate method. So it should be inside these brackets
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 haveJava Code:public void onCreate(Bundle savedInstanceState) { }
Button MyButton1 = (Button) findViewById(R.id.button1);
Also, get rid of the @Override notation.
- 08-19-2011, 12:23 AM #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); double answer = (((n*v)*q)/1000)/p; String myString = Double.toString(ppl); editText4.setText(myString); }}); } ; }
Similar Threads
-
Java Histogram program.. any adivce or hints wouls be great!!
By Chewart in forum New To JavaReplies: 8Last Post: 11-24-2009, 09:03 PM -
Great Demand For IT Professionals.
By avularamesh4u in forum Jobs DiscussionReplies: 2Last Post: 12-30-2008, 05:05 PM -
Hi, just found this great forum from Google.
By paulachrist in forum IntroductionsReplies: 7Last Post: 07-27-2008, 09:47 AM -
A great doubt in Java Applet,will u solve it!!
By anithababu in forum Java AppletsReplies: 6Last Post: 01-27-2008, 01:20 PM -
great book for teenager to learn Java?
By 21rouge in forum New To JavaReplies: 8Last Post: 12-03-2007, 05:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks