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

Thread: Rookie Question! Help Appreciated

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Rookie Question! Help Appreciated

    The purpose of this code: after typing 5 integers to then display the largest and smallest one.

    Im not sure where to go from here.

    This is my Current Code:

    import java.util.Scanner;
     
    public class Comparison
    {
        public static void main( String[] args )
        {
         Scanner input = new Scanner( System.in );
     
         int number1;
         int number2;
         int number3;
         int number4;
         int number5;
     
         System.out.print( "Enter First Integer: " );
         number1 = input.nextInt();
     
         System.out.print( "Enter First Integer: " );
         number2 = input.nextInt();
     
         System.out.print( "Enter Third Integer: " );
         number3 = input.nextInt();
     
         System.out.print( "Enter Fourth Integer: " );
         number4 = input.nextInt();
     
         System.out.print( "Enter Fifth Integer: " );
         number5 = input.nextInt();
     
         if ( number1 > number2 )
             System.out.printf( "%d == %d\n", number1, number2 );
     
         if ( number1 < number2 )
             System.out.printf( "%d < %d\n", number1, number2 );
     
     
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    Where to go from here... well what are the options? Where is here?
    What does the code do now? What should it do next?

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    Well right now it doesn't consider all 5 numbers, I want it to take the 5 numbers I input and then tell me the largest and smallest one of the 5

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    Then I would say the next step would be to make the code consider all 5 numbers.
    How would you do it without a computer?
    What steps would you take, specifically. Write down each thing you must do to find the answers yourself. Each number you look at, each number you compare to, etc...
    When you have a list of steps organized in a start to finish order, then you can start working on the code.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    I have no idea what you are saying I'm not sure how to make it consider all 5 numbers, never done it before.. that's why i'm asking..

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    I am saying do not worry about how "it" will solve the problem. I am asking how YOU would solve the problem.
    15 84 20 13 68 43
    In reality there are two problems to solve;
    1) find the highest number
    2) find the lowest number
    Work on one problem at a time. How would you find the highest number?

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    The way i'd find the highest number compare one number to all the others. That's what i'm trying to code too, but not sure how to do that exactly

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    Where is your list of steps to compare one number to all others?

    There are many ways to do this, and we do not want to do it for you. This is for you to learn, but we will help you translate your solution into code.

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    Is 84 > 15
    Is 84 > 20
    Is 84 > 68
    Is 84 > 13
    Is 84 > 68
    Is 84 > 43
    My list

    --- Update ---

    I know this... But i dont know how to write the code to make it check for multiple numbers, not just 1..

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    Okay, so far so good.
    Have a look at what you have done so far.
    First you selected a number, (by default just the first one in line)
    Then you compared this number to the next number in line. For this to happen you had to remember which number you were going to compare to the next.
    Then you repeat this process for each of the remaining numbers.

    -the following still needs to be done-
    Somewhere along the way, if a larger number is found, you will have to remember that number instead. When you reach the end of the set of numbers, the number being compared should be the largest. Problem of finding the largest number is solved.

    This means you may use a variable like largestNumberSoFar to keep track of the current largest number as you go.
    Give it a shot and see what you come up with

  11. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    I dont know how to make it remember a number.. Like i said im very new..

    --- Update ---

    Like I know the process, I just don't know what to use, that's my original question

  12. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    Then what is this:
     int number1;
         int number2;
         int number3;
         int number4;
         int number5;
    and this:
    number1 = input.nextInt();
         number2 = input.nextInt();
         number3 = input.nextInt();
         number4 = input.nextInt();
         number5 = input.nextInt();
    Looks like you set up 5 variables of type int, and give all five a value. If you can make the program remember five numbers why can you not make it remember another one?

  13. #13
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    I realize I need to make the bottom line something that instead of printing the answer, it remember's the number, but im not sure how to do that
    if ( number1 > number2 )
    System.out.printf( "%d > %d\n", number1, number2 );


    --- Update ---

    Ahhh okay i see

    --- Update ---

    Hmmm, still kinda confused, something like this?

    int number6;

    if ( number1 > number2 )
    number6 = input.nextInt();

    If number 1 > 2 it makes the number 6?

    then do number 6 > number 3

    If its > 3 it stores it as 7? or would that be incorrect?

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    Quote Originally Posted by Knighter View Post
    Like I know the process, I just don't know what to use, that's my original question
    I understand, but no one can write the code for you, you can read about why here.
    This exercise is to get you some practice turning a simple process into code so that you can handle a more complicated process. Work on the idea of breaking the problem down into steps that you can turn into code, that is what it is all about. Every detail matters when you tell a computer to do it for you.

    --- Update ---

    The pseudo code in post #13 does not match what you had in post #9
    (As a side note 'number6' is a poor description of what that number is for)
    Why are you getting a new number for number6, that should represent the largest number found so far. In post #9 that value was 84, which was the first number in the set, (number1) and not a new number given by the user.

  15. #15
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    I know it isn't the same, so how can I learn how to do this, cause I don't know what means to use cause I don't understand this and this isn't helping

    --- Update ---

    The way I was thinking of doing it was something like if ( number1 > number2, number3, number4, number5 )
    System.out.printf( "%d > %d\n", number1, number2 );

    and repeat it 4 times like so for each number, but that is creating an error, im not sure how to list multiple numbers instead of 1

  16. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    How about this for pseudo code:
    (First, the instructions)
    after typing 5 integers to then display the largest and smallest one

    set up a place to store 5 integers
    get 5 integers from the user
    set up a place to store the largest integer
    give the largest integer variable the value of the first integer.
    compare the largest integer variable to each of the remaining numbers in the set (the other 4)
    if a larger number is found, replace the largest integer value with the newly discovered larger value, else do nothing
    at the end of the set, the value in the largest integer variable will be the largest number given by the user

    Do you follow the idea with this? Any questions?

  17. #17
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    Yes sort of, your brain works a bit faster than mine with this stuff I'm a 4th year business student trying to self teach myself java hehe, these are some excercises I found online, however no solution or explanations are given but I really want to solve this one. My question is though how to find the largest integer of the 5, I know how to store it etc once its found but how do I find it, I only know how to compare 2 numbers, not 6, for example

    if ( number1 > number2 )
    System.out.printf( "%d > %d\n", number1, number2 );

    However I dont know how to make that consider multiple other numbers, not just one.

  18. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    ( number1 > number2, number3, number4, number5 ) Something like this just does not work
    Read over the pseudo code in post #16 again and see if you can follow the idea there. It is just a more detailed description of what you had in post #9
    You will have to compare them one at a time like you started to do in the original code.

  19. #19
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    So this is where I got to after some dabbling:

    I feel like the way im trying to store it is wrong though

    import java.util.Scanner;

    public class Comparison
    {
    public static void main( String[] args )
    {
    Scanner input = new Scanner( System.in );

    int number2;
    int number3;
    int number4;
    int number5;

    int largestinteger;


    System.out.print( "Enter First Integer: " );
    largestinteger = input.nextInt();

    System.out.print( "Enter Second Integer: " );
    number2 = input.nextInt();

    System.out.print( "Enter Third Integer: " );
    number3 = input.nextInt();

    System.out.print( "Enter Fourth Integer: " );
    number4 = input.nextInt();

    System.out.print( "Enter Fifth Integer: " );
    number5 = input.nextInt();

    if ( largestinteger < number2 )
    largestinteger = input.nextInt()



    }
    }


    --- Update ---

    Would it perhaps be

    if ( largestinteger < number2 )
    largestinteger = input.number2()
    ??

  20. #20
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Rookie Question! Help Appreciated

    I like it all the way up to:
    largestinteger = input.nextInt()

    When you do the test largestinteger < number2, if numbr2 turns out to be the larger, then you would assign the value of number2 to largestinteger
    largestinteger = number2;
    Then largestinteger would in fact hold the largest integer so far, continue to check number3 and number4.

    Beware the use of > and <

  21. #21
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    Nice I got it to work!

    So it all works but just to see your opinion on how I could make it better (if i can) feedback is nice

    import java.util.Scanner;

    public class Comparison
    {
    public static void main( String[] args )
    {
    Scanner input = new Scanner( System.in );

    int number2;
    int number3;
    int number4;
    int number5;

    int largestinteger;


    System.out.print( "Enter First Integer: " );
    largestinteger = input.nextInt();

    System.out.print( "Enter Second Integer: " );
    number2 = input.nextInt();

    System.out.print( "Enter Third Integer: " );
    number3 = input.nextInt();

    System.out.print( "Enter Fourth Integer: " );
    number4 = input.nextInt();

    System.out.print( "Enter Fifth Integer: " );
    number5 = input.nextInt();

    if ( largestinteger < number2 )
    largestinteger = number2;

    if ( largestinteger < number3 )
    largestinteger = number3;

    if ( largestinteger < number4 )
    largestinteger = number4;

    if ( largestinteger < number5 )
    largestinteger = number5;

    System.out.println( largestinteger );
    }
    }

  22. #22
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Next is to use an array and a loop. Ask the user how many numbers to test. Make the array that size and take that many inputs.

    Make it dynamic. Well done, I've been watching this thread

  23. #23
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rookie Question! Help Appreciated

    Thanks Kewish. Was some hair pulling for sure.

  24. #24
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    I was watching both you and jps. I was made aware of spoonfeeding the other day. And now I really see the benefit of leading you to the answer no matter how much you want to give the answer, you got to enjoy the wonderful ah-hah moment.

    I look forward to your dynamic version.

Similar Threads

  1. Big Rookie Question - New to Java, small problem Help appreciated!
    By Knighter in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 24th, 2013, 01:55 AM
  2. Rookie here, need help with sum of digits program
    By wkellogg10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2013, 07:25 PM
  3. All help would be much appreciated!!
    By Rkelly155 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 13th, 2012, 05:38 AM
  4. Help really appreciated
    By Thermal_Vent in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2010, 08:42 AM
  5. any help is much appreciated
    By Schmitz14 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2009, 07:51 PM