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

Thread: Number Format Exception

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Number Format Exception

    Okay, I am in the process of making this program that basically takes the different numbers that are put in, and puts text on the screen depending on which of the two options were selected. HOWEVER, every time i hit calculate, i am getting a Number Format Exception. If anyone could please help me with this i would be greatly appreciative.

    package resale_programs;
     
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Insets;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     
     
    public class minToSellGUIVERSION {
     
     
    	public static void main(String[] args) {
    		resaleGUI g = new resaleGUI();
    		g.setBounds(0, 0, 230, 385);
    		g.setVisible(true);
    		while(g.isVisible()) {};
     
    	}
     
     
    }
     
    class resaleGUI extends JFrame {
     
    	private String itemName;
    	private int quantity;
    	private double pricePerLot;
    	private int amountToSell;
    	private double amountPerSale;
     
    	public resaleGUI() {
    		initComponents();
    	}
     
    	private void calculateButtonPressed(ActionEvent e) {
    		resaleGUI newItem = new resaleGUI();
    		newItem.getQuantity();
    		newItem.pricePerLot();
    		newItem.amountToSell();
    		newItem.amountPerSale();
    		if(comboBox1.getSelectedItem().toString().equals("Profit Reaching Calculator")) {
    			label7.setText("You need to sell " + newItem.calcAmount() + " items to reach a profit.");
    		}
    		else {
    			label7.setText("You will make $" + newItem.calcProfit() + " from selling " + amountToSell + " packages!");
    		}
    	}
     
    	private int totalPackages() {
    		return quantity%amountToSell >= 1 ? (quantity/amountToSell)+1 : (quantity/amountToSell);
     
    	}
     
    	private double calcProfit() {
    		return pricePerLot - amountPerSale*amountToSell;
    	}
     
    	private int calcAmount() {
     
    		double perPackage = amountPerSale;
    		double currentPrice = 0;
    		int count = 0;
    		while(currentPrice < pricePerLot) {
    			currentPrice += perPackage;
    			count++;
    		}
    		return count;
    	}
     
    	private void amountToSell() {
    		amountToSell = Integer.parseInt(textField4.getText());
    	}
     
    	private void amountPerSale() {
    		amountPerSale = Double.parseDouble(textField3.getText());
    	}
     
    	private void pricePerLot() {
    		pricePerLot = Double.parseDouble(textField2.getText());
    	}
     
    	private void getQuantity() {
    		quantity = Integer.parseInt(textField1.getText());
    	}
     
    	private void initComponents() {
    		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    		// Generated using JFormDesigner Open Source Project license - unknown
    		comboBox1 = new JComboBox();
    		label2 = new JLabel();
    		textField1 = new JTextField();
    		label3 = new JLabel();
    		label4 = new JLabel();
    		textField2 = new JTextField();
    		label5 = new JLabel();
    		textField3 = new JTextField();
    		textField4 = new JTextField();
    		label6 = new JLabel();
    		button1 = new JButton();
    		label7 = new JLabel();
     
    		//======== this ========
    		Container contentPane = getContentPane();
    		contentPane.setLayout(null);
     
    		//---- comboBox1 ----
    		comboBox1.setModel(new DefaultComboBoxModel(new String[] {
    			"Profit Reaching Calculator",
    			"Profit Calculator"
    		}));
    		contentPane.add(comboBox1);
    		comboBox1.setBounds(new Rectangle(new Point(35, 50), comboBox1.getPreferredSize()));
     
    		//---- label2 ----
    		label2.setText("Business Calculator");
    		label2.setFont(new Font("Times New Roman", Font.PLAIN, 22));
    		contentPane.add(label2);
    		label2.setBounds(new Rectangle(new Point(20, 15), label2.getPreferredSize()));
    		contentPane.add(textField1);
    		textField1.setBounds(65, 100, 85, textField1.getPreferredSize().height);
     
    		//---- label3 ----
    		label3.setText("Quantity of Item:");
    		contentPane.add(label3);
    		label3.setBounds(new Rectangle(new Point(65, 85), label3.getPreferredSize()));
     
    		//---- label4 ----
    		label4.setText("Price per lot:");
    		contentPane.add(label4);
    		label4.setBounds(65, 130, label4.getPreferredSize().width, 15);
    		contentPane.add(textField2);
    		textField2.setBounds(65, 145, 85, textField2.getPreferredSize().height);
     
    		//---- label5 ----
    		label5.setText("Your sell price: ");
    		contentPane.add(label5);
    		label5.setBounds(new Rectangle(new Point(65, 175), label5.getPreferredSize()));
    		contentPane.add(textField3);
    		textField3.setBounds(65, 190, 85, textField3.getPreferredSize().height);
    		contentPane.add(textField4);
    		textField4.setBounds(65, 235, 85, textField4.getPreferredSize().height);
     
    		//---- label6 ----
    		label6.setText("Amount per package:");
    		contentPane.add(label6);
    		label6.setBounds(new Rectangle(new Point(65, 220), label6.getPreferredSize()));
     
    		//---- button1 ----
    		button1.setText("Calculate");
    		button1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				calculateButtonPressed(e);
    			}
    		});
    		contentPane.add(button1);
    		button1.setBounds(55, 265, 110, button1.getPreferredSize().height);
    		contentPane.add(label7);
    		label7.setBounds(10, 300, 185, 15);
     
    		{ // compute preferred size
    			Dimension preferredSize = new Dimension();
    			for(int i = 0; i < contentPane.getComponentCount(); i++) {
    				Rectangle bounds = contentPane.getComponent(i).getBounds();
    				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
    				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
    			}
    			Insets insets = contentPane.getInsets();
    			preferredSize.width += insets.right;
    			preferredSize.height += insets.bottom;
    			contentPane.setMinimumSize(preferredSize);
    			contentPane.setPreferredSize(preferredSize);
    		}
    		pack();
    		setLocationRelativeTo(getOwner());
    		// JFormDesigner - End of component initialization  //GEN-END:initComponents
    	}
     
    	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    	// Generated using JFormDesigner Open Source Project license - unknown
    	private JComboBox comboBox1;
    	private JLabel label2;
    	private JTextField textField1;
    	private JLabel label3;
    	private JLabel label4;
    	private JTextField textField2;
    	private JLabel label5;
    	private JTextField textField3;
    	private JTextField textField4;
    	private JLabel label6;
    	private JButton button1;
    	private JLabel label7;
    	// JFormDesigner - End of variables declaration  //GEN-END:variables
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Number Format Exception

    It helps to post the full error message, and trim your code down to an SSCCE. A NumberFormatExceptions is thrown by Number object parsers (such as the Double.parseDouble method you use) if the parameter is not a number. Add some println statements to see what value the parameter you pass to this method

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Number Format Exception

    I'm sorry here is the error
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at resale_programs.resaleGUI.getQuantity(minToSellGUIVERSION.java:93)
    	at resale_programs.resaleGUI.calculateButtonPressed(minToSellGUIVERSION.java:48)
    	at resale_programs.resaleGUI.access$0(minToSellGUIVERSION.java:46)
    	at resale_programs.resaleGUI$1.actionPerformed(minToSellGUIVERSION.java:162)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    i tried throwing in some println's in there before it called each of the textfield values and i am not getting a response

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Number Format Exception

    also i changed some code and tried again, this is the new code and the new error
    	private void amountToSell() {
    		System.out.println(textField4.toString());
    		amountToSell = Integer.parseInt(textField4.getText().toString());
    	}
     
    	private void amountPerSale() {
    		System.out.println(textField3.toString());
    		amountPerSale = Double.parseDouble(textField3.getText().toString());
    	}
     
    	private void pricePerLot() {
    		System.out.println(textField2.toString());
    		pricePerLot = Double.parseDouble(textField2.getText().toString());
    	}
     
    	private void getQuantity() {
    		System.out.println(textField1.toString());
    		quantity = Integer.parseInt(textField1.toString());
    	}

    here is the new error
    Exception in thread "AWT-EventQueue-0" javax.swing.JTextField[,65,100,85x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1050169,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]
    java.lang.NumberFormatException: For input string: "javax.swing.JTextField[,65,100,85x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1050169,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]"
    	at java.lang.NumberFormatException.forInputString(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at resale_programs.resaleGUI.getQuantity(minToSellGUIVERSION.java:93)
    	at resale_programs.resaleGUI.calculateButtonPressed(minToSellGUIVERSION.java:48)
    	at resale_programs.resaleGUI.access$0(minToSellGUIVERSION.java:46)
    	at resale_programs.resaleGUI$1.actionPerformed(minToSellGUIVERSION.java:162)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Number Format Exception

    at java.lang.Integer.parseInt(Unknown Source)
    at resale_programs.resaleGUI.getQuantity(minToSellGUI VERSION.java:93)
    Can you find the line where the error occurs? What code is on line 93?
    What is the argument to the parseInt() method? Is it a String of digits like: "123" or something else?

    Look at the code and see what is different about that line compared to the other lines above it that appear to work.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Number Format Exception

    Why are you creating resaleGUI twice? I can see why you create it in the main method, but the reason you are getting these errors is because you are creating a new resaleGUI in the calculateButtonPressed action and trying to convert text from its fields before it is even displayed.

    If you remove the line that creates a new resaleGUI in the calculateButtonPressed action and call the subsequent methods on the current resaleGUI object instead, the code will work (kind of).

    Among other things, your GUI creation code in the main method is not good. Check out the Java Tutorial HelloWorld example.

Similar Threads

  1. Number Format Exception while parsing long
    By Aamir in forum Java Networking
    Replies: 1
    Last Post: May 19th, 2011, 03:59 AM
  2. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. How to format?
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 01:07 PM
  5. [SOLVED] Help with code, Says number is NaN and i cant format it
    By NewGuy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2010, 08:28 PM