Why can I not make the numbers display on the text filed at a click of a button without writing a number first?

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.

Why can I not make the numbers display on the text filed at a click of a button without writing a number first?

this is the code used to make the calculator work, I have not fully finished it yet. I'm am new to Java programming (at beginners level).

//listen to action from the buttonsimportjava.awt.event.ActionListener;//implement event for buttonimportjava.awt.event.ActionEvent;importjavax.swing.JButton;publicclass CalcFunction implementsActionListener{/*reference to calculator using parent so the events for the button coded*/
Calculator parent;//used to store the math functionchar Selection=' ';//current result stored in the calculator is 0.doubleCurrent=0;//CalcFunction stores the reference to the to the Calculator class.
CalcFunction(Calculator parent){this.parent= parent;}
@Override //implement the event for the buttons and the text fieldpublicvoid actionPerformed(ActionEvent e){/*a gets the source of the button that was clicked in the calculator. Basically any button
* that was clicked, the ActionListener will check which button is clicked and it will store
* the clicked button's value. This will be stored in the new button created called
* clickedButton*/JButton clickedButton =(JButton) e.getSource();/*the texts that are displayed in the calculator text field will be stored in the variable
* named textfieldDisplay.*/String textfieldDisplayed = parent.inPutText.getText();//the value of the answers displayed will be displayed as 0.0 (in a a decimal value).double displayValue=0;if(!"".equals(textfieldDisplayed)){//
displayValue=Double.parseDouble(textfieldDisplayed);Object src = e.getSource();if(src == parent.btnPlus){
Selection ='+';Current=displayValue;
parent.inPutText.setText("");}elseif(src == parent.btnMinus){
Selection ='-';Current=displayValue;
parent.inPutText.setText("");}elseif(src == parent.btnDivide){
Selection ='/';Current=displayValue;
parent.inPutText.setText("");}elseif(src == parent.btnMultiply){
Selection ='*';Current=displayValue;
parent.inPutText.setText("");}if(Selection =='+'){
Selection='+';Current+=displayValue;
parent.inPutText.setText(""+Current);//end bracket for parent.btnPlus }elseif(Selection =='-'){
Selection='-';
Current-=displayValue;
parent.inPutText.setText(""+Current);//end bracket for parent.btnMinus }elseif(Selection =='*'){
Selection='*';Current*=displayValue;
parent.inPutText.setText(""+Current);//end bracket for parent.btnMultiply }elseif(Selection =='/'){
Selection='/';Current/=displayValue;
parent.inPutText.setText(""+Current);//end bracket for parent.btnDivide }else{// For all numeric buttons append the button's// label to the text fieldString ButtonLabel= clickedButton.getText();
parent.inPutText.setText(textfieldDisplayed + ButtonLabel);//end bracket for else.}}//end bracket for actionPerformed }}

below is the code used to set the calculator (how it looks like):

importjava.awt.*;//importjavax.swing.*;publicclass Calculator{//sets the frame which is the window of the calculator. Where the buttons and the contents are//presented. Basically its the background box. This frame is assigned the name "Calculator".JFrameFrame=newJFrame("Calculator");//sets the text box for the calculator. the maximum field size is 30.JTextField inPutText =newJTextField(30);//this JPanel is created to separately to add the buttons uniquely on its on section.//its almost like a graphical variable to store the buttons.JPanel buttonPanel =newJPanel();//number buttonsJButton btn1 =newJButton("1");JButton btn2 =newJButton("2");JButton btn3 =newJButton("3");JButton btn4 =newJButton("4");JButton btn5 =newJButton("5");JButton btn6 =newJButton("6");JButton btn7 =newJButton("7");JButton btn8 =newJButton("8");JButton btn9 =newJButton("9");JButton btn0 =newJButton("0");//math buttonsJButton btnPlus =newJButton("+");JButton btnMinus =newJButton("-");JButton btnMultiply =newJButton("*");JButton btnDivide =newJButton("/");//full stop and equal button.JButton btnEqual =newJButton("=");JButton btnBulletPoint =newJButton(".");public Calculator(){//sets a grid layout for the calculator 4 by 1.GridLayout buttonBorder =newGridLayout(4,1);//sets the frame visible to view the calculator itself.Frame.setVisible(true);//sets the frame size by 200 by 200.Frame.setSize(200,200);//sets the button panel visible to view the buttons.
buttonPanel.setVisible(true);//the button panel is assigned with grid layout.
buttonPanel.setLayout(buttonBorder);//added the buttons onto the Frame (Window)Frame.add(buttonPanel);//assigning the buttons to the Frame using the panel made for the buttons.
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btnPlus);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btnMinus);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btnMultiply);
buttonPanel.add(btn0);
buttonPanel.add(btn0);
buttonPanel.add(btnBulletPoint);
buttonPanel.add(btnEqual);
buttonPanel.add(btnDivide);//assigning the text box on the Frame itself at the top of the box.Frame.add(inPutText,BorderLayout.NORTH);
CalcFunction function=new CalcFunction(this);
btn1.addActionListener(function);
btn2.addActionListener(function);
btn3.addActionListener(function);
btn4.addActionListener(function);
btn5.addActionListener(function);
btn6.addActionListener(function);
btn7.addActionListener(function);
btn8.addActionListener(function);
btn9.addActionListener(function);
btn0.addActionListener(function);//public Calculator end bracket }publicstaticvoid main(String[] args){//
Calculator calc =new Calculator();//end of main }//end of class Calculator. }