Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By Norm

Thread: Subtracting the lowest integer from the highest?

  1. #1
    Iopu is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Subtracting the lowest integer from the highest?

    I need to write a program that allows the user to input 4 numbers in one line: 80 50 40 10 etc.
    The program should output the difference between the lowest and highest number ex: 80-10 = 70.
    I am clueless about how to do this. I managed to get java to read the four numbers by creating substrings, I don't even know if that's right. Is is possible to convert those substring into an integer and get java to subtract the lowest from the highest? Is that even how to go about doing this? Please help I'm completely lost.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Subtracting the lowest integer from the highest?

    Well, first of all, how would you do this on paper? Since you have the solution right there in your post, you actually know how to do it.

    Break this solution you have in your mind down to smaller steps. For example, how do you know that 10 is the smallest? Let X be the smallest number. You iterate through the list, compare each number to X, and when you find a number that is smaller than X, then X will be the new number, until the list is done. Then you have the smallest number.

    Same story for the largest number.

    Here's a link to parsing the String to a int: Integer (Java Platform SE 7 )

  3. #3
    Iopu is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    Thanks for your reply. I still can't figure how to do it. I'm an absolute beginner btw. I don't know the syntax very well.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Subtracting the lowest integer from the highest?

    I don't know the syntax very well
    Do you have a design for the program or a list of steps that the program needs to do to solve the problem?
    Post them and we'll help you figure out the code for doing them.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    simple program logic

    prompt the user to enter the 1st integer
    prompt the user to enter the 2nd integer
    prompt the user to enter the 3rd integer
    prompt the user to enter the 4th integer
    store those integers in an array

    SHORTCUT sort the elements the array and take the last element minus it by the first

  6. #6
    Iopu is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    Thanks for replies. Unfortunately all of the numbers must be put in, in one line. Basically the code must read 4 two digit numbers like this: 10 30 50 70. Between 10 and 90 and output the difference between the biggest and smallest.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Subtracting the lowest integer from the highest?

    There are several ways to read in numbers from a user. The Scanner class has methods that will read in numbers from one line or from multiple lines.
    Or the full line with all the numbers can be read into a String and some methods of the String class can be used to separate the numbers. Then the Strings with separate numbers can be converted to int values using a method from the Integer class.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Subtracting the lowest integer from the highest?

    Quote Originally Posted by Iopu View Post
    Thanks for replies. Unfortunately all of the numbers must be put in, in one line. Basically the code must read 4 two digit numbers like this: 10 30 50 70. Between 10 and 90 and output the difference between the biggest and smallest.
    Think about it. This line "10 40 60 29". How do *YOU* know what the individual numbers are? You check for something that separates one number from the other. What separates them? Right, a space! Wouldn't it be handy, if you could *split* a String on a space? That's right, you can! Now that you have all the individual Strings, you can try converting them into a number. Maybe the Integer class has somehting for that.

    This is what programming is all about. This is the lesson your professor wants you to learn. It's not about those damn numbers. It's about breaking your huge problem down into managable chunks and solve each chunk individually.

  9. #9
    Iopu is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    I still can't get it. I know how to convert a string to an int but not 4 substrings. Is it possible to write this program using just the one string without the substrings? Or is it possible to convert 4 substrings to 4 integers?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Subtracting the lowest integer from the highest?

    Re-read post #8 and then read the API doc for the String class. It has a method the will split a String into separate Strings that have the numbers where you can easily get to them.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Subtracting the lowest integer from the highest?

    If you type multiple integers on multiple lines, the scanner class will simply read the next one it comes to following white space (using the appropriate method of course). It does not matter how they are split between lines, four lines of one integer or one line with four integers or two lines with two integers. However, if you only type in three and are expecting four then your program will wait forever for user input. Reading in a single line and parsing it gives you more error control so you can re-prompt the user if they typed bad info or insufficient info.

    As was stated earlier check out the String and Integer classes for helpful methods. You need to read all the methods to see what is available and then you need to try them. For new classes with which I am unfamiliar I and others usually do the same thing too. And you need to experiment with the various methods so you can see how they work. It will make your programming tasks much easier and more enjoyable.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    Iopu is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    Thanks guys that helps a lot. At least now I know what I am trying to do is possible. I was afraid to spend too much time at it in case I was going about it the wrong way.

  13. #13
    Iopu is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    So I've got the scanner to read in the numbers and store them as an integer. Now how would I go about subtracting the smallest from the largest and outputting the result?
    Btw this program is for an exam that's worth 15% of my overall grade. It's a practical programming exam. I won't be graded on this program that I am writing now. But it is taken from the sample of what we are expected to know. This is the one that I don't know how to do.
    Last edited by Iopu; 11-10-2013 at 11:28 AM.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: Subtracting the lowest integer from the highest?

    You can only subtract the smallest from the largest number if you know what the numbers are. You have to scan (iterate over) your array and find those numbers. Suppose you are about to check the number at index position i and you already know the smallest and largest numbers so far; this is true even if you have checked no numbers yet (i == 0) What if the number at that position is larger than the largest number so far? What if it is smaller? It's your turn now ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    Quote Originally Posted by Iopu View Post
    So I've got the scanner to read in the numbers and store them as an integer. Now how would I go about subtracting the smallest from the largest and outputting the result?
    a quick way to go about this is by sorting the elements of your array by calling Arrays.sort() method... it will sort the elements of your array in ascending order...
    when finished sorting you'll be definite that the 1st element is the smallest and the last is the bigest, you'll simply minus the 1st element from the last

    It's a short and simple way of doing it

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Subtracting the lowest integer from the highest?

    Seems like overkill to me. It would be much better to find the maximum and minimum of a set of numbers which should run in linear time. It is a basic coding skill and something this exercise was probably supposed to teach. If you're going to recommend a canned method in the API then you should point to the Math class, not a sorting algorithm.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: Subtracting the lowest integer from the highest?

    And of course my reply #14 is completely ignored again; probably that invisible ink issue again ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,050
    Rep Power
    6

    Default Re: Subtracting the lowest integer from the highest?

    Quote Originally Posted by JosAH View Post
    And of course my reply #14 is completely ignored again; probably that invisible ink issue again ...

    kind regards,

    Jos
    Nah, its because people like to set themselves up to fail nowadays. Not performing research, not wanting to read, not using their brain to reason about things, jumping blindly into writing code without a single moment of prior thought, asking for help and then spend a thread trying to ignore all the help you get, etc. etc.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Subtracting the lowest integer from the highest?

    Repetition is the key to getting their attention. Only posting it once must mean its not important. I find that posting 4 times often works.
    gimbal2 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Subtracting the lowest integer from the highest?

    Quote Originally Posted by jim829 View Post
    Seems like overkill to me. It would be much better to find the maximum and minimum of a set of numbers which should run in linear time. It is a basic coding skill and something this exercise was probably supposed to teach. If you're going to recommend a canned method in the API then you should point to the Math class, not a sorting algorithm.

    Regards,
    Jim
    Yep you are right, I was just being lazy
    I had a look at it, and did it the proper way... It didn't hurt a bit
    THANX

Similar Threads

  1. Getting/Printing the Highest to lowest
    By micolord in forum New To Java
    Replies: 12
    Last Post: 09-14-2011, 04:44 PM
  2. Need Lowest and Highest decimal point for 0.73 and 0.76
    By maran.ramar in forum New To Java
    Replies: 5
    Last Post: 11-15-2010, 01:58 AM
  3. cant get highest and lowest to display
    By tracey in forum New To Java
    Replies: 2
    Last Post: 05-16-2010, 08:48 AM
  4. QuickSort highest lowest Situation
    By Tenn in forum New To Java
    Replies: 17
    Last Post: 05-06-2009, 04:37 AM
  5. Replies: 21
    Last Post: 11-05-2008, 02:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •