Results 1 to 14 of 14
  1. #1
    RattleSnake is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default 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:
    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.");
    	}
    
    		}
    }
    VerifyName.java:
    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();
    	}
    	
    
    }
    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:

    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..

    Weight determined! It is indeed Hazim!
    Weight determined. Person is too fat.
    Why :(

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    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
    ,
    Java Code:
    if (weight == 100) {
    	System.out.println("Weight determined. Person is too fat.");
    }
    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    RattleSnake is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Great - thanks a lot, works like a charm.

    Another question..

    How can I combine these two statements:

    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.");
    	}
    
    		}
    To an else-if method?

    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.");
    		}

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    if(condition){
      Statements
    }
    else if(condition){
      Statements
    }

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

    Default

    Quote Originally Posted by sunde887 View Post
    [code]
    if(Condio
    Statements
    I've got it: you're typing this on your cell phone; clean your keypad ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    I've got it: you're typing this on your cell phone; clean your keypad ;-)

    kind regards,

    Jos
    Lol, indeed, accidentally hit the done button and it removed the keyboard and I hit submit. Damn iPhone keyboard.

  9. #9
    RattleSnake is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    I was playing around and I removed a bracket and this seems to work well..

    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.");
    		}
    However, howcome this method compiles fine and there's a bracket with the else if?

    Java Code:
    	void checkPerson() {
    	if (!name.equals("Hazim")) {
    		isHazim = false;
    		System.out.println("Imposter detected!");
    	} else if (name.equals("Hazim")) {
    		isHazim = true;
    		}
    	
    	}

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  11. #11
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by sunde887 View Post
    It may be a good habit to wrap all of your conditionals statements in blocks.
    Rightly said!

    OP,

    Have a look at this nice article which will make the things more clear for you.

    Working with if-else statements

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  12. #12
    RattleSnake is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    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:

    Java Code:
    void FinalResult() {
    if (name == Hazim) & (age == 15) & (weight = 140)
    System.out.println("Final result determined! It is Hazim!)"
    }
    How could I do that?

  13. #13
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    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,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    Quote Originally Posted by sunde887 View Post
    Lol, indeed, accidentally hit the done button and it removed the keyboard and I hit submit. Damn iPhone keyboard.
    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,

    Jo
    Last edited by JosAH; 04-25-2011 at 05:11 PM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. A simple problem that i can't fix
    By baf06 in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 03:50 PM
  2. Problem with simple code in Java ME SDK 3.0
    By marvas in forum CLDC and MIDP
    Replies: 0
    Last Post: 10-13-2010, 07:58 PM
  3. Very simple problem
    By Andyj in forum Java Certification
    Replies: 3
    Last Post: 09-17-2010, 07:49 AM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  5. problem with a simple java code
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 03:46 AM

Posting Permissions

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