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

Thread: Need help understanding why my return value is not returning back.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help understanding why my return value is not returning back.

    Here is my assignment.

    Write a program that prompts the user for a positive integer n and prints “Hello World” n times. Of course, a value of n that is less than or equal to 0 is illegal. To ensure valid input, include a method
    int getPos()
    that prompts for a positive integer. If the value of that integer is less than or equal to 0, the method should print an appropriate message and request a positive number. When the user supplies a valid number, the method returns that number. (Hint: the break; statement is really handy in this program.)

    Below is my code

    /**
     * 
     * CSC 225 - Online
     * Problem 6
     * 
     */
     
    import java.util.*;
     
    public class Problem6
    {
       public static void main(String[] args)
       {
            Scanner scan = new Scanner(System.in);
            int input;
            int a;
     
            System.out.println( " Hello user! " );
            System.out.print( " Please enter the number of ireations you would like Hello World displayed. ");
            input = scan.nextInt();   
     
     
            getPos(input);
            System.out.println(input);
     
            for(a = 1; a <= input; a++)
            {
            System.out.println( " Hello World! ");    
            }
     
       }
     
       public static int getPos(int input)
       {
         Scanner scan = new Scanner(System.in);
     
         while (input <= 0)
         {
         System.out.println( " Error: Please enter a valid integer greater than 0." );
         System.out.print( " Please enter a valid number. " );
         input = scan.nextInt();
         }
     
         return input ;
     
       }
     
     
     
    }


    Below is my output


    Hello user!
    Please enter the number of ireations you would like Hello World displayed. -5
    Error: Please enter a valid integer greater than 0.
    Please enter a valid number. -5
    Error: Please enter a valid integer greater than 0.
    Please enter a valid number. -6
    Error: Please enter a valid integer greater than 0.
    Please enter a valid number. 55
    -5


    As you can see instead of returning 55 it returns my -5. FYI I placed a System.out.println tp see what the computer is doing. And I'm seeing its just not returning my value. Even if I remove my break statement in my while loop the text application just pauses....nothing happens. It does not display my Hello World statements. Any suggestions???

    Thanks Again!

    --- Update ---

    Never mind I figured it out. lol


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help understanding why my return value is not returning back.

    where does the code save the value returned by the getPos() method?

    Why is there an arg passed to the getPos() method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help understanding why my return value is not returning back.

    The main problem was you were not actually returning the new value and you weren't updating the input variable to the "new" value anyway, which meant the initial illegal value was used for the for loop.

    Here's the working code:


    *** Code removed.
    Last edited by Norm; February 28th, 2013 at 08:00 PM. Reason: Don't spoonfeed code to OP

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help understanding why my return value is not returning back.

    @I MIKE C I Please don't do the OPs work for him.

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

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help understanding why my return value is not returning back.

    Here is my updated code. Like I said Norm I have really haven't worked with methods to know why it wasn't working. But yeah I completed it.

    /**
     * Gregory B Shavers Jr
     * CSC 225 - Online
     * Problem 6
     * 
     */
     
    import java.util.*;
     
    public class Problem6
    {
       public static void main(String[] args)
       {
            Scanner scan = new Scanner(System.in);
            int input, output;
            int a;
     
            System.out.println( " Hello user! " );
            System.out.print( " Please enter the number of ireations you would like Hello World displayed. ");
            input = scan.nextInt();   
     
     
            output = getPos(input);
            System.out.println(output);
     
            for(a = 1; a <= output; a++)
            {
            System.out.println( " Hello World! ");    
            }
     
       }
     
       public static int getPos(int input)
       {
         Scanner scan = new Scanner(System.in);
     
         while (input <= 0)
         {
         System.out.println( " Error: Please enter a valid integer greater than 0." );
         System.out.print( " Please enter a valid number. " );
         input = scan.nextInt();
         }
     
         return input ;
     
       }
     
     
     
    }

    Thanks Again!

Similar Threads

  1. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  2. I'm back
    By Brt93yoda in forum The Cafe
    Replies: 3
    Last Post: November 15th, 2010, 10:29 PM
  3. Forgot to back up
    By WebPVP in forum Java Theory & Questions
    Replies: 5
    Last Post: October 13th, 2009, 07:13 PM