Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: How to Add all the Button Values

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to Add all the Button Values

    		public void actionPerformed(ActionEvent ae)
    		{
     
     
    				float A=0;
     
    				int a = Integer.parseInt(tf1.getText());
    				if(ae.getSource()==b2) 
    				{
    					A=(((30*a*14) / 100)+(30*a));
    					System.out.println("Dosa"+"*"+a+" = "+(A));//Buton 1 
    				}
    				if(ae.getSource()==b3) 
    				{
    					A=(((20*a*14) / 100)+(30*a));
    					System.out.println("Idly"+"*"+a+" = "+(A));//Button 2 
     
    				}
    				if(ae.getSource()==b4) 
    				{
    					A=(((20*a*14) / 100)+(30*a));
    					System.out.println("Vada"+"*"+a+" = "+(A));//Button 3 
    				}
    				if(ae.getSource()==b5) 
    				{
    					A=(((30*a*14) / 100)+(30*a));
    					System.out.println("Poori"+"*"+a+" = "+(A));//Button4 
    				}
     
     
    				//I Want to add All this Buttons Please tell me which logic to be implemented
    				l3.setText(String.valueOf(A));
     
     
    			}
    Last edited by dinakar; March 19th, 2014 at 09:09 AM. Reason: Modified


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to Add all the Button Values

    When posting code, please use the highlight tags to preserve formatting.

    What have you tried? Where exactly are you stuck? What exactly are you confused about?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Add all the Button Values

    Quote Originally Posted by KevinWorkman View Post
    When posting code, please use the highlight tags to preserve formatting.

    What have you tried? Where exactly are you stuck? What exactly are you confused about?
    Dude as I Above Shown my code was for a simple hotel management
    I Have 4 diff Items and different prices and atlast I Want to add the Price of that all items.

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    /*
    <APPLET CODE="hotel1" HEIGHT =300 WIDTH=500 >
    </APPLET>
    */
    public class hotel1 extends Applet implements ActionListener
    {
    Button b2,b3,b4,b5;
    TextField tf1;
    Color clr;
    Label l1,l2,l3;
    public hotel1()
    {

    clr=new Color(219,240,251);
    setBackground(clr);
    setLayout(null);
    tf1=new TextField(5);
    l1=new Label("Enter Quanity");
    l1.setBounds(5,10,130,30);
    l2=new Label();
    l2.setBounds(5,10,130,30);
    l3=new Label("Amount");
    l3.setBounds(100,70,180,30);
    tf1.setBounds(150,10,130,20);


    b2=new Button("Dosa");
    b2.setBounds(80,180,70,35);
    b3=new Button("Idly");
    b3.setBounds(20,130,80,30);
    b4=new Button("Vada");
    b4.setBounds(110,130,80,30);
    b5=new Button("Poori");
    b5.setBounds(200,130,80,30);



    add(l1);
    add(tf1);
    add(b2);
    add(b3);
    add(b4);
    add(b5);

    add(l2);
    add(l3);
    l2.setFont(new Font("",Font.BOLD,14));
    l2.setForeground(Color.RED);

    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    }


    public void actionPerformed(ActionEvent ae)
    {


    float A=0;

    int a = Integer.parseInt(tf1.getText());
    if(ae.getSource()==b2)
    {
    A(30*a*14) / 100)+(30*a));
    System.out.println("Dosa"+"*"+a+" = "+(A));//Buton 1
    }
    if(ae.getSource()==b3)
    {
    A(20*a*14) / 100)+(30*a));
    System.out.println("Idly"+"*"+a+" = "+(A));//Button 2

    }
    if(ae.getSource()==b4)
    {
    A(20*a*14) / 100)+(30*a));
    System.out.println("Vada"+"*"+a+" = "+(A));//Button 3
    }
    if(ae.getSource()==b5)
    {
    A(30*a*14) / 100)+(30*a));
    System.out.println("Poori"+"*"+a+" = "+(A));//Button4
    }



    l3.setText(String.valueOf(A));


    }


    public static void main(String[] args)
    {
    new hotel1();
    }



    }

    this was my Complete code

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to Add all the Button Values

    Again, when posting code, please use the highlight tags to preserve formatting. The code you've posted is pretty much unreadable.

    And the reason I'm asking what you're confused about is you've told us your requirement, but not what's preventing you from achieving that requirement. What are you confused about?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Add all the Button Values

    Hmm due i donno nothing about Highlighting the code and formatting I'm New in this field
    I Just want to know logic to add A1,A2,A3,A4.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to Add all the Button Values

    Quote Originally Posted by dinakar View Post
    Hmm due i donno nothing about Highlighting the code and formatting I'm New in this field
    Suggested reading: Announcements - What's Wrong With My Code?

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Add all the Button Values

    Quote Originally Posted by dinakar View Post
    Hmm due i donno nothing about Highlighting the code and formatting I'm New in this field
    I Just want to know logic to add A1,A2,A3,A4.
    		public void actionPerformed(ActionEvent ae)
    		{
     
     
    				float A=0;
     
    				int a = Integer.parseInt(tf1.getText());
    				if(ae.getSource()==b2) 
    				{
    					A=(((30*a*14) / 100)+(30*a));
    					System.out.println("Dosa"+"*"+a+" = "+(A));//Buton 1 
    				}
    				if(ae.getSource()==b3) 
    				{
    					A=(((20*a*14) / 100)+(30*a));
    					System.out.println("Idly"+"*"+a+" = "+(A));//Button 2 
     
    				}
    				if(ae.getSource()==b4) 
    				{
    					A=(((20*a*14) / 100)+(30*a));
    					System.out.println("Vada"+"*"+a+" = "+(A));//Button 3
    				}
    				if(ae.getSource()==b5) 
    				{
    					A=(((30*a*14) / 100)+(30*a));
    					System.out.println("Poori"+"*"+a+" = "+(A));//Button4 
    				}
     
     
    				//I Want to add All this Buttons Please tell me which logic to be implemented
    				l3.setText(String.valueOf(A));
     
     
    			}

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to Add all the Button Values

    Please do not post multiple copies of the same question. I've deleted your duplicate post.

    You aren't making it easy for us to help you- I'm still not sure exactly what you're asking, and you haven't posted an SSCCE or an MCVE demonstrating where you're stuck. You've told us you need to add all of the buttons, but you haven't specified what's stopping you from doing so. Are you asking how to add integer values? How to add buttons to a window? Something else?

    I recommend reading the link in my signature on asking questions the smart way and posting a more specific question (in this thread, not in a new post).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Add all the Button Values

    Quote Originally Posted by KevinWorkman View Post
    Please do not post multiple copies of the same question. I've deleted your duplicate post.

    You aren't making it easy for us to help you- I'm still not sure exactly what you're asking, and you haven't posted an SSCCE or an MCVE demonstrating where you're stuck. You've told us you need to add all of the buttons, but you haven't specified what's stopping you from doing so. Are you asking how to add integer values? How to add buttons to a window? Something else?

    I recommend reading the link in my signature on asking questions the smart way and posting a more specific question (in this thread, not in a new post).
    Dude I Cannot able to understand Y u r not getting me No I'm Not Asking How to add buttons to Window
    Iam Asking How to add that Button Values for example my first button value is 30 and 2nd button value is 40
    Now I Want logic to add this bothe values and I Should get value 70

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to Add all the Button Values

    Okay, and what have you tried?

    What is a button's value? Are you talking about the text displayed on the button? Its label? Some other associated value? I am "unable to understand Y u r not getting me" because you haven't asked a specific technical question. "Adding buttons" could mean any number of things.

    Again, I recommend you read the link in my signature on asking questions the smart way before you post again.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to Add all the Button Values

    I might be missing something here, but what value does "a" have?

    --- Update ---

    Oh nvm, I just saw it *facepalm*

    The way you have it, it doesn't look like you can add it

    Unless... Con you read one action at a time and have that stored in a variable and then add both variables? Will a be different when you add the button values?

    Also, what is this "+(A))" thing about?

    Hope that makes sense
    Last edited by Tea.EarlGrey.Hot.; March 19th, 2014 at 03:02 PM.

  12. #12
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I Want to add A1,A2,A3,A4 values

    Let us Suppose I get Values for A=10= A2=30 A3=40=A4=50 and I wanna get Output as Total = 10+30+40+50==130
    Please Suggest me Code for this to happen
    In this Logic a=No Of Quantity


    		public void actionPerformed(ActionEvent ae)
    		{
     
     
    				float A=0,A1=0,A2=0,A3=0,A4=0,Totall=0;
     
    				int a = Integer.parseInt(tf1.getText());
    				if(ae.getSource()==b2) 
    				{
    					A1=(((30*a*14) / 100)+(30*a));
    					System.out.println("Dosa"+"*"+a+" = "+(A1));//Buton 1 
    				}
    				if(ae.getSource()==b3) 
    				{
    					A2=(((20*a*14) / 100)+(30*a));
    					System.out.println("Idly"+"*"+a+" = "+(A2));//Button 2 
     
    				}
    				if(ae.getSource()==b4) 
    				{
    					A3=(((20*a*14) / 100)+(30*a));
    					System.out.println("Vada"+"*"+a+" = "+(A3));//Button 3 
    				}
    				if(ae.getSource()==b5) 
    				{
    					A4=(((30*a*14) / 100)+(30*a));
    					System.out.println("Poori"+"*"+a+" = "+(A4));//Button4 
    				}
    				if(ae.getSource()==b1&&ae.getSource()==b2||ae.getSource()==b3||ae.getSource()==b4||ae.getSource()==b5)
    				{
    					Totall=A1+A2+A3+A4;
    					System.out.println("Total="+Totall);
    				}

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to Add all the Button Values

    Like I said, please don't post multiple copies of the same question. I've merged your threads.

    I still don't understand what you're asking. If you have values A1 through A10 and you want to set the value of total to their sum, you simply do this:

    double total = A1+A2+A3+A4+A5+A6+A7+A8+A9+A10;

    If that's not what you're asking, you have to be more specific. It would be better if you posted an SSCCE or MCVE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Add all the Button Values

    already i Tried total = A1+A2+A3+A4; but its not working I'm Getting value of Total==0
    Please Advice me any other Logic

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to Add all the Button Values

    Like I said, it's pretty hard to help you without a small example. In this case, it can just be a main method that contains your 4 A variables hardcoded to a certain value, as well as the total variable and a print statement. Here, I'll start:

    public class Test{
       public static void main(String... args){
          float a1 = 10;
          float a2 = 20;
          float a3 = 30;
          float a4 = 40;
     
          float total = a1+a2+a3+a4;
          System.out.println(total);
       }
    }

    Modify that program so it reflects the problem you're having.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Add all the Button Values

    I Cannot Initialize the Values I Should Get Value Depending on the value of SMALL a and based on the Logic (((30*a*14) / 100)+(30*a));

  17. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to Add all the Button Values

    Then put that logic into the program I posted. The idea is to show us exactly what your code is doing, without all of the extra GUI code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Cannot add values
    By tonynsx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 16th, 2013, 10:06 PM
  2. add values in JTable
    By viper_pranish in forum AWT / Java Swing
    Replies: 3
    Last Post: July 27th, 2013, 08:16 AM
  3. add table to button
    By DeniLeet in forum Java Theory & Questions
    Replies: 6
    Last Post: June 4th, 2013, 10:22 AM
  4. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  5. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM