Converting String input to Integer from JTextField with Integer.parseInt but still receiving error message Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to convert strings into integers in Python?Converting an integer to a string in PHPConvert a string to an integer in JavaScript?Why can't I draw an ellipse with this code?How do I convert from int to String?how do you set text on JTextField that carries the java bean value after clicking submitJava applet layout and action handler issueNumberFormatException: empty string when clicking the JButton?Java JTextField actionListener not workingHow to split a number and a word which are altogether (as: 5John, 12Annie, 32Oliver) and prints them separately

Why these surprising proportionalities of integrals involving odd zeta values?

Are Flameskulls resistant to magical piercing damage?

Should man-made satellites feature an intelligent inverted "cow catcher"?

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

What is the ongoing value of the Kanban board to the developers as opposed to management

Is there a verb for listening stealthily?

Marquee sign letters

Unix AIX passing variable and arguments to expect and spawn

What's the connection between Mr. Nancy and fried chicken?

Is there a way to convert Wolfram Language expression to string?

What is the definining line between a helicopter and a drone a person can ride in?

Lights are flickering on and off after accidentally bumping into light switch

Are bags of holding fireproof?

Putting Ant-Man on house arrest

What could prevent concentrated local exploration?

Does traveling In The United States require a passport or can I use my green card if not a US citizen?

Short story about an alien named Ushtu(?) coming from a future Earth, when ours was destroyed by a nuclear explosion

How do I deal with an erroneously large refund?

How to break 信じようとしていただけかも知れない into separate parts?

Why aren't these two solutions equivalent? Combinatorics problem

Determine the generator of an ideal of ring of integers

Continue tikz picture on next page

Why doesn't the university give past final exams' answers?

Why do C and C++ allow the expression (int) + 4*5?



Converting String input to Integer from JTextField with Integer.parseInt but still receiving error message



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to convert strings into integers in Python?Converting an integer to a string in PHPConvert a string to an integer in JavaScript?Why can't I draw an ellipse with this code?How do I convert from int to String?how do you set text on JTextField that carries the java bean value after clicking submitJava applet layout and action handler issueNumberFormatException: empty string when clicking the JButton?Java JTextField actionListener not workingHow to split a number and a word which are altogether (as: 5John, 12Annie, 32Oliver) and prints them separately



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















  1. textSeventh is a JTextField

  2. I am using a submit button with an action listener

Code:



String amountInput = textSeventh.getText();
System.out.println(amountInput);

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UI2 implements ActionListener

public static void main(String[] args)
// Dont use the static context. Make an instance variable and call your method.
UI ui = new UI();
ui.makeUI();


static String questionFirst = "What is your first name?";
static String questionSecond = "What is your last name?";
static String questionThird = "What month were you born? Enter one number.";
static String questionFourth = "What year were you born?";
static String questionFifth = "What day were you born?";
static String questionSixth = "What is your bank account number";
static String questionSeventh = "How much is in your bank account? Include decimals.";

public void makeUI()
makeBox(questionFirst, questionSecond, questionThird, questionFourth, questionFifth, questionSixth, questionSeventh);


static JFrame frame = new JFrame("FortuneTeller");
static JPanel panel = new JPanel(new GridBagLayout());

static JLabel labelFirst = new JLabel();
static JTextField textFirst = new JTextField(50);

static JLabel labelSecond = new JLabel();
static JTextField textSecond = new JTextField(50);

static JLabel labelThird = new JLabel();
static JTextField textThird = new JTextField(50);

static JLabel labelFourth = new JLabel();
static JTextField textFourth = new JTextField(50);

static JLabel labelFifth = new JLabel();
static JTextField textFifth = new JTextField(50);

static JLabel labelSixth = new JLabel();
static JTextField textSixth = new JTextField(50);

static JLabel labelSeventh = new JLabel();
static JTextField textSeventh = new JTextField(50);

static JButton submitButton = new JButton("Submit");

static String firstName;
static String lastName;
static String month;
static String year;
static String day;
static String bankNum;
static String amount;

static char favoriteLetter;
static String both;
static String reverse;
static String favoritePalindrome;
static String favoriteColor; // red or blue
static String favoriteAnimal; // cat or dog
static String favoriteCar; // F150 or Minivan
static String favoriteNum;
static int intDollars;
static String math;

public void makeBox(String questionFirst, String questionSecond, String questionThird, String questionFourth, String questionFifth, String questionSixth,
String questionSeventh)
frame.add(panel);
frame.setVisible(true);
frame.setSize(700, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new GridLayout(8, 1));
// Now I am only trying to get input from the first box
panel.add(labelFirst);
labelFirst.setText(questionFirst);
panel.add(textFirst);

// This is the advice from my school's computer science teacher when we thought
// that
// the program was printing the initial value inside the textfield, nothing.
// Enter text
// Wait for submit button
// Then getText();
// I was still unable to get it to work

panel.add(labelSecond);
labelSecond.setText(questionSecond);
panel.add(textSecond);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
lastName = textSecond.getText();

panel.add(labelThird);
labelThird.setText(questionThird);
panel.add(textThird);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
month = textThird.getText();

panel.add(labelFourth);
labelFourth.setText(questionFourth);
panel.add(textFourth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
year = textFourth.getText();

panel.add(labelFifth);
labelFifth.setText(questionFifth);
panel.add(textFifth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
day = textFifth.getText();

panel.add(labelSixth);
labelSixth.setText(questionSixth);
panel.add(textSixth);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
bankNum = textSixth.getText();

panel.add(labelSeventh);
labelSeventh.setText(questionSeventh);
panel.add(textSeventh);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
amount = textSeventh.getText();

// need to add an actionListener to the button
submitButton.addActionListener(this);

panel.add(submitButton);
frame.pack();


@Override
public void actionPerformed(ActionEvent e)

// TODO Auto-generated method stub

// You need to get the source of the button. (JButton) e.getSource();
JButton buttonPressed = (JButton) e.getSource();
if (buttonPressed == submitButton)
String firstNameInput = textFirst.getText();
System.out.println(firstNameInput);












share|improve this question



















  • 7





    @GSGIv Could you add the error you get here..?

    – Shamitha Silva
    Jun 4 '18 at 4:18











  • Do you do this in an ActionListener or did you just call it immediately after creating the JTextField?

    – MadProgrammer
    Jun 4 '18 at 4:21











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) For the sake of the user, offer them a JSpinner with SpinnerNumberModel (instead of a text field).

    – Andrew Thompson
    Jun 4 '18 at 9:13











  • favoriteLetter = lastNameInput.toUpperCase().charAt(0); ... System.out.println("Your favorite letter is " + favoriteLetter + " ."); Do you see this System.out message? Because the charAt(0) looks extremely suspicious. I'd guess this is where your exception is being thrown from.

    – Rick
    Jun 5 '18 at 5:55


















0















  1. textSeventh is a JTextField

  2. I am using a submit button with an action listener

Code:



String amountInput = textSeventh.getText();
System.out.println(amountInput);

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UI2 implements ActionListener

public static void main(String[] args)
// Dont use the static context. Make an instance variable and call your method.
UI ui = new UI();
ui.makeUI();


static String questionFirst = "What is your first name?";
static String questionSecond = "What is your last name?";
static String questionThird = "What month were you born? Enter one number.";
static String questionFourth = "What year were you born?";
static String questionFifth = "What day were you born?";
static String questionSixth = "What is your bank account number";
static String questionSeventh = "How much is in your bank account? Include decimals.";

public void makeUI()
makeBox(questionFirst, questionSecond, questionThird, questionFourth, questionFifth, questionSixth, questionSeventh);


static JFrame frame = new JFrame("FortuneTeller");
static JPanel panel = new JPanel(new GridBagLayout());

static JLabel labelFirst = new JLabel();
static JTextField textFirst = new JTextField(50);

static JLabel labelSecond = new JLabel();
static JTextField textSecond = new JTextField(50);

static JLabel labelThird = new JLabel();
static JTextField textThird = new JTextField(50);

static JLabel labelFourth = new JLabel();
static JTextField textFourth = new JTextField(50);

static JLabel labelFifth = new JLabel();
static JTextField textFifth = new JTextField(50);

static JLabel labelSixth = new JLabel();
static JTextField textSixth = new JTextField(50);

static JLabel labelSeventh = new JLabel();
static JTextField textSeventh = new JTextField(50);

static JButton submitButton = new JButton("Submit");

static String firstName;
static String lastName;
static String month;
static String year;
static String day;
static String bankNum;
static String amount;

static char favoriteLetter;
static String both;
static String reverse;
static String favoritePalindrome;
static String favoriteColor; // red or blue
static String favoriteAnimal; // cat or dog
static String favoriteCar; // F150 or Minivan
static String favoriteNum;
static int intDollars;
static String math;

public void makeBox(String questionFirst, String questionSecond, String questionThird, String questionFourth, String questionFifth, String questionSixth,
String questionSeventh)
frame.add(panel);
frame.setVisible(true);
frame.setSize(700, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new GridLayout(8, 1));
// Now I am only trying to get input from the first box
panel.add(labelFirst);
labelFirst.setText(questionFirst);
panel.add(textFirst);

// This is the advice from my school's computer science teacher when we thought
// that
// the program was printing the initial value inside the textfield, nothing.
// Enter text
// Wait for submit button
// Then getText();
// I was still unable to get it to work

panel.add(labelSecond);
labelSecond.setText(questionSecond);
panel.add(textSecond);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
lastName = textSecond.getText();

panel.add(labelThird);
labelThird.setText(questionThird);
panel.add(textThird);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
month = textThird.getText();

panel.add(labelFourth);
labelFourth.setText(questionFourth);
panel.add(textFourth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
year = textFourth.getText();

panel.add(labelFifth);
labelFifth.setText(questionFifth);
panel.add(textFifth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
day = textFifth.getText();

panel.add(labelSixth);
labelSixth.setText(questionSixth);
panel.add(textSixth);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
bankNum = textSixth.getText();

panel.add(labelSeventh);
labelSeventh.setText(questionSeventh);
panel.add(textSeventh);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
amount = textSeventh.getText();

// need to add an actionListener to the button
submitButton.addActionListener(this);

panel.add(submitButton);
frame.pack();


@Override
public void actionPerformed(ActionEvent e)

// TODO Auto-generated method stub

// You need to get the source of the button. (JButton) e.getSource();
JButton buttonPressed = (JButton) e.getSource();
if (buttonPressed == submitButton)
String firstNameInput = textFirst.getText();
System.out.println(firstNameInput);












share|improve this question



















  • 7





    @GSGIv Could you add the error you get here..?

    – Shamitha Silva
    Jun 4 '18 at 4:18











  • Do you do this in an ActionListener or did you just call it immediately after creating the JTextField?

    – MadProgrammer
    Jun 4 '18 at 4:21











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) For the sake of the user, offer them a JSpinner with SpinnerNumberModel (instead of a text field).

    – Andrew Thompson
    Jun 4 '18 at 9:13











  • favoriteLetter = lastNameInput.toUpperCase().charAt(0); ... System.out.println("Your favorite letter is " + favoriteLetter + " ."); Do you see this System.out message? Because the charAt(0) looks extremely suspicious. I'd guess this is where your exception is being thrown from.

    – Rick
    Jun 5 '18 at 5:55














0












0








0








  1. textSeventh is a JTextField

  2. I am using a submit button with an action listener

Code:



String amountInput = textSeventh.getText();
System.out.println(amountInput);

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UI2 implements ActionListener

public static void main(String[] args)
// Dont use the static context. Make an instance variable and call your method.
UI ui = new UI();
ui.makeUI();


static String questionFirst = "What is your first name?";
static String questionSecond = "What is your last name?";
static String questionThird = "What month were you born? Enter one number.";
static String questionFourth = "What year were you born?";
static String questionFifth = "What day were you born?";
static String questionSixth = "What is your bank account number";
static String questionSeventh = "How much is in your bank account? Include decimals.";

public void makeUI()
makeBox(questionFirst, questionSecond, questionThird, questionFourth, questionFifth, questionSixth, questionSeventh);


static JFrame frame = new JFrame("FortuneTeller");
static JPanel panel = new JPanel(new GridBagLayout());

static JLabel labelFirst = new JLabel();
static JTextField textFirst = new JTextField(50);

static JLabel labelSecond = new JLabel();
static JTextField textSecond = new JTextField(50);

static JLabel labelThird = new JLabel();
static JTextField textThird = new JTextField(50);

static JLabel labelFourth = new JLabel();
static JTextField textFourth = new JTextField(50);

static JLabel labelFifth = new JLabel();
static JTextField textFifth = new JTextField(50);

static JLabel labelSixth = new JLabel();
static JTextField textSixth = new JTextField(50);

static JLabel labelSeventh = new JLabel();
static JTextField textSeventh = new JTextField(50);

static JButton submitButton = new JButton("Submit");

static String firstName;
static String lastName;
static String month;
static String year;
static String day;
static String bankNum;
static String amount;

static char favoriteLetter;
static String both;
static String reverse;
static String favoritePalindrome;
static String favoriteColor; // red or blue
static String favoriteAnimal; // cat or dog
static String favoriteCar; // F150 or Minivan
static String favoriteNum;
static int intDollars;
static String math;

public void makeBox(String questionFirst, String questionSecond, String questionThird, String questionFourth, String questionFifth, String questionSixth,
String questionSeventh)
frame.add(panel);
frame.setVisible(true);
frame.setSize(700, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new GridLayout(8, 1));
// Now I am only trying to get input from the first box
panel.add(labelFirst);
labelFirst.setText(questionFirst);
panel.add(textFirst);

// This is the advice from my school's computer science teacher when we thought
// that
// the program was printing the initial value inside the textfield, nothing.
// Enter text
// Wait for submit button
// Then getText();
// I was still unable to get it to work

panel.add(labelSecond);
labelSecond.setText(questionSecond);
panel.add(textSecond);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
lastName = textSecond.getText();

panel.add(labelThird);
labelThird.setText(questionThird);
panel.add(textThird);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
month = textThird.getText();

panel.add(labelFourth);
labelFourth.setText(questionFourth);
panel.add(textFourth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
year = textFourth.getText();

panel.add(labelFifth);
labelFifth.setText(questionFifth);
panel.add(textFifth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
day = textFifth.getText();

panel.add(labelSixth);
labelSixth.setText(questionSixth);
panel.add(textSixth);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
bankNum = textSixth.getText();

panel.add(labelSeventh);
labelSeventh.setText(questionSeventh);
panel.add(textSeventh);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
amount = textSeventh.getText();

// need to add an actionListener to the button
submitButton.addActionListener(this);

panel.add(submitButton);
frame.pack();


@Override
public void actionPerformed(ActionEvent e)

// TODO Auto-generated method stub

// You need to get the source of the button. (JButton) e.getSource();
JButton buttonPressed = (JButton) e.getSource();
if (buttonPressed == submitButton)
String firstNameInput = textFirst.getText();
System.out.println(firstNameInput);












share|improve this question
















  1. textSeventh is a JTextField

  2. I am using a submit button with an action listener

Code:



String amountInput = textSeventh.getText();
System.out.println(amountInput);

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UI2 implements ActionListener

public static void main(String[] args)
// Dont use the static context. Make an instance variable and call your method.
UI ui = new UI();
ui.makeUI();


static String questionFirst = "What is your first name?";
static String questionSecond = "What is your last name?";
static String questionThird = "What month were you born? Enter one number.";
static String questionFourth = "What year were you born?";
static String questionFifth = "What day were you born?";
static String questionSixth = "What is your bank account number";
static String questionSeventh = "How much is in your bank account? Include decimals.";

public void makeUI()
makeBox(questionFirst, questionSecond, questionThird, questionFourth, questionFifth, questionSixth, questionSeventh);


static JFrame frame = new JFrame("FortuneTeller");
static JPanel panel = new JPanel(new GridBagLayout());

static JLabel labelFirst = new JLabel();
static JTextField textFirst = new JTextField(50);

static JLabel labelSecond = new JLabel();
static JTextField textSecond = new JTextField(50);

static JLabel labelThird = new JLabel();
static JTextField textThird = new JTextField(50);

static JLabel labelFourth = new JLabel();
static JTextField textFourth = new JTextField(50);

static JLabel labelFifth = new JLabel();
static JTextField textFifth = new JTextField(50);

static JLabel labelSixth = new JLabel();
static JTextField textSixth = new JTextField(50);

static JLabel labelSeventh = new JLabel();
static JTextField textSeventh = new JTextField(50);

static JButton submitButton = new JButton("Submit");

static String firstName;
static String lastName;
static String month;
static String year;
static String day;
static String bankNum;
static String amount;

static char favoriteLetter;
static String both;
static String reverse;
static String favoritePalindrome;
static String favoriteColor; // red or blue
static String favoriteAnimal; // cat or dog
static String favoriteCar; // F150 or Minivan
static String favoriteNum;
static int intDollars;
static String math;

public void makeBox(String questionFirst, String questionSecond, String questionThird, String questionFourth, String questionFifth, String questionSixth,
String questionSeventh)
frame.add(panel);
frame.setVisible(true);
frame.setSize(700, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new GridLayout(8, 1));
// Now I am only trying to get input from the first box
panel.add(labelFirst);
labelFirst.setText(questionFirst);
panel.add(textFirst);

// This is the advice from my school's computer science teacher when we thought
// that
// the program was printing the initial value inside the textfield, nothing.
// Enter text
// Wait for submit button
// Then getText();
// I was still unable to get it to work

panel.add(labelSecond);
labelSecond.setText(questionSecond);
panel.add(textSecond);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
lastName = textSecond.getText();

panel.add(labelThird);
labelThird.setText(questionThird);
panel.add(textThird);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
month = textThird.getText();

panel.add(labelFourth);
labelFourth.setText(questionFourth);
panel.add(textFourth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
year = textFourth.getText();

panel.add(labelFifth);
labelFifth.setText(questionFifth);
panel.add(textFifth);

// get text will be empty here. You should be calling this after the user enters text and clicks submit.
day = textFifth.getText();

panel.add(labelSixth);
labelSixth.setText(questionSixth);
panel.add(textSixth);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
bankNum = textSixth.getText();

panel.add(labelSeventh);
labelSeventh.setText(questionSeventh);
panel.add(textSeventh);
// get text will be empty here. You should be calling this after the user enters text and clicks submit.
amount = textSeventh.getText();

// need to add an actionListener to the button
submitButton.addActionListener(this);

panel.add(submitButton);
frame.pack();


@Override
public void actionPerformed(ActionEvent e)

// TODO Auto-generated method stub

// You need to get the source of the button. (JButton) e.getSource();
JButton buttonPressed = (JButton) e.getSource();
if (buttonPressed == submitButton)
String firstNameInput = textFirst.getText();
System.out.println(firstNameInput);









java swing integer jtextfield






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 2:04









mvmn

1,8241524




1,8241524










asked Jun 4 '18 at 4:17









GSGlvGSGlv

11




11







  • 7





    @GSGIv Could you add the error you get here..?

    – Shamitha Silva
    Jun 4 '18 at 4:18











  • Do you do this in an ActionListener or did you just call it immediately after creating the JTextField?

    – MadProgrammer
    Jun 4 '18 at 4:21











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) For the sake of the user, offer them a JSpinner with SpinnerNumberModel (instead of a text field).

    – Andrew Thompson
    Jun 4 '18 at 9:13











  • favoriteLetter = lastNameInput.toUpperCase().charAt(0); ... System.out.println("Your favorite letter is " + favoriteLetter + " ."); Do you see this System.out message? Because the charAt(0) looks extremely suspicious. I'd guess this is where your exception is being thrown from.

    – Rick
    Jun 5 '18 at 5:55













  • 7





    @GSGIv Could you add the error you get here..?

    – Shamitha Silva
    Jun 4 '18 at 4:18











  • Do you do this in an ActionListener or did you just call it immediately after creating the JTextField?

    – MadProgrammer
    Jun 4 '18 at 4:21











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) For the sake of the user, offer them a JSpinner with SpinnerNumberModel (instead of a text field).

    – Andrew Thompson
    Jun 4 '18 at 9:13











  • favoriteLetter = lastNameInput.toUpperCase().charAt(0); ... System.out.println("Your favorite letter is " + favoriteLetter + " ."); Do you see this System.out message? Because the charAt(0) looks extremely suspicious. I'd guess this is where your exception is being thrown from.

    – Rick
    Jun 5 '18 at 5:55








7




7





@GSGIv Could you add the error you get here..?

– Shamitha Silva
Jun 4 '18 at 4:18





@GSGIv Could you add the error you get here..?

– Shamitha Silva
Jun 4 '18 at 4:18













Do you do this in an ActionListener or did you just call it immediately after creating the JTextField?

– MadProgrammer
Jun 4 '18 at 4:21





Do you do this in an ActionListener or did you just call it immediately after creating the JTextField?

– MadProgrammer
Jun 4 '18 at 4:21













1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) For the sake of the user, offer them a JSpinner with SpinnerNumberModel (instead of a text field).

– Andrew Thompson
Jun 4 '18 at 9:13





1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) For the sake of the user, offer them a JSpinner with SpinnerNumberModel (instead of a text field).

– Andrew Thompson
Jun 4 '18 at 9:13













favoriteLetter = lastNameInput.toUpperCase().charAt(0); ... System.out.println("Your favorite letter is " + favoriteLetter + " ."); Do you see this System.out message? Because the charAt(0) looks extremely suspicious. I'd guess this is where your exception is being thrown from.

– Rick
Jun 5 '18 at 5:55






favoriteLetter = lastNameInput.toUpperCase().charAt(0); ... System.out.println("Your favorite letter is " + favoriteLetter + " ."); Do you see this System.out message? Because the charAt(0) looks extremely suspicious. I'd guess this is where your exception is being thrown from.

– Rick
Jun 5 '18 at 5:55













1 Answer
1






active

oldest

votes


















1














If you're getting a NumberFormatException then java can't parse whatever it is as a number.



Other things to check for would be empty strings "" or null strings (in which case it may throw a null pointer exception).



Maybe also check if there is spurious whitespace - get rid of it with a trim e.g.



//get favorite number
String amountInput = textSeventh.getText();

if (amountInput == null)
System.err.println("amountInput was null, no point continuing");
return;


// make it a bit more obvious if there's whitespace
System.out.println("amountInput = +++" + amountInput + "+++");

amountInput = amountInput.trim();

if (amountInput.equalsIgnoreCase(""))
System.err.println("There's nothing there!!");


int dollars = -1337;
try
dollars = Integer.parseInt(amountInput);
catch (Exception e)
System.err.println("Error when parsing value.n" + e);
// optional
// e.printStackTrace();

System.out.println(dollars);





share|improve this answer























  • Here is part of the error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    – GSGlv
    Jun 4 '18 at 12:40












  • The error message is still present with the edited code above

    – GSGlv
    Jun 4 '18 at 12:53











  • in which line actually you are getting the error message? Can you post the entire edited code here?

    – Sivanandham
    Jun 4 '18 at 13:03












  • And one more thing. StringIndexOutOfBoundsException means your trying to retrieve invalid index. Check length of the String before parsing into integer.

    – Sivanandham
    Jun 4 '18 at 13:07











  • I am trying to input an integer or double value such as 12 into the textField and the convert it from a String to an integer.

    – GSGlv
    Jun 4 '18 at 13:13











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50673334%2fconverting-string-input-to-integer-from-jtextfield-with-integer-parseint-but-sti%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you're getting a NumberFormatException then java can't parse whatever it is as a number.



Other things to check for would be empty strings "" or null strings (in which case it may throw a null pointer exception).



Maybe also check if there is spurious whitespace - get rid of it with a trim e.g.



//get favorite number
String amountInput = textSeventh.getText();

if (amountInput == null)
System.err.println("amountInput was null, no point continuing");
return;


// make it a bit more obvious if there's whitespace
System.out.println("amountInput = +++" + amountInput + "+++");

amountInput = amountInput.trim();

if (amountInput.equalsIgnoreCase(""))
System.err.println("There's nothing there!!");


int dollars = -1337;
try
dollars = Integer.parseInt(amountInput);
catch (Exception e)
System.err.println("Error when parsing value.n" + e);
// optional
// e.printStackTrace();

System.out.println(dollars);





share|improve this answer























  • Here is part of the error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    – GSGlv
    Jun 4 '18 at 12:40












  • The error message is still present with the edited code above

    – GSGlv
    Jun 4 '18 at 12:53











  • in which line actually you are getting the error message? Can you post the entire edited code here?

    – Sivanandham
    Jun 4 '18 at 13:03












  • And one more thing. StringIndexOutOfBoundsException means your trying to retrieve invalid index. Check length of the String before parsing into integer.

    – Sivanandham
    Jun 4 '18 at 13:07











  • I am trying to input an integer or double value such as 12 into the textField and the convert it from a String to an integer.

    – GSGlv
    Jun 4 '18 at 13:13















1














If you're getting a NumberFormatException then java can't parse whatever it is as a number.



Other things to check for would be empty strings "" or null strings (in which case it may throw a null pointer exception).



Maybe also check if there is spurious whitespace - get rid of it with a trim e.g.



//get favorite number
String amountInput = textSeventh.getText();

if (amountInput == null)
System.err.println("amountInput was null, no point continuing");
return;


// make it a bit more obvious if there's whitespace
System.out.println("amountInput = +++" + amountInput + "+++");

amountInput = amountInput.trim();

if (amountInput.equalsIgnoreCase(""))
System.err.println("There's nothing there!!");


int dollars = -1337;
try
dollars = Integer.parseInt(amountInput);
catch (Exception e)
System.err.println("Error when parsing value.n" + e);
// optional
// e.printStackTrace();

System.out.println(dollars);





share|improve this answer























  • Here is part of the error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    – GSGlv
    Jun 4 '18 at 12:40












  • The error message is still present with the edited code above

    – GSGlv
    Jun 4 '18 at 12:53











  • in which line actually you are getting the error message? Can you post the entire edited code here?

    – Sivanandham
    Jun 4 '18 at 13:03












  • And one more thing. StringIndexOutOfBoundsException means your trying to retrieve invalid index. Check length of the String before parsing into integer.

    – Sivanandham
    Jun 4 '18 at 13:07











  • I am trying to input an integer or double value such as 12 into the textField and the convert it from a String to an integer.

    – GSGlv
    Jun 4 '18 at 13:13













1












1








1







If you're getting a NumberFormatException then java can't parse whatever it is as a number.



Other things to check for would be empty strings "" or null strings (in which case it may throw a null pointer exception).



Maybe also check if there is spurious whitespace - get rid of it with a trim e.g.



//get favorite number
String amountInput = textSeventh.getText();

if (amountInput == null)
System.err.println("amountInput was null, no point continuing");
return;


// make it a bit more obvious if there's whitespace
System.out.println("amountInput = +++" + amountInput + "+++");

amountInput = amountInput.trim();

if (amountInput.equalsIgnoreCase(""))
System.err.println("There's nothing there!!");


int dollars = -1337;
try
dollars = Integer.parseInt(amountInput);
catch (Exception e)
System.err.println("Error when parsing value.n" + e);
// optional
// e.printStackTrace();

System.out.println(dollars);





share|improve this answer













If you're getting a NumberFormatException then java can't parse whatever it is as a number.



Other things to check for would be empty strings "" or null strings (in which case it may throw a null pointer exception).



Maybe also check if there is spurious whitespace - get rid of it with a trim e.g.



//get favorite number
String amountInput = textSeventh.getText();

if (amountInput == null)
System.err.println("amountInput was null, no point continuing");
return;


// make it a bit more obvious if there's whitespace
System.out.println("amountInput = +++" + amountInput + "+++");

amountInput = amountInput.trim();

if (amountInput.equalsIgnoreCase(""))
System.err.println("There's nothing there!!");


int dollars = -1337;
try
dollars = Integer.parseInt(amountInput);
catch (Exception e)
System.err.println("Error when parsing value.n" + e);
// optional
// e.printStackTrace();

System.out.println(dollars);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 4 '18 at 4:37









RickRick

443210




443210












  • Here is part of the error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    – GSGlv
    Jun 4 '18 at 12:40












  • The error message is still present with the edited code above

    – GSGlv
    Jun 4 '18 at 12:53











  • in which line actually you are getting the error message? Can you post the entire edited code here?

    – Sivanandham
    Jun 4 '18 at 13:03












  • And one more thing. StringIndexOutOfBoundsException means your trying to retrieve invalid index. Check length of the String before parsing into integer.

    – Sivanandham
    Jun 4 '18 at 13:07











  • I am trying to input an integer or double value such as 12 into the textField and the convert it from a String to an integer.

    – GSGlv
    Jun 4 '18 at 13:13

















  • Here is part of the error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    – GSGlv
    Jun 4 '18 at 12:40












  • The error message is still present with the edited code above

    – GSGlv
    Jun 4 '18 at 12:53











  • in which line actually you are getting the error message? Can you post the entire edited code here?

    – Sivanandham
    Jun 4 '18 at 13:03












  • And one more thing. StringIndexOutOfBoundsException means your trying to retrieve invalid index. Check length of the String before parsing into integer.

    – Sivanandham
    Jun 4 '18 at 13:07











  • I am trying to input an integer or double value such as 12 into the textField and the convert it from a String to an integer.

    – GSGlv
    Jun 4 '18 at 13:13
















Here is part of the error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

– GSGlv
Jun 4 '18 at 12:40






Here is part of the error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

– GSGlv
Jun 4 '18 at 12:40














The error message is still present with the edited code above

– GSGlv
Jun 4 '18 at 12:53





The error message is still present with the edited code above

– GSGlv
Jun 4 '18 at 12:53













in which line actually you are getting the error message? Can you post the entire edited code here?

– Sivanandham
Jun 4 '18 at 13:03






in which line actually you are getting the error message? Can you post the entire edited code here?

– Sivanandham
Jun 4 '18 at 13:03














And one more thing. StringIndexOutOfBoundsException means your trying to retrieve invalid index. Check length of the String before parsing into integer.

– Sivanandham
Jun 4 '18 at 13:07





And one more thing. StringIndexOutOfBoundsException means your trying to retrieve invalid index. Check length of the String before parsing into integer.

– Sivanandham
Jun 4 '18 at 13:07













I am trying to input an integer or double value such as 12 into the textField and the convert it from a String to an integer.

– GSGlv
Jun 4 '18 at 13:13





I am trying to input an integer or double value such as 12 into the textField and the convert it from a String to an integer.

– GSGlv
Jun 4 '18 at 13:13



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50673334%2fconverting-string-input-to-integer-from-jtextfield-with-integer-parseint-but-sti%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко