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.

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 )

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.

Re: Subtracting the lowest integer from the highest?

Quote:

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.

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

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.

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.

Re: Subtracting the lowest integer from the highest?

Quote:

Originally Posted by

**Iopu** 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.

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?

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.

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

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.

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.

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

Re: Subtracting the lowest integer from the highest?

Quote:

Originally Posted by

**Iopu** 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

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

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

Re: Subtracting the lowest integer from the highest?

Quote:

Originally Posted by

**JosAH** 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.

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.

Re: Subtracting the lowest integer from the highest?

Quote:

Originally Posted by

**jim829** 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