Can anybody help me how to transfer the inputting of my numbers from left to right...

Well here is my program..

import java.awt.*;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator2 extends JPanel implements ActionListener
{
     private JTextField display = new JTextField("0");

     private String buttonText = "789/456*123-0.=+";

     private double result = 0;

     private String operator = "=";

     private boolean calculating = true;

     public Calculator2()
     {
          setLayout(new BorderLayout());

          display.setEditable(false);
          add(display, "North");

          JPanel p = new JPanel();
          p.setLayout(new GridLayout(4, 4));

          for (int i = 0; i < buttonText.length(); i++) {
          JButton b = new JButton(buttonText.substring(i, i + 1));
          p.add(b);
          b.addActionListener(this);

     }
     add(p, "Center");
 }

 public void actionPerformed(ActionEvent e)
 {
      String cmd = e.getActionCommand();
      if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals(".")) {
      if (calculating)
        display.setText(cmd);
      else
        display.setText(display.getText() + cmd);
       calculating = false;
    }
    else
    {
        if (calculating)
        {
           if (cmd.equals("-"))
           {
               display.setText(cmd);
               calculating = false;
           }
           else
           operator = cmd;
       }
       else
       {
          double x = Double.parseDouble(display.getText());
          calculate(x);
          operator = cmd;
          calculating = true;
       }
    }
  }

  private void calculate(double n)
  {
     if (operator.equals("+"))
      result += n;
    else if (operator.equals("-"))
      result -= n;
    else if (operator.equals("*"))
      result *= n;
    else if (operator.equals("/"))
      result /= n;
    else if (operator.equals("="))
      result = n;
    display.setText("" + result);
   }

   public static void main(String[] args)
   {
       JFrame frame = new JFrame();
       frame.setTitle("Calculator");
       frame.setSize(200, 200);
       frame.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
       System.exit(0);
   }
});

    Container contentPane = frame.getContentPane();
    contentPane.add(new Calculator2());
    frame.show();
  }
}

thanks...:)

just add following line display.setHorizontalAlignment(JTextField.RIGHT); above the statement display.setEditable(false); Thanks

just add following line display.setHorizontalAlignment(JTextField.RIGHT); above the statement display.setEditable(false); Thanks

...Thanks a lot....:)

import java.util.Scanner;
import java.io.IOException;

public class calculator

{
public static void main(String[] args) throws IOException
{
    char opt,ch;
    int firstnumber,secondnumber,answer;




        System.out.println (("Welcome to the calculator!");
        System.out.println ("To multiply use m, to divide use d, to add use a, to subtract use s");

        Scanner keyboard = new Scanner(System.in);

        do
        {
            System.out.println ("Do you want to multiply, divide, add, or subtract? ");
            opt=(char)System.in.read();

            System.out.println("Enter any two values");
            firstnumber = keyboard.nextInt();
            secondnumber = keyboard.nextInt();


            switch(opt)
            {
                case 'm' :
                        answer=firstnumber*secondnumber;
                        System.out.println("Multiplication : "+answer);
                        break;
                case 'd' :
                        answer=firstnumber/secondnumber;
                        System.out.println("Division       : "+answer);
                        break;
                case 'a' :
                        answer=firstnumber+secondnumber;
                        System.out.println("Addition       : "+answer);
                        break;
                case 's' :
                        answer=firstnumber-secondnumber;
                        System.out.println("Subtraction    : "+answer);
                        break;
                default  :  System.out.println("Invalid Option..");
            }
            System.out.println("Do u want to continue(y/n) : ");
            ch=(char)System.in.read();

        }while(ch != 'n');

}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.