Results 1 to 14 of 14
- 04-25-2011, 02:22 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Very new to Java, simple problem..
Hi guys,
I just started learning Java last night and based on what I learned I tried to make a program.. It works, but it doesn't work.
Here's the source code (using Eclipse, if it matters):
WeightChecker.java:
VerifyName.java:Java Code:class WeightChecker { int age; int weight; boolean isHazim; String name = "Hazim"; void checkPerson() { if (!name.equals("Hazim")) { isHazim = false; System.out.println("Imposter detected!"); } else if (name.equals("Hazim")) { isHazim = true; } } void checkWeight() { if (weight == 10); System.out.println("Weight determined! It is indeed Hazim!"); } void checkWeight2() { { if (weight == 100); System.out.println("Weight determined. Person is too fat."); } } }
So.. I initiated an instance, starting at 10,000 weight, and then I called the checkWeight and apparently, if the weight is equals to 10, then it should print:Java Code:class VerifyName { public static void main(String[] arguments) { WeightChecker hazim = new WeightChecker(); hazim.isHazim = false; hazim.name = "Hazim"; hazim.weight = 10000; hazim.checkPerson(); hazim.checkWeight(); hazim.checkWeight2(); } }
Weight determined! It is indeed Hazim!
But if the weight equals to 100, then it should print:
Weight determined. Person is too fat.
However, when I ran the script in Eclipse, this was my output..
Why :(Weight determined! It is indeed Hazim!
Weight determined. Person is too fat.
- 04-25-2011, 02:34 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
When you type in your ten thousand, don't type a comma, i.e. 10000 instead of 10,000; those commas are Locale bounded.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2011, 02:41 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Don't add a semicolon after your if statements...it causes the conditional to terminate, and the next line will be evaluated independent of the conditional if statement.
- 04-25-2011, 02:41 PM #4
You are ending your if statements with a semicolon ; which makes the next print statements as standalone statements. And thats why they print just as if they were called directly. There is no weight validation there.
Have an habit of using curly braces with your if statements. Like,
Hope that helps,Java Code:if (weight == 100) { System.out.println("Weight determined. Person is too fat."); }
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 04-25-2011, 03:23 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Great - thanks a lot, works like a charm.
Another question..
How can I combine these two statements:
To an else-if method?Java Code:void checkWeight() { if (weight <= 141) System.out.println("Weight determined! It is indeed Hazim!"); } void checkWeight2() { { if (weight >= 140) System.out.println("Weight determined. Person is too fat."); } }
I tried this but Eclipse says it's wrong.. :(
Java Code:void checkWeight() { if (weight <= 141) System.out.println("Weight determined! It is indeed Hazim!"); else if { (weight >= 140) System.out.println("Weight determined. Person is too fat."); }
- 04-25-2011, 03:25 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Java Code:if(condition){ Statements } else if(condition){ Statements }
- 04-25-2011, 03:27 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 04-25-2011, 03:28 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
- 04-25-2011, 03:29 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
I was playing around and I removed a bracket and this seems to work well..
However, howcome this method compiles fine and there's a bracket with the else if?Java Code:void checkWeight() { if (weight <= 141) System.out.println("Weight determined! It is indeed Hazim!"); else if (weight >= 140) System.out.println("Weight determined. Person is too fat."); }
Java Code:void checkPerson() { if (!name.equals("Hazim")) { isHazim = false; System.out.println("Imposter detected!"); } else if (name.equals("Hazim")) { isHazim = true; } }
- 04-25-2011, 03:31 PM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Every clause in the if else clause has a statement(or a statement block). It may be a good habit to wrap all of your conditionals statements in blocks.
- 04-25-2011, 03:39 PM #11
Rightly said!
OP,
Have a look at this nice article which will make the things more clear for you.
Working with if-else statements
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 04-25-2011, 03:43 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Awesome! That made things a lot clearer.
I have one more question.. I'm not sure what it's called.. But what is the "and" in Java?
Like I want to do:
How could I do that?Java Code:void FinalResult() { if (name == Hazim) & (age == 15) & (weight = 140) System.out.println("Final result determined! It is Hazim!)" }
- 04-25-2011, 03:51 PM #13
You are talking about bitwise AND operator "&"
Read some good documentation of Java operators, there are many.
Here is one good link : Java Operators Tutorial
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 04-25-2011, 04:06 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Stupid iPhones; I don't want to type on a vacuum cleaner either; those devices aren't made for the job; here, have a look at this real device. Now that's what I call a device.
kind regards,
JoLast edited by JosAH; 04-25-2011 at 04:11 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
A simple problem that i can't fix
By baf06 in forum New To JavaReplies: 3Last Post: 04-06-2011, 02:50 PM -
Problem with simple code in Java ME SDK 3.0
By marvas in forum CLDC and MIDPReplies: 0Last Post: 10-13-2010, 06:58 PM -
Very simple problem
By Andyj in forum Java CertificationReplies: 3Last Post: 09-17-2010, 06:49 AM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
problem with a simple java code
By boy22 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks