Blank Line

Discussion in 'Programming General' started by Draucia, Nov 27, 2009.

Blank Line
  1. Unread #1 - Nov 27, 2009 at 3:43 PM
  2. Draucia
    Referrals:
    0

    Draucia Guest

    Blank Line

    I just started on java, and I'm learning from this book for dummies. I wanted to see what I learned, so I am doing a sample project. It's about a guy named Bob and Jack having a conversation.

    Jack.java~~~~~~~~~~~~~~~~~~~~~~~~~~~~`



    Code:
    public class Jack {
    	int age;
    	float weight;
    	float height;
    	String color;
    	
    	public String intro(String aWord){
    		String introreply = "Jack: Hello, my Name is Jack." + aWord;
    			return introreply;
    		
    	}
    }


    Bob~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`



    Code:
    public class Bob {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		String Introduction = null;
    		Jack jackreply = new Jack();
    		
    		jackreply.intro(Introduction);
    		Introduction = jackreply.intro ("Bob: Nice to Meet you Jack, my name is Bob. ");
    		System.out.println(Introduction);
    		
    		// TODO Auto-generated method stub
    
    	}
    
    }

    When I run it, it says "Hello, my name is... Nice to meet you jack."
    I want to put a empty line space so the text isn't all in one line. How can I do this? I searched on google, it said something about \n, but It didn't work. Could somebody help me out?
    And could somebody tell me if that code is written right?

    EDIT: Could somebody tell me how to make it wait for a certain amount of time?
     
  3. Unread #2 - Nov 27, 2009 at 4:27 PM
  4. Stuart
    Joined:
    May 5, 2005
    Posts:
    1,580
    Referrals:
    2
    Sythe Gold:
    10

    Stuart Guru
    Banned

    Blank Line

    using println will work you just have to print more then one line. You can use \n also which is a new line which you just place in your string like so.

    String line = "hello?\nmy name is stuart";
     
  5. Unread #3 - Nov 27, 2009 at 4:34 PM
  6. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Blank Line

    1. Start your fields with lowercase letters (intro as opposed to Intro).
    2. Change the intro method in Jack to
    Code:
    public String into(String str) {
        return "Jack: Hello, my name is Jack." + '\n' + str;
    }
    To make it wait, use Thread.sleep(long millis)
     
  7. Unread #4 - Nov 28, 2009 at 7:35 AM
  8. Draucia
    Referrals:
    0

    Draucia Guest

    Blank Line

    How do I insert the thread.sleep?

    Errr not what my tutorial showed me.. Anyways, if I change the intro method in jack, what would I write in bob (the reply)?

    THis?

    Public String intro(str){
    return ("Bob: Nice to meet you, I'm Bob.")
    }


    And what is "Str"? Just random letters or does it actually mean something.




    EDIT: And i'm still confused on this:


    String Introduction = null;

    Jack jackreply = new Jack();

    The tutorial told me to write that, but I don't get it. Could you please help me?
     
  9. Unread #5 - Nov 29, 2009 at 7:38 PM
  10. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Blank Line

    You need to use code tags. I edited your first post.

    To make the program sleep (wait) you call the method Thread.sleep. This method has one parameter which is the number of milliseconds you want it to wait.

    So

    Code:
    Thread.sleep(1000);
    
    Makes the program wait for 1 second before moving on.

    @Jimmy. Doesn't Thread.sleep throw an error that you need to catch? I'm too lazy to make sure so... Help the man with that. D:

    Compare your method with Jimmy's

    Code:
    public String intro(String aWord){
    		String introreply = "Jack: Hello, my Name is Jack." + aWord;
    			return introreply;
    		
    	}
    
    Code:
    public String into(String str) {
        return "Jack: Hello, my name is Jack." + '\n' + str;
    }
    
    Jimmy has simply combined everything into the return what ever the fuck you call it....

    So it could be written like...

    Code:
    public String into(String str) {
    String reply = "Jack: Hello, my name is Jack." + '\n' + str;
    return reply;
    }
    
    Its all a matter of style... When you are beginning it is easier to put everything out by its self.... So Jimmmmmmmy. Be nice to the people and don't go all combining on them.

    str is just the variable name that is used with the parameter. You can name it what ever you want... However I think its just a convention to use str for strings... >_>. You could change it to what ever you want as long as you change the variable name everywhere its used....

    Now I'l walk you through what your program does...

    Code:
    String Introduction = null;
    
    That just makes a string that is set to nothing...

    Code:
    Jack jackreply = new Jack();
    That creates a new instance of your class Jack. Its object orientation which i suck at explaining... And its hard for newbies to understand... Or was for me...

    Code:
    jackreply.intro(Introduction);
    
    That seems to do nothing... Returns a string that has nothing added to it since introduction is null....?

    Code:
    Introduction = jackreply.intro ("Bob: Nice to Meet you Jack, my name is Bob. ");
    
    That uses the intro method of the Jack object you made and named jackreply... It sets introduction equal to the output.... So that does do something...

    Code:
    System.out.println(Introduction);
    That prints introduction....
     
  11. Unread #6 - Nov 29, 2009 at 9:43 PM
  12. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Blank Line

    Yes, Thread.sleep() throws an InterruptedException. You can usually ignore it, unless you have a compelling reason to do something when the sleep is interrupted.

    So, just do something like
    Code:
    try {
        Thread.sleep(1000L); //or however long to sleep for in milliseconds
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
     
  13. Unread #7 - Nov 29, 2009 at 9:45 PM
  14. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Blank Line

    Oh. I thought it was one of the ones that you HAD to catch... or just do throws exception and ignore it. lol
     
  15. Unread #8 - Nov 29, 2009 at 11:34 PM
  16. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Blank Line

    You do. Perhaps I worded that improperly. You can usually swallow the exception, with little to no consequences. You cannot simply ignore it, as it is descended from java.lang.Exception, and is not descended from java.lang.RuntimeException.

    While putting the throws declaration all the way up the stack into the main() method will stop you from having to catch it, if a RuntimeException does occur, rather then an interrupted sleep, the exception will go all the way up the stack into the main() method, and disrupt your program.
     
  17. Unread #9 - Dec 2, 2009 at 5:14 PM
  18. Draucia
    Referrals:
    0

    Draucia Guest

    Blank Line

    ^ that post made no sense to me what so ever.

    I'm still confused how to do the "Jimmy way"

    This is what I'm doing so far:


    Jack.class~~~~~~~~~~~~~



    Code:
    public class Jack {
    	int age;
    	float weight;
    	float height;
    	String color;
    	
    	public String intro(String str) {
    	    return "Jack: Hello, my name is Jack." + '\n' + str;
    	
    	
    	
    	}
    }
    

    Bob.class~~~~~~~~~~~~~~~~~~`

    This has a few errors
    Code:
    
    public class Bob {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args){
    	
    		
    		String intro = null;
    		String str =  null;
    		
    		Bob bobreply =  new Bob();
    		
    		bobreply.str(intro);
    		intro = bobreply.intro("Nice to meet you Jack, I'm Bob.");
    		System.out.println(intro);
    		
    	
    		
    	}
    
    	
    	
    }
    

    Thanks for all your posts and time guys, it's appreciated.
     
  19. Unread #10 - Dec 2, 2009 at 6:36 PM
  20. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Blank Line

    Few things...

    1. What is the point of
    Code:
    	int age;
    	float weight;
    	float height;
    	String color;
    in the Jack class (as they are all unused)?

    2. There is no "intro" method in the "Bob" class, so that is the problem with the compile-time errors.

    3. Your main() method doesn't make any sense. Do you mean for it to be something more like:
    Code:
        public static final void main(String[] args) {
            System.out.println(new Bob().intro("STRING HERE"));
        }
    ?
     
  21. Unread #11 - Dec 4, 2009 at 1:49 PM
  22. Draucia
    Referrals:
    0

    Draucia Guest

    Blank Line

    I was going to do the age weight and things later.

    I replaced the main method thing, but I'll confused :(.

    If you would be kind of, could you please write a sample code that says
    "Bob: Hello, my name is Bob."
    "Jack: Hey Bob, I'm jack."

    And maybe a time waiting period?
     
  23. Unread #12 - Dec 4, 2009 at 2:32 PM
  24. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Blank Line

    Well if you don't want to use object orientation you just need to put:

    Code:
    System.out.println("Bob: Hello, my name is Bob.");
    System.out.println("Jack: Hey Bob, I'm jack.");
    
    Thats minus the waiting period... >_>.
     
  25. Unread #13 - Dec 5, 2009 at 7:02 AM
  26. Draucia
    Referrals:
    0

    Draucia Guest

    Blank Line

    But anybody can do that.. I wan't to do something with 2 class, that call own each other.
     
  27. Unread #14 - Dec 5, 2009 at 8:22 PM
  28. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Blank Line

    ^Okay.

    I'm going to change your tut a bit.... 2 Classes.

    Person
    Talker

    Class person will replace the bob / jack stuff.

    Talker will be used to talk...

    So. Person will be something like below:

    Code:
    public class Person
    {
    	private String name;
    	
    	public Person(String name)//Constructor
    	{
    		this.name = name;//this.name is the private string at the top... name is the parameter
    	}
    	
    	public String intro(String str)
    	{
    		String intro = name + ": Hello, my Name is " + name + "." + str;
    		return intro;
    	}
    }
    
    Here is the talker

    Code:
    public class Talker
    {
    	public static void main(String[] args)
    	{
    		Person jack = new Person("Jack");
    		Person bob = new Person("Bob");
    		
    		System.out.println(jack.intro("I'm cool"));
    		System.out.println(bob.intro("I'm cool"));
    	}
    }
    
    Look at that and try to understand it. If you don't know what a constructor is google it and look for the offical java tut series...
     
  29. Unread #15 - Dec 5, 2009 at 10:39 PM
  30. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    Blank Line

    Temporarily halt execution of currently working thread for X ms:
    Code:
    Thread.sleep(X);
    Use a new line:
    Code:
    public String intro(String str) {
        return "Jack: Hello, my name is Jack\n" + str;
    }
     
< JAVA Ideas | A few questions.. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site