Please can anyone tell me how to convert a string in a text field to an integer. im using

[ String q = answerField.getText();

int n1 = Integer.parseInt(q);]

in my programme but i get the errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Enter Answer here!"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at BackgroundImage.<init>(BackgroundImage.java:72)
at BackgroundImage.main(BackgroundImage.java:15)
can anyone help please?

Below is my programme code please can any one help me convert a string value into an integer:

import javax.swing.*;

import java.awt.*;

public class BackgroundImage extends JFrame {
       JScrollPane scrollPane;
       ImageIcon icon;
       Image image;
       String t;
       int choice;

       public static void main(String args[]) {

               BackgroundImage frame = new BackgroundImage();
               // frame.BackgroundImage(frame, menuFrame);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
               frame.setLocationRelativeTo(null);
               frame.setVisible(true);
       }

       public BackgroundImage() {
               icon = new ImageIcon("f://main game finish.jpg");

               // //////////////////////////////////////
               JPanel panel = new JPanel() {
                       protected void paintComponent(Graphics g) {
                               // Display image at at full size
                               g.drawImage(icon.getImage(), 0, 0, null);

                               // Scale image to size of component
                               Dimension d = getSize();
                               g.drawImage(icon.getImage(), 1400, 1000, d.width, d.height,
                                               null);

                               // Fix the image position in the scroll pane
                               Point p = scrollPane.getViewport().getViewPosition();
                               g.drawImage(icon.getImage(), p.x, p.y, null);



                                       }


               };
               // ///////////////////////////////////////

               panel.setOpaque(false);
               panel.setPreferredSize(new Dimension(400, 400));
               scrollPane = new JScrollPane(panel);
               getContentPane().add(scrollPane);

               String question = "<html>" +
               "<b><u>PLEASE ANSWER THE QUESTION:</u></b><br>" +
               "1)IS THE ANSWER 5:<br>" +
               "2)IS THE ANSWER 6:<br>" +
               "3)IS THE ANSWER 30:" +
               "</html>";

               JLabel questionLabel = new JLabel(question);
               panel.add(questionLabel);



               JTextField answerField = new JTextField("Enter Answer here!");
               answerField.setSize(1000, 1000);
               answerField.setLocation(new Point(10,20));
               panel.add(answerField);
               String q = answerField.getText(); 

               int n1 = Integer.parseInt(q);

               JButton okButton = new JButton("OK");
               panel.add(okButton);
       }
}

i,v tried using the Integer.parseInt statement but i get these errors:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Enter Answer here!"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at BackgroundImage.<init>(BackgroundImage.java:72)
    at BackgroundImage.main(BackgroundImage.java:15)

please can anyone help

Parsing the answer needs to occur after they have entered the answer, such as in a button handler for the "Ok" button. You are currently parsing the original value you placed in that field "Enter Answer here!".

Your code is correct. This is how it is done.
The problem is that you are trying to convert the String: "Enter Answer here!" into an int which cannot happen.
As you can see at the message that you posted it:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Enter Answer here!"

So I guess that in the code you posted:

String q = answerField.getText();
int n1 = Integer.parseInt(q);

You give q the value: "Enter Answer here!"

If q has a number as value like this, it will work:

String q = "23";
int i = Integer.parseInt(q);

i has now the int value 23

If q has any String value like this, it will NOT work:

String q = "Enter Answer here!";
int i = Integer.parseInt(q);

The second line will throw the NumberFormatException that you saw.

How to avoid it?. enter at the textfield only int numbers.
And better: read and learn about exceptions:

String q;
int n1;
try {
  q = answerField.getText(); 
  n1 = Integer.parseInt(q);
} catch (NumberFormatException nfe) {
  System.out.println( "NumberFormatException occured: " + nfe.getMessage() );
}

you must desclare this one first bro!

String q="";
q = answerField.getText();
int n1 = Integer.parseInt(q);

//email me whats the result ok!
jalpex91@gmail.com

you must desclare this one first bro!

String q="";
q = answerField.getText();
int n1 = Integer.parseInt(q);

//email me whats the result ok!
jalpex91@gmail.com

This is what puk has already written : String q = answerField.getText();
It is the same as what you suggested. If he is going to use your two-line solution or not will not make any difference

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.