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 2 of 2

Thread: Confused about string that I think shouldn't be null

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Confused about string that I think shouldn't be null

    I am trying to write a basic four-function calculator. Here is the code:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    class SingleObjectCalculator extends JFrame
    {
         JTextField screen = new JTextField("0",15);
         JButton one;
         JButton two;
         JButton three;
         JButton four;
         JButton five;
         JButton six;
         JButton seven;
         JButton eight;
         JButton nine;
         JButton zero;
         JButton point;
         JButton plus;
         JButton minus;
         JButton times;
         JButton divide;
         JButton equals;
         JButton clear;
         Stack<String> opndStck = new Stack<String>();
         Stack<String> optrStck = new Stack<String>();
     
         class ButtonListener implements ActionListener
         {
              boolean ptPshd = false;
              boolean optrPshd = false;
              double fOpnd = 0;
              double sOpnd = 0;
              double res = 0;
              String exprStr = "";
              String optrStr = "";
              String curOpndStr = "";
              String prvOpndStr = "";
              String curOptrStr = "";
              String prvOptrStr = "";
              String prvPrvOptrStr = "";
     
              public void actionPerformed (ActionEvent ae)
              {
                   String butStr = ae.getActionCommand();
     
                   if (butStr.equals("0"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             exprStr = butStr;
                        }
                        optrPshd = false;
                   }
                   else if (butStr.equals("1"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("2"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("3"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                  }
                  else if (butStr.equals("4"))
                  {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("5"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("6"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("7"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("8"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("9"))
                   {
                        if (!optrPshd)
                        {
                             if (exprStr.equals("0"))
                             {
                                 exprStr = butStr;
                                 screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                        }
                        else
                        {
                             if (exprStr.length() != 0)
                             {
                                  exprStr = butStr;
                                  screen.setText(exprStr);
                             }
                             else
                             {
                                  exprStr += butStr;
                                  screen.setText(exprStr);
                             }
                             optrPshd = false;
                        }
                   }
                   else if (butStr.equals("."))
                   {
     
                        if (!ptPshd)
                        {
                             if (!optrPshd)
                             {
                                  if (exprStr.equals("0"))
                                  {
                                      exprStr += butStr;
                                      screen.setText(exprStr);
                                  }
                                  else
                                  {
                                       exprStr += butStr;
                                       screen.setText(exprStr);
                                  }
                             }
                             else
                             {
                                  if (exprStr.length() != 0)
                                  {
                                      exprStr = ("0" + butStr);
                                      screen.setText(exprStr);
                                  }
                                  else
                                  {
                                       exprStr += butStr;
                                       screen.setText(exprStr);
                                  }
                                  optrPshd = false;
                             }
                        }
                        ptPshd = false;                   
                   }
                   else if (butStr.equals("+"))
                   {
                        optrStr = butStr;
                        ptPshd = false;
     
                        if (!optrPshd)
                        {
                             if (opndStck.empty() && optrStck.empty())
                             {
                                  opndStck.push(screen.getText());
                                  optrStck.push(optrStr);
                             }
                             else if (!opndStck.empty() && !optrStck.empty())
                             {
                                  curOpndStr = screen.getText();
                                  curOptrStr = optrStr;                    
                             }
                             optrPshd = true;
                        }
                        else
                        {
                             optrStck.pop();
                             optrStck.push(optrStr);
                        }
     
                        if (opndStck.size() > 0 && optrStck.size() > 0)
                        {
                                  prvOpndStr = opndStck.pop();
                                  prvOptrStr = optrStck.pop();
     
                                  sOpnd = Double.parseDouble(curOpndStr);
                                  fOpnd = Double.parseDouble(prvOpndStr);
     
                                  System.out.println(sOpnd);
                                  System.out.println(fOpnd);
     
                             while (opndStck.size() > 1 && optrStck.size() > 1)
                             {
     
                                  if (opndStck.size() == 1 && optrStck.size() == 1)
                                  {
                                       if (prvOptrStr.equals("+"))
                                       {
                                           res = fOpnd + sOpnd;
                                           System.out.println(res);
                                       }                           
                                       else if (prvOptrStr.equals("-"))
                                       {
                                            res = fOpnd - sOpnd;
                                            System.out.println(res);
                                       }                           
                                  }
                                  else
                                  {
                                       prvOpndStr = opndStck.pop();
                                       prvOptrStr = optrStck.pop();
                                       prvPrvOptrStr = optrStck.pop();
     
                                       sOpnd = Double.parseDouble(curOpndStr);
                                       fOpnd = Double.parseDouble(prvOpndStr);
     
                                       if (prvOptrStr.equals("*"))
                                       {
                                           res = fOpnd * sOpnd;
                                           System.out.println(res);
                                       }                           
                                       else if (prvOptrStr.equals("/"))
                                       {
                                            res = fOpnd / sOpnd;
                                            System.out.println(res);
                                       }
                                  }
     
                                  opndStck.push(res + "");
                             }
                             screen.setText(res + "");
                        }
                        else if (opndStck.size() <= 1 && optrStck.size() == 1)
                        {
                             prvOpndStr = opndStck.pop();
                             prvOptrStr = optrStck.pop();
                        }
                   }
                   else if (butStr.equals("-"))
                   {
                        optrStr = butStr;
                        ptPshd = false;
     
                        if (!optrPshd)
                        {
                             if (opndStck.empty() && optrStck.empty())
                             {
                             opndStck.push(screen.getText());
                            optrStck.push(optrStr);
                             }
                             else
                             {
                                  curOpndStr = screen.getText();
                                  curOptrStr = optrStr;                    
                             }
                             optrPshd = true;
                        }
                        else
                        {
                             optrStck.pop();
                             optrStck.push(optrStr);
                        }
     
                   }
                   else if (butStr.equals("*"))
                   {
                        optrStr = butStr;
                        ptPshd = false;
     
                        if (!optrPshd)
                        {
                             if (opndStck.empty() && optrStck.empty())
                             {
                                  opndStck.push(screen.getText());
                                  optrStck.push(optrStr);
                             }
                             else
                             {
                                  curOpndStr = screen.getText();
                                  curOptrStr = optrStr;                    
                             }
                             optrPshd = true;
                        }
                        else
                        {
                             optrStck.pop();
                             optrStck.push(optrStr);
                        }
                   }
                   else if (butStr.equals("/"))
                   {
                        optrStr = butStr;
                        ptPshd = false;
     
                        if (!optrPshd)
                        {
                             if (opndStck.empty() && optrStck.empty())
                             {
                             opndStck.push(screen.getText());
                             optrStck.push(optrStr);
                             }
                             else
                             {
                                  curOpndStr = screen.getText();
                                  curOptrStr = optrStr;                    
                             }
                             optrPshd = true;
                        }
                        else
                        {
                             optrStck.pop();
                             optrStck.push(optrStr);
                        }
     
                        for (String s : opndStck)
                        {
                            System.out.print(s);
                        }
                        System.out.println();
                        for (String s : optrStck)
                        {
                            System.out.print(s);
                        }
                        System.out.println();
                        System.out.println();
                   }
                   else if (butStr.equals("="))
                   {
                        optrStr = butStr;
                        ptPshd = false;
                        opndStck.push(screen.getText());
                        optrStck.push(optrStr);
                   }
                   else if (butStr.equals("C"))
                   {
                        screen.setText("0");
                        while (!opndStck.empty())
                        {
                             opndStck.pop();
                        }
                        while (!optrStck.empty())
                        {
                             optrStck.pop();
                        }
     
                        ptPshd = false;
                        optrPshd = false;
                        fOpnd = 0;
                        sOpnd = 0;
                        res = 0;
                        exprStr = "";
                        optrStr = "";
                        curOpndStr = "";
                        prvOpndStr = "";
                        prvPrvOptrStr = "";
                        curOptrStr = "";
                        prvOptrStr = "";
                   }
              }
         }    
     
         ActionListener bl = new ButtonListener();
     
         SingleObjectCalculator()
         {
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
              setSize(250,250);
              setVisible(true);
     
              setLayout(new GridBagLayout());
     
              GridBagConstraints gbc = new GridBagConstraints();
              Insets bi = new Insets(15, 15, 15, 15);
     
              gbc.gridwidth = 4;
              gbc.gridx = 0;
              gbc.gridy = 0;
              add(screen, gbc);
     
              gbc.gridwidth = 1;
     
              seven = new JButton("7");
              gbc.ipady = 5;
              gbc.gridx = 0;
              gbc.gridy = 1;
              add(seven, gbc);
     
              eight = new JButton("8");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 1;
              gbc.gridy = 1;          
              add(eight, gbc);
     
              nine = new JButton("9");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 2;
              gbc.gridy = 1;
              add(nine, gbc);
     
              plus = new JButton("+");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 3;
              gbc.gridy = 1;
              add(plus, gbc);
     
              four = new JButton("4");
              gbc.weighty = 0;
              gbc.gridx = 0;
              gbc.gridy = 2;
              add(four, gbc);
     
              five = new JButton("5");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 1;
              gbc.gridy = 2;          
              add(five, gbc);
     
              six = new JButton("6");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 2;
              gbc.gridy = 2;
              add(six, gbc);
     
              minus = new JButton("-");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 3;
              gbc.gridy = 2;
              add(minus, gbc);
     
              one = new JButton("1");
     
              gbc.weighty = 0;
              gbc.gridx = 0;
              gbc.gridy = 3;
              add(one, gbc);
     
              two = new JButton("2");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 1;
              gbc.gridy = 3;          
              add(two, gbc);
     
              three = new JButton("3");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 2;
              gbc.gridy = 3;
              add(three, gbc);
     
              times = new JButton("*");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 3;
              gbc.gridy = 3;
              add(times, gbc);
     
              point = new JButton(".");
     
              gbc.weighty = 0;
              gbc.gridx = 0;
              gbc.gridy = 4;
              add(point, gbc);
     
              zero = new JButton("0");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 1;
              gbc.gridy = 4;          
              add(zero, gbc);
     
              equals = new JButton("=");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 2;
              gbc.gridy = 4;
              add(equals, gbc);
     
              divide = new JButton("/");
              //gbc.fill = GridBagConstraints.RELATIVE;
              gbc.gridx = 3;
              gbc.gridy = 4;
              add(divide, gbc);
     
              clear = new JButton("C");
              gbc.gridwidth = 4;
              gbc.gridx = 4;
              gbc.gridy = 0;
              add(clear, gbc);
     
              one.addActionListener(bl);
              two.addActionListener(bl);
              three.addActionListener(bl);
              four.addActionListener(bl);
              five.addActionListener(bl);
              six.addActionListener(bl);
              seven.addActionListener(bl);
              eight.addActionListener(bl);
              nine.addActionListener(bl);
              zero.addActionListener(bl);
              point.addActionListener(bl);
              equals.addActionListener(bl);
              plus.addActionListener(bl);
              minus.addActionListener(bl);
              times.addActionListener(bl);
              divide.addActionListener(bl);
              clear.addActionListener(bl);
         }
     
         public static void main (String args[])
         {
              SingleObjectCalculator soc = new SingleObjectCalculator();
         }
    }

    Currently it throws a NumberFormatExcpetion at line 400:

                                       sOpnd = Double.parseDouble(curOpndStr);

    The JVM claims that it is throwing the exception because the string curOpndStr is empty (as it is when I initialize it at the beginning of actionPerformed()). I am able to trace the error back to a block of code where I think that I reassign curOpndStr a non-empty value.
                             if (opndStck.empty() && optrStck.empty())
                             {
                                  opndStck.push(screen.getText());
                                  optrStck.push(optrStr);
                             }
                             else if (!opndStck.empty() && !optrStck.empty())
                             {
                                  curOpndStr = screen.getText();
                                  curOptrStr = optrStr;                    
                             }

    I have test that block of code and know that, when the "+" button is pushed for the first time, opndStck.empty() && optrStck.empty() becomes false, I am confused as to why, when I test the code after it curOpndStr is empty.

    Is there some way that I am inadvertantly resetting the value?

    I apologize for posting such a long code, but I thought it might help.


  2. #2
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Confused about string that I think shouldn't be null

    Never mind. I figured it out.

    It was silly mistake.

Similar Threads

  1. I'm confused...
    By acolar in forum Object Oriented Programming
    Replies: 1
    Last Post: April 14th, 2011, 12:14 PM
  2. Confused about my Career
    By DanBrown in forum The Cafe
    Replies: 10
    Last Post: February 9th, 2011, 07:47 AM
  3. Confused with Arrays
    By Solidius in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 09:54 AM
  4. Confused about setting up JSP
    By mjpam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: September 17th, 2010, 05:14 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM