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 14 of 14

Thread: noob having problem with return...

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default noob having problem with return...

    hi guys... i tried to create a simple program. The main method is supposed to call out the secondary method to use Scanner in order to ask a person to type in a number, which will be assigned to int x. Then the secondary method supposed to return the int x to main method, which supposed to print it out. And for some reason I just can't do it. No matter what the main method refusing to accept the int x. What am i doing wrong?
    import java.util.Scanner;
    public class Return{
    	static Scanner newNum= new Scanner(System.in);
    	public static void main(String[] args){
     
    		getInt();
     
    		System.out.println(x);
    	}
    	public static  getInt( ) {
    		// TODO Auto-generated method stub
    		int x;
    		System.out.print("give a num");
    		x= newNum.nextInt();
    		return x;
    }}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: noob having problem with return...

    Ah. A misconception of how things work.

    The call to the method, getInt(), causes the method to execute and a value to be returned, but the main() method has to assign or use that value. As you have it now, the value returned from the method is discarded. You could in main():

    int x = getInt();

    or

    System.out.println( getInt() );

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    sash25 (October 10th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    21
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Re: noob having problem with return...

    also another thing to add... you don't have return type on the method signature.

    public static int getInt() { // is what that line should be.

    You should then be able to put either of the lines GregBrannon stated in main and it should work.

  5. The Following 2 Users Say Thank You to jbarke12 For This Useful Post:

    GregBrannon (October 10th, 2014), sash25 (October 10th, 2014)

  6. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: noob having problem with return...

    thnx guys for quick response, sometimes u have a small mistake and ur whole code stuck while u are getting a headache (which i have right now) trying to find what's wrong. This is how u learn I guess. Hope in a few weeks things become easier(I'm optimistic).

  7. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: noob having problem with return...

    Thanks jbarke12 for fixing my incomplete answer.

    I also wanted to add that you, sash25, should know that the 'x' in the method and the 'x' in the main() method do not refer to the same variable. They are two different variables with the same name. The first line I wrote above could also be:

    int value = getInt(); // the value in 'x' returned by the method is stored here in 'value'

    but then your original println statement would have to be changed to:

    System.out.println( value );

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    sash25 (October 10th, 2014)

  9. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: noob having problem with return...

    yep greg, i had a feeling it's not the same x. thnx.
    can any one recomend where i can get exercises for noobs in the internet?

  10. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: noob having problem with return...

    What are you using as a study guide now? Do you have a textbook? If not, there are a couple good ones available free on the 'net. The best course is to follow a structured guide to learn the skills in the right order, do the right amount of practice (it's hard to do too much), and then complete the exercises at the end of each chapter that expand on the concepts presented in the chapter, possibly adding in skills previously presented, hopefully learned.

  11. #8
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: noob having problem with return...

    I use only youtube, mostly Derek Banas videos (are u familiar with his channel?). Textbook? I think its better watching tutorials on video, but maybe i'm wrong.

  12. #9
    Junior Member
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    21
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Re: noob having problem with return...

    YouTube videos have their place, but its important to understand the basics that you would pick up from a good computer science book. Especially just starting out. The text books are normally laid out in a manner that your learn things in the right order.

  13. The Following User Says Thank You to jbarke12 For This Useful Post:

    GregBrannon (October 10th, 2014)

  14. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: noob having problem with return...

    I agree with jbarke12 for some of the same and different reasons, primarily though because watching random videos puts you in charge of your syllabus, and when was the last time you developed a beginning programming course? And, as you've indicated by your question, videos do not provide the necessary examples, opportunities to practice and exercises related directly to the material presented that a good book will. If you're not practicing several hours for every hour of instruction, you're not doing it right.

  15. #11
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: noob having problem with return...

    can u recommend a good textbook?
    there is a text book called Head First Java (http://uet.vnu.edu.vn/~chauttm/e-boo...nd-edition.pdf). I been told it's good.

  16. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: noob having problem with return...

    This is the best collection of opinions on Java books I've seen, but it's just that, a collection of people's opinions. I think most every Java book ever written is listed there and positive things are said about them all.

    Many people like the Head First series. I'm also sure that many don't.

  17. The Following User Says Thank You to GregBrannon For This Useful Post:

    sash25 (October 10th, 2014)

  18. #13
    Junior Member
    Join Date
    Oct 2014
    Location
    USA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: noob having problem with return...

    **[I]I posted the working refactored version below, the fixes are in the comments. I hope this helps[/I].


    *** code removed
    Last edited by Norm; October 10th, 2014 at 08:07 PM. Reason: Please don't do OPs work for him

  19. #14
    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: noob having problem with return...

    @Java Guy Please read: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. A problem with return.
    By ddscs in forum Android Development
    Replies: 3
    Last Post: April 17th, 2013, 12:38 PM
  2. [SOLVED] Problem with return value.
    By E.K.Virtanen in forum Object Oriented Programming
    Replies: 3
    Last Post: March 27th, 2013, 12:45 PM
  3. NOOB problem here.
    By Nemus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2011, 08:13 AM
  4. probably a simple noob problem
    By Unaffiliated in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2011, 12:25 AM
  5. Problem with Return Function
    By Tracy22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2010, 03:32 PM