Results 1 to 17 of 17
- 11-08-2010, 05:00 AM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
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:
and here is my main classJava 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))); } }
So when I run this as is, I keep gettingJava 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:
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.Exception in thread "main" java.lang.ArithmeticException: / by zero
at TieTheKnot.tieKnot(TieTheKnot.java:29)
at MarriageEquations.main(MarriageEquations.java:63)
Thank you for your help. Any suggestions on syntax is welcome as well!
-
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!
- 11-08-2010, 05:10 AM #3
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.
- 11-08-2010, 05:12 AM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
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?
- 11-08-2010, 05:16 AM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Thanks for the tip. I just tried it and I don't get a zero answer when i do it manually, so what gives?
- 11-08-2010, 05:17 AM #6
- 11-08-2010, 05:17 AM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Okay, so now I'm not getting the error, I'm just getting 0 as a return value!
- 11-08-2010, 05:18 AM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 11-08-2010, 05:29 AM #9
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.
- 11-08-2010, 05:33 AM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
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-08-2010, 05:37 AM #11
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. :)
- 11-08-2010, 05:44 AM #12
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 11-08-2010, 05:45 AM #13
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
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
- 11-08-2010, 05:50 AM #14
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
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?
- 11-08-2010, 06:17 AM #15
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. ;)
- 11-08-2010, 02:36 PM #16
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 11-08-2010, 02:53 PM #17
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Similar Threads
-
help debugging
By mluu510 in forum New To JavaReplies: 3Last Post: 08-21-2010, 01:28 PM -
Debugging
By daro in forum EclipseReplies: 0Last Post: 07-22-2009, 05:02 PM -
Debugging In NetBeans IDE
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks