4 Replies Latest reply: Oct 25, 2012 9:43 AM by 968909 RSS

    Applet, input string value, convert to int, increase by one....

    968909
      We have to write a program that will take a string of digits, ex 875, into a JTextField. When you hit enter it will display in the Area incremented by one,
      I can get it to display the value but I cant figure out how to add the value, as an integer and have it increment by one.

      Any help would be appreciated.
      Please and thank you ~

      Here is what I have:

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class CreateAnApplet extends JApplet implements ActionListener {
           
           JTextField boxRectangle;
           JTextArea boxLocation;
           JLabel heading;
           int increase;
           int str;
           
           public void init () {
                
                boxRectangle = new JTextField (10);
                getContentPane().add(boxRectangle);
                boxRectangle.addActionListener(this);
                
                heading = new JLabel("Enter something herer");
                getContentPane().add(heading);
                
                getContentPane().setLayout(new FlowLayout());
                
                boxLocation = new JTextArea(10,20);
                getContentPane().add(boxLocation);
                
           }

           public void actionPerformed(ActionEvent e){
                str = Integer.parseInt(boxRectangle.getText());
                     
                String str = boxRectangle.getText();
                boxLocation.setText(str);
                
                ++increase;
                *boxLocation.setText("Your new number is now: " increase str);*
           }
      }

      Edited by: SunshineJava on Oct 24, 2012 4:45 PM
        • 1. Re: Applet, input string value, convert to int, increase by one....
          ilkinesrefli
          Hello, is it ok?
          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;
          
          public class CreateAnApplet extends JApplet implements ActionListener {
          
              JTextField boxRectangle;
              JTextArea boxLocation;
              JLabel heading;
          
              @Override
              public void init() {
                  boxRectangle = new JTextField(10);
                  getContentPane().add(boxRectangle);
                  boxRectangle.addActionListener(this);
                  heading = new JLabel("Enter something here");
                  getContentPane().add(heading);
                  getContentPane().setLayout(new FlowLayout());
                  boxLocation = new JTextArea(10, 20);
                  getContentPane().add(boxLocation);
              }
          
              @Override
              public void actionPerformed(ActionEvent e) {
                  try {
                      int rectangle = Integer.parseInt(boxRectangle.getText());
                      boxLocation.setText("Your new number is now: " + (rectangle + 1));
                  } catch (NumberFormatException ex) {
                      boxLocation.setText("Please input an integer number..");
                  }
              }
          }
          • 2. Re: Applet, input string value, convert to int, increase by one....
            rp0428
            >
            I can get it to display the value but I cant figure out how to add the value, as an integer and have it increment by one.
            >
            Well first you need to set it to something.

            You declare variables named 'increase' and 'str' (bad name for an int)
            int increase;
            int str;
            Then you populate 'str' instead of 'increase'
            public void actionPerformed(ActionEvent e){
            str = Integer.parseInt(boxRectangle.getText());
            Then you define a new local variable name 'str' which is a String
            String str = boxRectangle.getText();
            boxLocation.setText(str);
            Then you try to increment 'increase' which was never assigned a value
            ++increase;
            Straighten out this mess and populate 'increase' abvoe using the Integer.parseInt
            • 3. Re: Applet, input string value, convert to int, increase by one....
              968909
              Thank you - that is a mess. I will try your suggestions and reply :-)
              • 4. Re: Applet, input string value, convert to int, increase by one....
                968909
                Amazing when I cleaned up the mess. Thank you - excellent direction. :-)

                Final product:

                import javax.swing.*;
                import java.awt.*;
                import java.awt.event.*;

                public class CreateAnApplet extends JApplet implements ActionListener {

                     JTextField boxRectangle;
                     JTextArea boxLocation;
                     JLabel heading;
                     int increase;
                     int word;


                     public void init () {


                          boxRectangle = new JTextField (10);
                          getContentPane().add(boxRectangle);
                          boxRectangle.addActionListener(this);


                          heading = new JLabel("Enter something herer");
                          getContentPane().add(heading);


                          getContentPane().setLayout(new FlowLayout());


                          boxLocation = new JTextArea(10,20);
                          getContentPane().add(boxLocation);


                     }

                     public void actionPerformed(ActionEvent e){
                          increase = Integer.parseInt(boxRectangle.getText());

                          ++increase;
                          boxLocation.setText("Your new number is now: " +increase);
                     }
                }