Results 1 to 14 of 14
  1. #1
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default 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. #2
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    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.

  3. #3
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    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!

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    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. #5
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    thanks sehudson

    i'll give it ago and hopefully it will work

  6. #6
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    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. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by venictus View Post
    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. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    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);
                           double answer = ((n*v)*q)/1000;
                          String myString = Double.toString(answer);
    
                          editText4.setText(myString);
    	                    }});
    Last edited by sehudson; 08-18-2011 at 01:36 AM.

  9. #9
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    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(){})

  10. #10
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    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);
    }});

  11. #11
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    does your class extend Activity?

  12. #12
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  13. #13
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    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. #14
    venictus is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  1. Replies: 8
    Last Post: 11-24-2009, 10:03 PM
  2. Great Demand For IT Professionals.
    By avularamesh4u in forum Jobs Discussion
    Replies: 2
    Last Post: 12-30-2008, 06:05 PM
  3. Hi, just found this great forum from Google.
    By paulachrist in forum Introductions
    Replies: 7
    Last Post: 07-27-2008, 10:47 AM
  4. A great doubt in Java Applet,will u solve it!!
    By anithababu in forum Java Applets
    Replies: 6
    Last Post: 01-27-2008, 02:20 PM
  5. great book for teenager to learn Java?
    By 21rouge in forum New To Java
    Replies: 8
    Last Post: 12-03-2007, 06:23 AM

Posting Permissions

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