Results 1 to 3 of 3
  1. #1
    mattyd27 is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default "Newbie needs help with code" Calculator

    hi im new to coding and am about to take up a course just need some help on this calculator i have spent the last week trying to find out what is wrong with my code but to no ends i keep getting the illegal start of expression at this line "/ public static void main(String args[]){" any help would be appreciated greatly
    P.s.
    the code for the calculator works fine just the public static area thanks again
    mattyd27

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package calculator3;
    /**
    *
    * @author user
    */
    public class calculator1 extends javax.swing.JFrame {
    /**
    * Creates new form calculator1
    */
    public calculator1() {
    }
    public class Calculator1 extends javax.swing.JFrame {
    double plusMinus;
    double firstDouble;
    double secondDouble;
    double totalDouble;
    int plusClick;
    int minusClick;
    /**
    * Creates new form Calculator1
    */
    public Calculator1() {
    initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    jBtn1 = new javax.swing.JButton();
    jBtn2 = new javax.swing.JButton();
    jBtn3 = new javax.swing.JButton();
    jBtn4 = new javax.swing.JButton();
    jBtn5 = new javax.swing.JButton();
    jBtn6 = new javax.swing.JButton();
    jBtn7 = new javax.swing.JButton();
    jBtn8 = new javax.swing.JButton();
    jBtn9 = new javax.swing.JButton();
    jBtnClear = new javax.swing.JButton();
    jBtnPlus = new javax.swing.JButton();
    jBtnMinus = new javax.swing.JButton();
    jBtn0 = new javax.swing.JButton();
    jBtnEquals = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jTextField1.setEditable(false);
    jTextField1.setText("0");

    jBtn1.setText("1");
    jBtn1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn1ActionPerformed(evt);
    }
    });

    jBtn2.setText("2");
    jBtn2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn2ActionPerformed(evt);
    }
    });

    jBtn3.setText("3");
    jBtn3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn3ActionPerformed(evt);
    }
    });

    jBtn4.setText("4");
    jBtn4.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn4ActionPerformed(evt);
    }
    });

    jBtn5.setText("5");
    jBtn5.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn5ActionPerformed(evt);
    }
    });

    jBtn6.setText("6");
    jBtn6.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn6ActionPerformed(evt);
    }
    });

    jBtn7.setText("7");
    jBtn7.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn7ActionPerformed(evt);
    }
    });

    jBtn8.setText("8");
    jBtn8.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn8ActionPerformed(evt);
    }
    });

    jBtn9.setText("9");
    jBtn9.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn9ActionPerformed(evt);
    }
    });

    jBtnClear.setText("c");
    jBtnClear.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtnClearActionPerformed(evt);
    }
    });

    jBtnPlus.setText("+");
    jBtnPlus.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtnPlusActionPerformed(evt);
    }
    });

    jBtnMinus.setText("-");
    jBtnMinus.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtnMinusActionPerformed(evt);
    }
    });

    jBtn0.setText("0");
    jBtn0.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtn0ActionPerformed(evt);
    }
    });

    jBtnEquals.setText("=");
    jBtnEquals.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jBtnEqualsActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(20, 20, 20)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jBtn4)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jBtn5)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jBtn6)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jBtnPlus))
    .addGroup(layout.createSequentialGroup()
    .addComponent(jBtn1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jBtn2)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jBtn3)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jBtnClear))
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addComponent(jBtn0)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jBtn7)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jBtn8)))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jBtn9)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jBtnMinus)
    .addComponent(jBtnEquals))))
    .addContainerGap(79, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(39, 39, 39)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jBtn1)
    .addComponent(jBtn2)
    .addComponent(jBtn3)
    .addComponent(jBtnClear))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jBtn4)
    .addComponent(jBtn5)
    .addComponent(jBtn6)
    .addComponent(jBtnPlus))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jBtn7)
    .addComponent(jBtn8)
    .addComponent(jBtn9)
    .addComponent(jBtnMinus))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jBtn0)
    .addComponent(jBtnEquals))
    .addContainerGap(113, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>
    private void jBtn7ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn7.ge tText()); }
    private void jBtn1ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn1.ge tText());
    }
    private void jBtn2ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn2.ge tText());
    }
    private void jBtn3ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn3.ge tText());
    }
    private void jBtn4ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn4.ge tText());
    }
    private void jBtn5ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn5.ge tText());
    }
    private void jBtn6ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn6.ge tText());
    }
    private void jBtn8ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn8.ge tText());
    }
    private void jBtn9ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn9.ge tText()); }
    private void jBtn0ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText(jTextField1.getText()+jBtn0.ge tText());
    }
    private void jBtnClearActionPerformed(java.awt.event.ActionEven t evt) {
    jTextField1.setText("");
    }
    private void jBtnMinusActionPerformed(java.awt.event.ActionEven t evt) {
    firstDouble=(Double.parseDouble(String.valueOf(jTe xtField1.getText())));
    jTextField1.setText("");
    minusClick=1;
    }
    private void jBtnPlusActionPerformed(java.awt.event.ActionEvent evt) {
    firstDouble=(Double.parseDouble(String.valueOf(jTe xtField1.getText())));
    jTextField1.setText("");
    plusClick=1; }
    private void jBtnEqualsActionPerformed(java.awt.event.ActionEve nt evt) {
    secondDouble=Double.parseDouble(String.valueOf(jTe xtField1.getText()));
    if (plusClick>0){
    totalDouble=firstDouble+secondDouble;
    jTextField1.setText((String.valueOf(totalDouble))) ;
    firstDouble=0;
    secondDouble=0;
    plusClick=0;
    if (minusClick>0){
    totalDouble=firstDouble-secondDouble;
    firstDouble=0;
    secondDouble=0;
    minusClick=0;
    }
    /**
    * @param args the command line arguments
    */{
    }
    public static void main(String args[]){
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(calculator1.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(calculator1.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(calculator1.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(calculator1.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new calculator1().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jBtn0;
    private javax.swing.JButton jBtn1;
    private javax.swing.JButton jBtn2;
    private javax.swing.JButton jBtn3;
    private javax.swing.JButton jBtn4;
    private javax.swing.JButton jBtn5;
    private javax.swing.JButton jBtn6;
    private javax.swing.JButton jBtn7;
    private javax.swing.JButton jBtn8;
    private javax.swing.JButton jBtn9;
    private javax.swing.JButton jBtnClear;
    private javax.swing.JButton jBtnEquals;
    private javax.swing.JButton jBtnMinus;
    private javax.swing.JButton jBtnPlus;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: "Newbie needs help with code" Calculator

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: "Newbie needs help with code" Calculator

    It looks to me like you are brute forcing your code. I'm sure it will eventually work, but as you say, you have
    spent the last week trying to figure out what is wrong.
    I suggest you take the time to read thru the Java Tutorials (see my signature) and learn about
    actionListeners, ActionEvents (specifically the getSource() method), and inner classes, you could cut down
    on your code by more than 50%. In fact, from what I see, you don't even need to retain your button instances.

    Staying away from GUI designers and focusing on layout managers would also be advisable as you will learn a lot more.
    And finally, unless you enjoy typing, I suggest you learn about the import keyword.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 5
    Last Post: 04-29-2012, 07:18 AM
  3. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM
  4. Newbie question about basic code - "Hello World"
    By Kimomaru in forum New To Java
    Replies: 3
    Last Post: 10-13-2011, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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