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

Thread: Program crashes when user input's decimals

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program crashes when user input's decimals

     
    import javax.swing.*; // import the swing library for I/O
     
    class change
    {
        public static void main (String[] param)
        {
     
          	changedue();
            System.exit(0);
     
        } // END main
     
    	public static void changedue()
    	{
    	String textinput;
    	String textinput1;
    	String value1;
    	int sale = 0;
    	int amount = 0;
    	int chnge = 0;
    	int f, twenty, ten, five, one;
    	int price;
    	int cost = 0;
    	int change1;
     
       	f = 0;
        	twenty = 0;
        	ten = 0;
        	five = 0;
        	one = 0;
     
    	{
    	textinput = JOptionPane.showInputDialog("Please enter the amount of the sale");
    	cost = Integer.parseInt(textinput);
    	textinput1 = JOptionPane.showInputDialog(null, "Please enter the amount the customer paid in");
    	price = Integer.parseInt(textinput1);
    	change1 = (price - cost) * 100; 
     
    	System.out.println(change1);
     
        if (change1 >= 50)
        {
    	f = change1 / 50;
    	double d = Math.floor(f);  
    	change1 = change1 - (f * 50); 
        } 
        while (change1 >= 20)
        {
            twenty = change1 / 20;
    	double d = Math.floor(twenty);
            change1 = change1 - (twenty * 20);
        }
        while (change1 >= 10)
        {
    	ten = change1 / 10;
    	double d = Math.floor(ten);
            change1 = change1 - (ten * 10);
        }
        while (change1 >= 5)
        {
    	five = change1 / 5;
    	double d = Math.floor(five);
            change1 = change1 - (five * 5);
        }
        while (change1 >= 1)
        {
     
    	one = change1 / 1;
    	double d = Math.floor(one);
            change1 = change1 - (one * 1);
        }
    	System.out.println("number of one penny's" + one);
    	System.out.println("number of five penny's" + five);
    	System.out.println("number of ten penny's" + ten);
    	System.out.println("number of twenty penny's" + twenty);
    	System.out.println("number of fifty penny's" + f);
    }
    		}
         // END changedue
     
     
    } // END class change


    I get this error whenever I input a decimal number like 9.50, why? how can i stop this :S

    Terminal spits out this when i do
    Exception in thread "main" java.lang.NumberFormatException: For input string: "9.50"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at change.changedue(change.java:36)
    at change.main(change.java:9)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Program crashes when user input's decimals

    Look at the Double class for a method to parse decimal numbers. The Integer class's parseInt() method is for integer/whole numbers (digits only)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program crashes when user input's decimals

    	public static void changedue()
    	{
    	String textinput;
    	String textinput1;
    	String value1;
    	int sale = 0;
    	int amount = 0;
    	int chnge = 0;
    	int f, twenty, ten, five, one;
    	double price;
    	double cost = 0;
    	int change1;
     
       	f = 0;
        	twenty = 0;
        	ten = 0;
        	five = 0;
        	one = 0;
     
    	{
    	textinput = JOptionPane.showInputDialog("Please enter the amount of the sale");
    	cost = Double.parseDouble(textinput);
    	textinput1 = JOptionPane.showInputDialog(null, "Please enter the amount the customer paid in");
    	price = Double.parseDouble(textinput1);
    	change1 = (price - cost) * 100;

    I added Double.parseDouble(textinput1); instead of the Interger parse...was this what you meant? and i changed price/cost from int to double
    but i get this error now

    error: possible loss of precision
    change1 = (price - cost) * 100;
    ^
    required: int
    found: double

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Program crashes when user input's decimals

    error: possible loss of precision
    The compiler is saying that if you assign a double like 9.5 to an int variable you will lose the 0.5 part because the int variable will hold 9.
    If that is what you want to happen, then use a cast to int to tell the compiler that is what you want to do:
       int anInt = (int)9.5;    // cast double to int
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: May 9th, 2013, 03:31 PM
  2. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  3. Getting program to ask user input three times
    By Java_noob333 in forum Object Oriented Programming
    Replies: 5
    Last Post: February 24th, 2013, 08:03 AM
  4. user input program using BufferedReader..
    By mbalamula in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 22nd, 2013, 06:46 AM
  5. How to make an error message when my program crashes?
    By noFear in forum Java Theory & Questions
    Replies: 10
    Last Post: August 11th, 2010, 08:50 AM