Results 1 to 17 of 17
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Post Need some help debugging: Keeps return 0 as a value!

    Hello All,

    so I'm trying to run this little code to calculate a given value and unfortunately it keeps returning 0 as the value. So I created classes: One that is the main class and the other being where I define my method, etc...

    So Here is the one containing the method:

    Java Code:
    //Define class
    public class TieTheKnot {
    
    	public int T;//How many Years have you been dating
    	public int L;//Number of times a day that something makes you think of other person
    	public int C;//If families get together, #of times there will be uncomfortable friction
    	public int S;//How many Shared interests/goals you two have?
    	public int A;//How many individual or conflicting interests and/or goals do you two have?
    	public int D;//Average # of disagreements you have with this person in a month
    
    // Constructor-catches variables from the object & passes it on to the variables used in the class containing
    // the method declaration
    
    public TieTheKnot (int T, int L, int C, int S, int A, int D) {
    
    	this.T = T;
    	this.L = L;
    	this.C = C;
    	this.S = S;
    	this.A = A;
    	this.D = D;
    }
    
    
    public int tieKnot() {
    
    	return (S/A)*(L/(D+1))+((T^2)/(3*(C+1)));
    	}
    
    }
    and here is my main class

    Java Code:
      
    import java.io.*;
    import java.util.Scanner;
    
       public class MarriageEquations{
       
          public static void main (String[] args) throws IOException 
          {
          
             int question1 = 1;
             int question2 = 2;
             int question3 = 3;
    			
    		    
          //Ask users whether they want to know the play Marriage Equations game
          //Ask users for his/her name
             System.out.println("Welcome and thank you for playing the love equation game.  What is your name?"); 
          
          //This is how you read from the keyboard.  
          //1st We create the object that will read from keyboard
             Scanner myScanner = new Scanner(System.in);
          
          //We can now declare the String that the users will put in
             String userName = myScanner.next();
          
          //Greeting
             System.out.println("Hello "+ userName +".");
             System.out.println("There are three questions, please read them and decide which one you would like to do first.");
             System.out.println("1. Should I get married?");
             System.out.println("2. What are the chances that my marriage will last?");
             System.out.println("3. How many kids hsould I have?");
             System.out.println();
             System.out.print("Now that you have read them, please choose an answer by typing \"1\" for the first, \"2\" for the second one ");
             System.out.print("and \"3\" for the last one.");
             System.out.println();
             int Answer = myScanner.nextInt();
             System.out.println("You chose: "+Answer+".");
          
             if (question1 == Answer) System.out.println("it works");
          
             switch (Answer) {
                case 1:	
                   System.out.println("You chose to ask Whether you should I get married?");
                   System.out.println();
                   System.out.println("Please answer the following questions!");
                   System.out.println("How many Years have you been dating");
                   int T = myScanner.nextInt();//
                   System.out.println("Number of times a day that something makes you think of other person");
                   int L = myScanner.nextInt();//
                   System.out.println("If families get together, #of times there will be uncomfortable friction");
                   int C = myScanner.nextInt();//
                   System.out.println("How many Shared interests/goals you two have?");
                   int S = myScanner.nextInt();//
                   System.out.println("How many individual or conflicting interests and/or goals do you two have?");
                   int A = myScanner.nextInt();//
                   System.out.println("What is the number of disagreements you have with this person in a month?");
                   int D = myScanner.nextInt();//
                   System.out.println("Thank you.  We are now computing your answers...");
    					System.out.println();
    			
    					//Initialize objects or create objects
    					TieTheKnot knots = new TieTheKnot( T, L, C, S, A, D);
    					
    					System.out.println(knots.tieKnot());
      
                   break;
                case 2:
    So when I run this as is, I keep getting
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at TieTheKnot.tieKnot(TieTheKnot.java:29)
    at MarriageEquations.main(MarriageEquations.java:63)
    I don't understand why it's returning 0 as a value or why it's not passing the values collected from the user and giving me a non 0 value.

    Thank you for your help. Any suggestions on syntax is welcome as well!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hm, I didn't get the error when I ran it, but perhaps it's because I entered different input (after all, I've just celebrated my 25th anniversary).

    So in what conditions do you get the divide by zero exception? And do you understand that you are doing int division and what this means?

    Oh, and by the way, a big welcome to the Java forum!!!! Hope to see you around, to see you post interesting questions, and even more, to see you post clever solutions to the questions of others.

    Much luck!

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Right here...
    //Initialize objects or create objects

    Add this line:
    System.out.println("T="+T+", L="+L+", C="+C+", S="+S+", A="+A+", D="+D);

    That way you can see what the values are and make the calculation yourself to see where the /0 is.

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Hello Fubarable,

    What do you mean by what conditions?
    some Background I guess. I am using JGrasp for my compiling. As far as values, I type random numbers, so it's pretty straight forward.

    and to your second question, no I don't know the implications. Should I use double?

  5. #5
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Thanks for the tip. I just tried it and I don't get a zero answer when i do it manually, so what gives?

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    What were the numbers given?

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Okay, so now I'm not getting the error, I'm just getting 0 as a return value!

  8. #8
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by zack View Post
    what were the numbers given?
    t=3, l=3, c=8, s=3, a=10, d=20

  9. #9
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Now, as Fubarable mentioned, you need to familiarize yourself with Integer division. L/(D+1), for example, is 3/21. In Integer divison, this is 0 (rounded DOWN to the nearest Integer value). 3.0/21.0, however (Double or Float division), would produce 0.142857. If this is what you want, you need to store your numbers as doubles or cast them to such during the equation.

  10. #10
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Zack View Post
    Now, as Fubarable mentioned, you need to familiarize yourself with Integer division. L/(D+1), for example, is 3/21. In Integer divison, this is 0 (rounded DOWN to the nearest Integer value). 3.0/21.0, however (Double or Float division), would produce 0.142857. If this is what you want, you need to store your numbers as doubles or cast them to such during the equation.
    Thanks. So I should store them as float or double? Also, how would I cast that?
    Last, though I am familiar with the primitive data types and now that you mention it, it's pretty obvious, is there a place I could go to look that up specifically discussion divisions or is this just something I should have remembered from the primitive data types and their definitions, hence the implications?

  11. #11
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    There isn't really a repository of information regarding it. What's important to remember is that the kept data type is the broadest data type. As an example, think of: 3+5.0+"6". In this case, we start with 3, then add 5.0 to become 8.0, then add "6" to become "8.06". Our result is a String because of the three data types, Strings are the broadest (store the most information). In your case, integers are all integers, which means the result has to be an integer. Start including doubles one-at-a-time and you can see the effect.

    For casting, you can just place (double) in front of the variable. I would say instead to store them as doubles though. That just makes life easy. :)

  12. #12
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Zack View Post
    There isn't really a repository of information regarding it. What's important to remember is that the kept data type is the broadest data type. As an example, think of: 3+5.0+"6". In this case, we start with 3, then add 5.0 to become 8.0, then add "6" to become "8.06". Our result is a String because of the three data types, Strings are the broadest (store the most information). In your case, integers are all integers, which means the result has to be an integer. Start including doubles one-at-a-time and you can see the effect.

    For casting, you can just place (double) in front of the variable. I would say instead to store them as doubles though. That just makes life easy. :)
    Thanks, I'll do that. So when is it the right time to use int?

  13. #13
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Okay, so now I'm getting two new errors:

    MarriageEquations.java:40: possible loss of precision
    found : double
    required: int
    switch (Answer) {
    ^
    TieTheKnot.java:29: operator ^ cannot be applied to double,int
    return ((S/A)*(L/(D+1))+((T^2)/(3*(C+1))));
    ^
    2 errors

  14. #14
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Never mind. I just realize that a switch only works with certain data type, excluding double...any tips in this case? How should I handle this?

  15. #15
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Convert the final answer to an integer. I expect you will be rounding it anyway.

    PS: You don't want to use ^ for power. ^ is the XOR operation. Use Math.pow() instead. ;)

  16. #16
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Zack View Post
    Convert the final answer to an integer. I expect you will be rounding it anyway.

    PS: You don't want to use ^ for power. ^ is the XOR operation. Use Math.pow() instead. ;)
    Thanks for the tip on the second one. I'll do that. With respect to converting the final answer to an integer, if I do that, won't I be back to getting a zero again, if the answer is less than 1 but greater than 0 (e.g. 0.123232)?

  17. #17
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Never mind, I understood what you meant. Thanks Zach. This was very helpful. This finally spit out my answer. Sweet! Now I"m going to continue studying so I can figure out how to handle my integers! thanks!

Similar Threads

  1. help debugging
    By mluu510 in forum New To Java
    Replies: 3
    Last Post: 08-21-2010, 01:28 PM
  2. Debugging
    By daro in forum Eclipse
    Replies: 0
    Last Post: 07-22-2009, 05:02 PM
  3. Debugging In NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM

Tags for this Thread

Posting Permissions

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