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

Thread: returing my value from an array

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default returing my value from an array

    // please help me, I am trying to return the lowest value from the array. having trouble trying to capture the return value by placing it into the variable .. int myLowest = getLowest(yourNumbers); and then printing out myLowest


    import java.util.*;



    public class numArrays
    {
    public static void main(String[] args){


    int numbers;
    int[]yourNumbers;

    Scanner keyboard = new Scanner(System.in);

    System.out.print("How many numbers do you want to enter?");
    numbers = keyboard.nextInt();

    yourNumbers = new int[numbers];

    for (int i = 0; i < yourNumbers.length; i++){
    System.out.print("Enter Numbers" + (i + 1) + ":");
    yourNumbers[i] = keyboard.nextInt();
    }

    System.out.print("Here are your numbers:");
    for(int i = 0; i < yourNumbers.length; i++)
    System.out.print(yourNumbers[i] + " ");

    getLowest(yourNumbers);

    }




    public static int getLowest(int[] yourNumbers)
    {

    int lowest = yourNumbers[0];
    {
    for(int i = 1; i < yourNumbers.length; i++)

    if (yourNumbers[i] < lowest)
    lowest = yourNumbers[i];

    }

    return(lowest);


    int myLowest = getLowest(yourNumbers);

    System.out.print(myLowest);


    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: returing my value from an array

    When posting code, please use the highlight tags to preserve formatting.

    What does this code do? What exactly are you stuck on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    ARsNGlocks (August 12th, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returing my value from an array

    sorry about that, the user inputs how ever many numbers they want then it returns them as an array and i am trying to get it to return the lowest number out of the entered numbers. it compiles up untill the very last part.

    if you remove this part of the code:

    int myLowest = getLowest(yourNumbers);

    System.out.print(myLowest);

    it will compile and run but wont return lowest, I am just trying to figure out how to print the lowest number entered by the user

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

    Default Re: returing my value from an array

    Before you remove those lines, when you compile or run, do you get errors? If so, post those.

  6. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returing my value from an array

    no errors, runs and prints out the users numbers, just doesnt print the lowest.

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

    Default Re: returing my value from an array

    Please post your code correctly using code or highlight tags which are explained near the top of this link. If you haven't read that entire page, please do so.

  8. #7
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: returing my value from an array

    Difficult to read due to the formatting, but at a guess, this

    return(lowest);

    Will be executed, hence

    int myLowest = getLowest(yourNumbers);
     
    System.out.print(myLowest);

    Is never going to execute.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: returing my value from an array

    Quote Originally Posted by ARsNGlocks View Post
    no errors
    I find that hard to believe. You should be getting an unreachable statement error as you have code after the return statement.
    Improving the world one idiot at a time!

  10. #9
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returing my value from an array

    Quote Originally Posted by Junky View Post
    I find that hard to believe. You should be getting an unreachable statement error as you have code after the return statement.

    there are no errors when the code below the return is removed

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: returing my value from an array

    Ok. Do you still have problems? If so post your latest code and include full error messages.
    Improving the world one idiot at a time!

  12. #11
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: returing my value from an array

    I tested out the last given code - with the statements I do indeed get
    the error 'unreachable code' which is obvious as stated previous. When I
    ran the code through the debugger after removing the two offending(?)
    lines of code - I got this:

    How many numbers do you want to enter? 14
    Enter Numbers1: 2
    Enter Numbers2: 5
    Enter Numbers3:2
    Enter Numbers4:3
    Enter Numbers5:1
    Enter Numbers6:1
    Enter Numbers7:1
    Enter Numbers8:22
    Enter Numbers9:2
    Enter Numbers10:3
    Enter Numbers11:3
    Enter Numbers12:5
    Enter Numbers13:1
    Enter Numbers14:5
    Here are your numbers:2 5 2 3 1 1 1 22 2 3 3 5 1 5

    Which was exactly how it should of executed.

    When the method terminated (at the return) control was handed back to
    method main. Your problem lies in the visibility of your two removed statements.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  13. #12
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returing my value from an array

    Quote Originally Posted by Ada Lovelace View Post
    I tested out the last given code - with the statements I do indeed get
    the error 'unreachable code' which is obvious as stated previous. When I
    ran the code through the debugger after removing the two offending(?)
    lines of code - I got this:

    How many numbers do you want to enter? 14
    Enter Numbers1: 2
    Enter Numbers2: 5
    Enter Numbers3:2
    Enter Numbers4:3
    Enter Numbers5:1
    Enter Numbers6:1
    Enter Numbers7:1
    Enter Numbers8:22
    Enter Numbers9:2
    Enter Numbers10:3
    Enter Numbers11:3
    Enter Numbers12:5
    Enter Numbers13:1
    Enter Numbers14:5
    Here are your numbers:2 5 2 3 1 1 1 22 2 3 3 5 1 5

    Which was exactly how it should of executed.

    When the method terminated (at the return) control was handed back to
    method main. Your problem lies in the visibility of your two removed statements.

    Wishes Ada xx
    yes you are correct, I was trying to figure out what I had to do to get it to also return the lowest number out of the user input

    --- Update ---

    This is my problem, I have having trouble trying to figure out how to get the last part to execute.

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

    Default Re: returing my value from an array

    This is my problem, I have having trouble trying to figure out how to get the last part to execute.
    Put the "last part" before the return? Post your updated code correctly with the latest results and a description of what the code should be doing, and we'll see if we can help. Right now I'm not sure what the problem is.

  15. #14
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returing my value from an array

    Quote Originally Posted by GregBrannon View Post
    Put the "last part" before the return? Post your updated code correctly with the latest results and a description of what the code should be doing, and we'll see if we can help. Right now I'm not sure what the problem is.

    import java.util.*;
     
     
     
    public class numArrays
    {
    public static void main(String[] args){
     
     
    int numbers;
    int[]yourNumbers; 
     
    Scanner keyboard = new Scanner(System.in);
     
    System.out.print("How many numbers do you want to enter?");
    numbers = keyboard.nextInt();
     
    yourNumbers = new int[numbers];
     
    for (int i = 0; i < yourNumbers.length; i++){
       System.out.print("Enter Numbers" + (i + 1) + ":");
         yourNumbers[i] = keyboard.nextInt();
         }
     
    System.out.print("Here are your numbers:");
     for(int i = 0; i < yourNumbers.length; i++)
       System.out.print(yourNumbers[i] + " ");
     
    getLowest(yourNumbers);
     
    }
     
     
     
     
    public static int getLowest(int[] yourNumbers)
    {
     
      int lowest = yourNumbers[0];
      {
      for(int i = 1; i < yourNumbers.length; i++)
     
      if (yourNumbers[i] < lowest)
      lowest = yourNumbers[i];
     
      }
     
     
     
     
        int myLowest = getLowest(yourNumbers);
     
        System.out.print(myLowest);
     
      }
      }

    I am trying to get it to print the lowest number out of the user inputs.

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

    Default Re: returing my value from an array

    You now have a method specified to return an 'int', but it has no return statement. The use of methods can be confusing at first, but don't make it harder than it is. DO NOT include a statement in the method that calls itself unless you're designing a recursive method that has an escape strategy, which you do not.

    Besides deleting the last two statements in the method, here's what else you could do to fix what you have:

    1. (A) Change getLowest() to type void and (B) print the lowest value before leaving the method (B is kind of what you have now), or

    2. Leave the method returning an int (lowest), and have main() print the result, as in:

    System.out.println( "The lowest is: " + getLowest(yourNumbers) );

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

    ARsNGlocks (August 15th, 2014)

  18. #16
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returing my value from an array

    import java.util.*;
     
     
     
    public class numArrays
    {
    public static void main(String[] args){
     
     
    int numbers;
    int[]yourNumbers; 
     
    Scanner keyboard = new Scanner(System.in);
     
    System.out.print("How many numbers do you want to enter?");
    numbers = keyboard.nextInt();
     
    yourNumbers = new int[numbers];
     
    for (int i = 0; i < yourNumbers.length; i++){
       System.out.print("Enter Numbers" + (i + 1) + ":");
         yourNumbers[i] = keyboard.nextInt();
         }
     
    System.out.print("Here are your numbers:");
     for(int i = 0; i < yourNumbers.length; i++)
       System.out.print(yourNumbers[i] + " ");
     
    getLowest(yourNumbers);
     
    }
     
     
     
     
    public static int getLowest(int[] yourNumbers)
    {
     
      int lowest = yourNumbers[0];
      {
      for(int i = 1; i < yourNumbers.length; i++)
     
      if (yourNumbers[i] < lowest)
      lowest = yourNumbers[i];
     
      }
     
     
     
      System.out.println( "The lowest is: " + getLowest(yourNumbers) );
     
      }
      }
    }

    is this correct, cause i still get an error.

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

    Default Re: returing my value from an array

    What errors? Post those too. And PLEASE, please, please, begin class names with capital letters.

  20. #18
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: returing my value from an array

    Quote Originally Posted by ARsNGlocks View Post
    is this correct, cause i still get an error.
    I want you to walk into the kitchen, open the cutlery drawer, grab a spoon and smack yourself on the forehead. How can it possibly be correct if you are getting errors?
    Improving the world one idiot at a time!

  21. #19
    Junior Member
    Join Date
    Aug 2014
    Posts
    8
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returing my value from an array

    Quote Originally Posted by Junky View Post
    I want you to walk into the kitchen, open the cutlery drawer, grab a spoon and smack yourself on the forehead. How can it possibly be correct if you are getting errors?
    ha, yea sorry, I meant my format on that last part of code I added, this is error i am getting.

    numArrays.java:53: error: missing return statement
    }
    ^
    1 error

    --- Update ---

    or this error when I add a {

    numArrays.java:57: error: class, interface, or enum expected
    }
    ^
    1 error

  22. #20
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: returing my value from an array

    What is going on here:

    public static int getLowest(int[] yourNumbers)
    {
     
      int lowest = yourNumbers[0];
      {
      for(int i = 1; i < yourNumbers.length; i++)
     
      if (yourNumbers[i] < lowest)
      lowest = yourNumbers[i];
     
      }
     
     
     
      System.out.println( "The lowest is: " + getLowest(yourNumbers) );
     
      }
      }
    }

    You've got an opening bracket *before* your for loop for some reason, but not after. Then you don't have any brackets with your if statement (this is a horrible habit to get into). You then close that first stray bracket, but then close 2 more for some reason? Huh?

    Properly format this code, including proper indentation, and match up each opening and closing bracket to its partner.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  23. #21
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: returing my value from an array

    numArrays.java:53: error: missing return statement
    You told the compiler the method would return an value:

    public static int getLowest
    And you told the compiler a fib

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  24. #22
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: returing my value from an array

    I skimmed over this feed and think I have found the issue here. The code below is not tested:

    public static int getLowest(int[] yourNumbers)  //you have to return something since you declared it as a int
    {
     
      int lowest = yourNumbers[0];
     
      for(int i = 1; i < yourNumbers.length; i++)
    {
     
      if (yourNumbers[i] < lowest)
       {
      lowest = yourNumbers[i];
       }
    }
      // you really don't need this here in my opinion, move this to main. If this its intended purpose make it a void statement
     
      System.out.println( "The lowest is: " + getLowest(yourNumbers) );
     
         return lowest;  
      }
    }

  25. #23
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: returing my value from an array

    Spoonfeeding is not helping.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Re: Blue Pelican Java- Array of Hope; char array for loops?
    By ZaneDarklace in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 14th, 2014, 11:46 AM
  2. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  3. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  4. Replies: 8
    Last Post: December 8th, 2012, 03:47 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM