Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: complete noob, pretty sure I'm just missing one argument

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default complete noob, pretty sure I'm just missing one argument

    Hi, I'm brand new to Java and I've just been making simple projects to get into the swing of things. So far I've had reasonable success but I can't figure out what the problem is with this code. I know it has something to do with the "System.out.println(puppyObject.setTime" thing but I have no idea what is wrong with it. It's probably something really simple and not important at all but I'd just like to know since I believe this might be important in future projects.

    Here is the code:

    import java.util.Scanner;
     
    class kitten{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		puppy puppyObject = new puppy();	
     
    		System.out.println(puppyObject.setTime(0, 0, 0));
    		System.out.println(puppyObject.toMilitary());
    	}
    }
     
    import java.util.Scanner;
     
    class puppy{
    	public int hour;
    	public int minute;
    	public int second;
     
    	public void setTime(int h, int m, int s){
    		Scanner input = new Scanner(System.in);
     
    		hour = h;
    		minute = m;
    		second = s;
     
    		System.out.println("Set the hour.");
    		h = input.nextInt();
     
    		while(m == 0){
    		if(h >= 0 && h <=24){
    			System.out.println("Set the minute.");
    			m = input.nextInt();
    		}
    		else{
    			System.out.println("Try again, make sure you input a number between 0 and 24.");
    			h = input.nextInt();	
    		}
    		}
     
    		while(s == 0){
    		if(m >= 0 && m <= 60){
    			System.out.println("Set the second.");
    			s = input.nextInt();
    		}
    		else{
    			System.out.println("Try again, make sure you input a number between 0 and 60.");
    			m = input.nextInt();
    		}
    		}
     
    		if(s >= 0 && s <= 60){
     
    		}
    		else{
    			System.out.println("Try again, make sure you input a number between 0 and 60.");
    			s = input.nextInt();
    		}
    	}
     
    	public String toMilitary(){
    		return String.format("%02d:%02d:%02d", hour, minute, second);
    	}
    }

    Thanks in advance,
    Terty

    EDIT: I forgot to mention that I'd also just like to ask if I have to import the Scanner util in the main class if it's not really used there but the code that needs it is included.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: complete noob, pretty sure I'm just missing one argument

    The setTime() method has no return type (void). The System.out.println() method accepts an argument, where the argument is the thing you want to print to the console window.
    When you say:
    System.out.println(puppyObject.setTime(0, 0, 0));
    What do you expect to be printed?
    Since the setTime() method returns nothing, you cannot print the value returned from the setTime() method (you cannot print nothingness).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: complete noob, pretty sure I'm just missing one argument

    Also, since the hour minute and second are within the scope of the setTime() method, unless they're returned somehow, they will not be accessible after the method completes.

    By the way, your little check to make sure s is between 0 and 60 is not looped.

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: complete noob, pretty sure I'm just missing one argument

    When I run everything that's in the puppy class it runs just fine, so I can't quite really understand why it can't just work the same. What do I have to use instead of System.out.println in this?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: complete noob, pretty sure I'm just missing one argument

    The println() method expects that all the parameters that are passed to it (inside the ()s) are Strings or can be converted to a String.
    Change the setTime method so that it returns a String that can be printed.

    However a method named: set... normally doesn't return anything. The set... method would be called to set a value and there would be other methods that returned a value.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Terty_Tree (August 21st, 2014)

  7. #6
    Junior Member
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: complete noob, pretty sure I'm just missing one argument

    I used to have it on getTime but my friend changed it to setTime when he took a look at it, he's slightly better than I am in this but no pro. Thanks for the reply! :-D

    What method can I use instead of println to use the now "getTime" method that I made?

  8. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: complete noob, pretty sure I'm just missing one argument

    If you want to dump output to the screen, println() is your way to go. So you can either execute the println() within the getTime() method, or you can have getTime return a String that the calling method can use println() on.

  9. #8
    Junior Member
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: complete noob, pretty sure I'm just missing one argument

    Quote Originally Posted by Norm View Post
    The println() method expects that all the parameters that are passed to it (inside the ()s) are Strings or can be converted to a String.
    Change the setTime method so that it returns a String that can be printed.

    However a method named: set... normally doesn't return anything. The set... method would be called to set a value and there would be other methods that returned a value.
    Oh wow, thanks. I just figured out what you were saying. As I said I'm a complete noob but it makes a lot of sense now! I believe this is gonna be a huge help to me in all future projects.

    --- Update ---

    Quote Originally Posted by blab View Post
    If you want to dump output to the screen, println() is your way to go. So you can either execute the println() within the getTime() method, or you can have getTime return a String that the calling method can use println() on.
    Thanks, also helped me figure it out. :-D
    Last edited by Terty_Tree; August 21st, 2014 at 01:17 PM.

Similar Threads

  1. Pretty New!
    By gr*8tD in forum Member Introductions
    Replies: 2
    Last Post: August 10th, 2014, 04:58 AM
  2. complete NOOB so confused..i need help with a program please!!!!
    By jayo88 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2014, 06:24 AM
  3. Replies: 2
    Last Post: June 29th, 2013, 03:47 AM
  4. complete noob, direction needed pls.
    By delamer in forum Java Theory & Questions
    Replies: 3
    Last Post: October 13th, 2012, 01:54 PM
  5. Complete Noob -- A little help would be appreciated...
    By echmiel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 5th, 2012, 05:31 PM