Kobrien
Kobrien

Reputation: 85

Getting compiler error for unknown reason

Alright so my program is getting the user's input to figure out the prime numbers (up to the max which the user entered), and then display these results in a scrollable JFrame. I have done all of that (I believe so at least) but keep getting one error when I try and compile it. Also, if you see any other mistakes that I have missed feel free to let me know!

Code:

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

public class PrimeNumbersJ extends JFrame
{
    private static final int WIDTH=400;
    private static final int HEIGHT=300;

    //JFrame Components
    private JLabel jlblMaxNumber;

    private JTextArea  jtaOutput;

    private JTextField jtfMaxNumber;

    private JButton jbtnCalculate, jbtnClear, jbtnExit;

    private CalculateButtonHandler calculateHandler;
    private ClearButtonHandler  clearHandler;
    private ExitButtonHandler   exitHandler;

    private JScrollPane scrollingResult;

    private JPanel jpnlTop = new JPanel();
    private JPanel jpnlCenter = new JPanel();
    private JPanel jpnlBottom = new JPanel();


    public PrimeNumbersJ()
    {
        // Set the title and Size:
        setTitle("Prime Numbers with JFrame");
        setSize(WIDTH, HEIGHT);
        jpnlBottom.setLayout(new GridLayout(1, 3));

        // Instantiate the JLabel components:
        jlblMaxNumber = new JLabel("Enter the Largest Number to test: ", SwingConstants.LEFT);

        // Instantiate the JTextFields:
        jtfMaxNumber = new JTextField(10);

        // Make the JTextArea scrollable:
        jtaOutput = new JTextArea(10,1);
        scrollingResult = new JScrollPane(jtaOutput);

        // Instantiate and register the Calculate button for clicks events:
        jbtnCalculate = new JButton("Calculate");
        calculateHandler = new CalculateButtonHandler();
        jbtnCalculate.addActionListener(calculateHandler);

        // Instantiate and register the Clear button for clicks events:
        jbtnClear = new JButton("Clear");
        clearHandler = new ClearButtonHandler();
        jbtnClear.addActionListener(clearHandler);

        // Instantiate and register the Exit button for clicks events:
        jbtnExit = new JButton("Exit");
        exitHandler = new ExitButtonHandler();
        jbtnExit.addActionListener(exitHandler);

        // Assemble the JPanels:
        jpnlTop.setLayout(new GridLayout(1, 2));
        jpnlTop.add(jlblMaxNumber);
        jpnlTop.add(jtfMaxNumber);

        jpnlCenter.setLayout(new GridLayout(1, 1));
        jpnlCenter.add(scrollingResult);

        jpnlBottom.setLayout(new GridLayout(1, 3));
        jpnlBottom.add(jbtnCalculate);
        jpnlBottom.add(jbtnClear);
        jpnlBottom.add(jbtnExit);

        // Start to add the components to the JFrame:
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());

        pane.add(jpnlTop, BorderLayout.NORTH);
        pane.add(jpnlCenter, BorderLayout.CENTER);
        pane.add(jpnlBottom, BorderLayout.SOUTH);


        // Show the JFrame and set code to respond to the user clicking on the X:
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        jpnlTop.setLayout(new GridLayout(1, 3));
        jpnlTop.add(jlblMaxNumber);
        jpnlTop.add(jtfMaxNumber);

        jpnlCenter.setLayout(new GridLayout(1, 1));
        jpnlCenter.add(scrollingResult);

        jpnlBottom.add(jbtnCalculate);
        jpnlBottom.add(jbtnClear);
        jpnlBottom.add(jbtnExit);

        // Show the JFrame and set code to respond to the user clicking on the X:
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }//End Constructor


    private class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            int iRemainder,iPrimeCheck;
            int iNumbertoTest = 0;
            boolean bValidInput = true;
            String sPrime ="";

            try
            {
                iNumbertoTest = Integer.parseInteger(jtfMaxNumber.getText());
        }
            catch (Exception aeRef)
            {
                JOptionPane.showMessageDialog(null,"Enter the Max Number to Test.", getTitle(), JOptionPane.WARNING_MESSAGE);
                bValidInput = false;
            }// end of catch


            if ( bValidInput )
            {

                for(iNumberToTest = 1;iNumberToTest <= 100;iNumberToTest++)    {
                    iRemainder = 0;
                    for(iPrimeCheck = 1;iPrimeCheck <= iNumberToTest;iPrimeCheck++){
                        if(iNumberToTest % iPrimeCheck == 0){
                                iRemainder++;
                            }
                        }
                        if(iRemainder == 2 || iNumberToTest == 1)
                        {
                                String sNumber = Integer.toString(iNumberToTest);
                                sPrime = sPrime + (sNumber + "\n");
                        }


            }
                // Populate the output by using the methods in the user defined class::

                jtaOutput.append("The Prime Numbers Are: \n" + sPrime  + "\n");
            } // end if
        } //end ActionPerformed
    }//End CalculateButtonHandler

    private class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }//end ExitButtonHandler


    private class ClearButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            jtfMaxNumber.setText("");
            jtaOutput.setText("");
         }
    } // end ClearButtonHandler






    public static void main(String args[])
    {
        PrimeNumbersJ primNumJ = new PrimeNumbersJ();
    }
}

Error

java:120: cannot find symbol
symbol  : method parseInteger(java.lang.String)
location: class java.lang.Integer
            iMaxNumber = Integer.parseInteger(jtfMaxNumber.getText());
                                ^

Upvotes: 0

Views: 118

Answers (3)

Andremoniy
Andremoniy

Reputation: 34900

The Integer class doesn't contain method called parseInteger. Use parseInt instead.

Upvotes: 2

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20741

change Integer.parseInteger() to

Integer.parseInt()

also declare int iNumberToTest as class variable in CalculateButtonHandler class

Upvotes: 4

jlordo
jlordo

Reputation: 37813

Integer.parseInteger()

does not exist.

Are you looking for Integer.parseInt() ???

Upvotes: 6

Related Questions