Results 1 to 10 of 10
  1. #1
    Tamtome is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Please need help asap

    I need to make a program that can count how much money you get back in different money notes, i get this error: Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Uppgift222.main(Uppgift222.java:32)

    can somebody please help me i dont have that much time :(


    Java Code:
    import java.util.Scanner;
    public class Uppgift222 {
    public static void main(String args[]){
    		
    		Scanner räkna = new Scanner(System.in);
    		int summa,betalade,pengar, fåtillbaka;
    		System.out.println("Var snäll och ange summan på varan: ");
    		summa = räkna.nextInt();
    		System.out.println("Summan: " + summa);
    		System.out.println("Hur mycket betalade du: ");
    		betalade = räkna.nextInt();
    		System.out.println("Betalade: " + betalade);
    		fåtillbaka = betalade - summa;
    		System.out.println("Att få tillbaka : " + fåtillbaka);
    		
    		int hundralapp = 0;//100 kr
    		int femtiolapp = 0;// 50 kr
    		int tjugor = 0;//20 kr
    		int tior = 0;//10 kr
    		int femmor = 0;//5 kr
    		int enkronor = 0;//1 kr
    		
    		
    	hundralapp=fåtillbaka/100;
    	femtiolapp = (fåtillbaka%(hundralapp*100))/50;
    	tjugor = (fåtillbaka%(femtiolapp*50))/20;
    	tior = (fåtillbaka%(tjugor*20))/10;
    	femmor = (fåtillbaka%(tior*50))/5;
    	enkronor = (fåtillbaka%(femmor*5))/1;
    	
    	System.out.println("så här många hundralappar " + hundralapp);
    	System.out.println("så här många femtiolappar " + femtiolapp);
    	System.out.println("så här många tjugolappar " + tjugor);
    	System.out.println("så här många tior " + tior);
    	System.out.println("så här många femmor " + femmor);
    	System.out.println("så här många enkronor " + enkronor);
    	}
    }

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Java Code:
    femtiolapp = (fåtillbaka%(hundralapp*100))/50;
    Whenever hundralapp is 0, this will cause a runtime error. You want either an if statement or a ?: operation to make sure it does the right thing with a denominator of zero.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Tamtome View Post
    I need to make a program that can count how much money you get back in different money notes, i get this error: Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Uppgift222.main(Uppgift222.java:32)

    can somebody please help me i dont have that much time :(
    I don't have much time either so you should print the value of each variable before you do any calculation with it and see which one equals zero.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    7

    Default

    in java, when you do something like that

    System.out.println(100 % 0);

    you will get the exception java.lang.ArithmeticException: / by zero. so check if one of the reusult of your terms is 0.

  5. #5
    Tamtome is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    thanks for the replies! j2me64, how do i check that?

  6. #6
    Tamtome is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    this line is giving me error: tjugor = (fåtillbaka%(femtiolapp*50))/20;

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Tamtome View Post
    this line is giving me error: tjugor = (fåtillbaka%(femtiolapp*50))/20;
    If it was an Arithmetic error, variable 'femtiolapp' was zero. Did you implement my suggestion and print all variables? If so, you could've seen what was wrong.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  8. #8
    Tamtome is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    jos, im trying to use this: if (!(hundralapp<= 0)) {

    but now im having problems with they money notes, do u know how i can fix it?

    Java Code:
    import java.util.Scanner;
    public class Test222 {
    public static void main(String args[]){
    		
    		Scanner räkna = new Scanner(System.in);
    		int summa,betalade,pengar, fåtillbaka;
    		System.out.println("Var snäll och ange summan på varan: ");
    		summa = räkna.nextInt();
    		System.out.println("Summan: " + summa);
    		System.out.println("Hur mycket betalade du: ");
    		betalade = räkna.nextInt();
    		System.out.println("Betalade: " + betalade);
    		fåtillbaka = betalade - summa;
    		System.out.println("Att få tillbaka : " + fåtillbaka);
    		
    		int hundralapp = 0;
    		int femtiolapp = 0;
    		int tjugor = 0;
    		int tior = 0;
    		int femmor = 0;
    		int enkronor = 0;
    		
    		if (!(hundralapp<= 0)) {	
    	hundralapp=fåtillbaka/100;
    		
    	femtiolapp = (fåtillbaka%(hundralapp*100))/50;
    	tjugor = (fåtillbaka%(femtiolapp*50))/20;
    	tior = (fåtillbaka%(tjugor*20))/10;
    	femmor = (fåtillbaka%(tior*50))/5;
    	enkronor = (fåtillbaka%(femmor*5))/1;
    		}
    	System.out.println("så här många hundralappar " + hundralapp);
    	System.out.println("så här många femtiolappar " + femtiolapp);
    	System.out.println("så här många tjugolappar " + tjugor);
    	System.out.println("så här många tior " + tior);
    	System.out.println("så här många femmor " + femmor);
    	System.out.println("så här många enkronor " + enkronor);
    		
    }
    
    	
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Tamtome View Post
    jos, im trying to use this: if (!(hundralapp<= 0)) {

    but now im having problems with they money notes, do u know how i can fix it?
    What are those 'problems'? Please be more explicit and don't make us guess.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  10. #10
    Tamtome is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    im sorry i didnt mean to! im gonna try to explain, now it only shows how much money you get back but not how many money notes, instead of showing how much change u get it only shows zero. it prints like this


    Var snäll och ange summan på varan:
    300
    Summan: 300
    Hur mycket betalade du:
    500
    Betalade: 500
    Att få tillbaka : 200
    så här många hundralappar 0
    så här många femtiolappar 0
    så här många tjugolappar 0
    så här många tior 0
    så här många femmor 0
    så här många enkronor 0

Similar Threads

  1. Need help ASAP, please
    By KAM0002 in forum New To Java
    Replies: 6
    Last Post: 12-08-2010, 05:58 PM
  2. Need help asap please!
    By mbm4ever in forum Java Applets
    Replies: 4
    Last Post: 08-12-2010, 03:57 PM
  3. Need some help ASAP
    By varma in forum New To Java
    Replies: 11
    Last Post: 01-08-2010, 01:15 AM
  4. Need answer asap!!!
    By uranis_khai in forum New To Java
    Replies: 3
    Last Post: 07-07-2009, 09:48 AM
  5. Need Java Help ASAP
    By L-dog in forum New To Java
    Replies: 22
    Last Post: 10-02-2008, 11:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •