Page 1 of 2 12 LastLast
Results 1 to 20 of 34
Like Tree6Likes

Thread: Using if to print words

  1. #1
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Using if to print words

    I am trying to make it so that 0 is the equivalent to a closed door and 1 is the equivalent to open. My problem is that I have it set at the moment to print 0 or 1 depending on which Demo I run but I am looking for a way for it to print Closed if the value is 0, and open if the value is 1. Can some help me out please. :D

    Java Code:
    class Car {
    
    	int gear = 0;
    	int speed = 0;
    	int RFdoor = 0;
    	int LFdoor = 0;
    	int boot = 0;
    	int bonet = 0;
    	
    	void changeGear(int newValue){
    		gear = newValue;
    	}
    	
    	void speedUp(int increment) {
    		speed = speed + increment;
    	}
    	
    	void applyBrakes(int decrement) {
    		speed = speed - decrement;
    	}
    	
    	void openRFdoor(int increment) {
    		RFdoor = RFdoor + increment;
    	}
    	
    	void closeRFdoor(int decrement) {
    		RFdoor = RFdoor - decrement;
    	}
    	
    	void openLFdoor(int increment) {
    		LFdoor = LFdoor + increment;
    	}
    	
    	void closeLFdoor(int decrement) {
    		LFdoor = LFdoor - decrement;
    	}
    	
    	void openBoot(int increment) {
    		boot = boot + increment;
    	}
    	
    	void closeBoot(int decrement) {
    		boot = boot - decrement;
    	}
    	
    	void openBonet(int increment) {
    		bonet = boot + increment;
    	}
    	
    	void closeBonet(int decrement) {
    		bonet = bonet - decrement;
    	}
    	
    	void PrintStates() {
    		System.out.println("Gear:" + gear + 
    		"   Speed:" + speed + 
    		"   Right-Front Door:" + RFdoor + 
    		"   Left-Front Door:" + LFdoor + 
    		"   Boot:" + boot + 
    		"   Bonet:" + bonet);
    		}
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,876
    Rep Power
    25

    Default Re: Using if to print words

    Write a small method that takes the value as an argument and returns a String: "Closed" if the value is 0, and "open" if the value is 1
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    Quote Originally Posted by Norm View Post
    Write a small method that takes the value as an argument and returns a String: "Closed" if the value is 0, and "open" if the value is 1
    Would you be able to write me an example of such code as I am relatively new to Java and I am unsure how to do what you are saying.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,876
    Rep Power
    25

    Default Re: Using if to print words

    Take a look at the tutorial:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    What part don't you understand:
    write a method that returns a String
    pass it the value as an argument
    test the value of the argument and chose a String to return
    return a String
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    Could you possibly give me an example as I am not sure how to use this in my situation.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,876
    Rep Power
    25

    Default Re: Using if to print words

    Do you know what a method is? Have you called a method in any of your code?
    The posted code has many methods defined in it. None of them return a value.
    Here's how you'd have one return a value:
    Java Code:
        int speedUp(int increment) {
            speed = speed + increment;   //  compute the new speed
            return speed;                      //  return the new speed
        }
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    Quote Originally Posted by TANKDS View Post
    Could you possibly give me an example as I am not sure how to use this in my situation.
    Do you not understand if-statements, or do you not understand how to define a method?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  8. #8
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    Quote Originally Posted by Norm View Post
    Do you know what a method is? Have you called a method in any of your code?
    The posted code has many methods defined in it. None of them return a value.
    Here's how you'd have one return a value:
    Java Code:
        int speedUp(int increment) {
            speed = speed + increment;   //  compute the new speed
            return speed;                      //  return the new speed
        }
    Why would I need to get a return of speed?

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    Quote Originally Posted by TANKDS View Post
    Why would I need to get a return of speed?
    I think he was just demonstrating how to return a value.

    Can you clarify what you don't understand?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  10. #10
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    All i want is to implement the if statement so that when I run my CarDemo.class file rather than returning a 0 or 1 it will return closed or open.

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    Quote Originally Posted by TANKDS View Post
    All i want is to implement the if statement so that when I run my CarDemo.class file rather than returning a 0 or 1 it will return closed or open.
    Then this is what you should read: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,876
    Rep Power
    25

    Default Re: Using if to print words

    it will return closed or open.
    What method in the CarDemo class is returning a value? I don't see any methods in the posted code that return a value.
    Do you mean prints the value?

    How much of the tutorial at the link I posted did you read? There is a lot of material there on methods. For example:
    http://docs.oracle.com/javase/tutori...turnvalue.html
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    I use System.out.println to get a value back in the CarDemo.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,876
    Rep Power
    25

    Default Re: Using if to print words

    to get a value back
    It's a vocabulary problem.
    The println method does NOT return any value. It writes a String to the output target which is the console.
    A method uses the return statement to return a value.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    I may have confused you, I use println to show me the value of the gears etc once the Demo had ran. My problem is that Right-Front Door etc are being showed as 0 or 1 and I want to the print to show them as open or closed, I am trying to use the if and else if to help my println show closed and not 0.

  16. #16
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Using if to print words

    Quote Originally Posted by TANKDS View Post
    My problem is that Right-Front Door etc are being showed as 0 or 1 and I want to the print to show them as open or closed, I am trying to use the if and else if to help my println show closed and not 0.
    Ok, so you want to use an if-else statement.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  17. #17
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    Quote Originally Posted by awinston View Post
    Ok, so you want to use an if-else statement.
    I have tried to implement an if else statement but I keep getting a lot of errors, 20 at the moment. I was wondering if you could give me an example of what code I should use where because this seems to not currently be working correctly. :(

  18. #18
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Using if to print words

    you can use an inline if:

    Java Code:
    "   Right-Front Door:" + ((RFdoor == 0) ? "Closed" : "Open") +
    TANKDS likes this.

  19. #19
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default Re: Using if to print words

    What errors are you getting when you try the if-else statement?

  20. #20
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Using if to print words

    Quote Originally Posted by .paul. View Post
    you can use an inline if:

    Java Code:
    "   Right-Front Door:" + ((RFdoor == 0) ? "Closed" : "Open") +
    You Sir, are an absolute genius! Could you briefly explain the code possibly. It worked and did everything that I wanted it to. I just need to know how it works please. :)

Page 1 of 2 12 LastLast

Similar Threads

  1. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 01:14 AM
  2. Need to find a way to print line spacing by words
    By technoreject in forum New To Java
    Replies: 1
    Last Post: 07-07-2011, 03:43 PM
  3. Replies: 3
    Last Post: 02-13-2011, 09:54 PM
  4. How to print words in different colours?
    By veen in forum New To Java
    Replies: 7
    Last Post: 06-10-2010, 05:54 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM

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
  •