# Testing which 'int' is highest?

• 03-15-2012, 01:22 AM
Etimer
Testing which 'int' is highest?
How can I test to see which int in an array is highest?

Just to clear things up, I declared 4 'int's and set them all to random numbers with the Random utility.
Then I created an array and put them in it.

ALSO: I know that I could just do a bunch of 'if' statements or something, but i just wanted to know if there was a simple function to use.

Thanks!
• 03-15-2012, 01:38 AM
MonkeyMan
Re: Testing which 'int' is highest?
Looping through the array and storing the highest number with a handy if-statement gets you what you want :) I dunno if there's a really really simple array function to fetch the highest one, but at least this way you don't have to make a bunch of if-statements.

Code:

```      int[]highest = new int[4];       highest[0] = 1;       highest[1] = 4;       highest[2] = 3;       highest[3] = 2;       int i = 0;       int highestNumber = 0;       while(i < highest.length){           if( highest[i] > highestNumber){               highestNumber = highest[i];           }           System.out.println("The number at index " + i + " in the array is " + highest[i]);           i++;       }         System.out.println("The highest number is " + highestNumber);     }```
• 03-15-2012, 01:47 AM
Norm
Re: Testing which 'int' is highest?
A problem with the posted code: it assumes there is a positive number in the list.
• 03-15-2012, 01:52 AM
Etimer
Re: Testing which 'int' is highest?
Well what i need is to know which int is the highest.
For example, in your code, i would need it to print:
"The highest number is highest[1]"
Do you know how i could do that?
• 03-15-2012, 01:55 AM
Norm
Re: Testing which 'int' is highest?
Quote:

need is to know which int is the highest.
You would write a loop and compare all the ints in the list against the current highest one and save it if the new one was higher than the current highest.
You could either save the value itself or the index into the array of the value.
• 03-15-2012, 01:58 AM
Etimer
Re: Testing which 'int' is highest?
So I would have to write a bunch of 'if' statements? Like:

if(n1 > n2 && n1 > n3 && n1 > n4){
...
}else{
if(n2 > ....){
and so on?
• 03-15-2012, 02:08 AM
Norm
Re: Testing which 'int' is highest?
Are the numbers in an array? Then use a for loop and look at each element in the array one at a time by using the loop index to index into the array. Use an if statement to test each element against the highest one so far.

A trick: assume the first element is the highest and use its value as the current highest as you go through the loop comparing it to the rest of the elements in the array.
• 03-15-2012, 02:26 AM
MonkeyMan
Re: Testing which 'int' is highest?
Now this code gives you everything you need except negative figures, the highest int in the array, and the index the highest number is at. Unless this is what you're asking, we aren't understanding each other :) Just test it man, you'll see.

Code:

```      int[]highest = new int[4];       highest[0] = 1;       highest[1] = 4;       highest[2] = 8;       highest[3] = 2;       int i = 0;       int highestNumber = 0;       int indexWithHighest = 0;       while(i < highest.length){           if( highest[i] > highestNumber){               highestNumber = highest[i];               indexWithHighest = i;           }                     System.out.println("The number at index " + i + " in the array is " + highest[i]);           i++;       }         System.out.println("The highest number is " + highestNumber + " and it's located in index " + indexWithHighest);     } }```
• 03-15-2012, 02:28 AM
Norm
Re: Testing which 'int' is highest?
Why are you posting code instead of helping the OP learn how to solve the problem himself?

http://www.javaprogrammingforums.com...n-feeding.html
• 03-15-2012, 02:33 AM
MonkeyMan
Re: Testing which 'int' is highest?
Because I find that I learn better and A LOT faster myself when I have a working code and syntax I can test and pick apart myself. I get frustrated when people assume I know advanced functions and methods and syntax, that would take me days and weeks to find examples for online. When someone gives me the right syntax, I can immediately start following the logic and googling the parts I don't know, and I have saved enormous amount of time. Without examples, you only waste the newcomers time. Saying you're helping by not showing him anything that would help him, is a poor excuse for being too lazy to open a compiler.
• 03-15-2012, 02:36 AM
Norm
Re: Testing which 'int' is highest?
Ok, write an example that solves a similar problem that will require some thought to convert to the what is needed.
Also add enough comments to describe the logic and any tricks or special techniques that are used.

Quote:

being too lazy to open a compiler
You'll never know.
• 03-15-2012, 02:39 AM
Etimer
Re: Testing which 'int' is highest?
I just realized that i was doing this all wrong, there was a simpler way to do it but i just didn't realize it.
Sorry for putting you guys through all the trouble.
But thanks for helping me out, i learned a few things in the process!
• 03-15-2012, 02:45 AM
MonkeyMan
Re: Testing which 'int' is highest?
Thanks for the link, it was very informative. I understand where I went wrong and the thread does make a good point. Should have not given a directly correct answer and should have gone about it in a more round about way. However, I still insist that writing compilable code example (not a direct answer as you pointed out) tops verbal in-the-general-direction-mystery answers that assume the person can find examples for elsewhere.
• 03-15-2012, 03:35 AM
pbrockway2
Re: Testing which 'int' is highest?
@OP: I'm glad you've found a simpler way.

-----

The forum software doesn't give me a way of saying that I "Like" the first part of monkey's last post, but have trouble with the last sentence of it.

The sort of question in this thread is very common. And, fundamentally, the answer is always the same: "how would you solve this problem *without* using a computer?". Once an algorithm is arrived at the coding can begin.

Being conscious, aware and critical of how we solve problems is not a common trait. (And it is typically not taught at all.) Nor is precisely describing things (greatest array value vs the index of the greatest value). So I would concede that seeing a fully worked out solution may (if reflected upon) be helpful. But at some point the aspiring programmer must take the plunge and undertake the task of figuring out an algorithm themselves.

And it is precisely at that point that we meet them here. We can only assume that anyone posting a question here is, and intends to be, the author of their own code. They ought to be encouraged to develop an algorithm and test it with code. (It's their compiler, not Norm's that matters!) And plenty of people here can help with both the algorithm and its implementation in code by providing explanations of compiler messages, suggestions and comments about runtime oddities, and questions aimed at sharpening the OP's precision of both expression and thought.

That posters can find examples from elsewhere (their textbooks, and the good, the bad and the ugly that is the blogs and videos of teh internet) is not an assumption: it's a danger. Problems like the one that motivated this thread cry out, not for a lucky "find" - from here or elsewhere - but for some creative thought. We should encourage and help that creativity.
• 03-15-2012, 05:18 PM
MonkeyMan
Re: Testing which 'int' is highest?
I hear what you're saying and on a side note, very well written! You ever thought of writing a book of any sort? Was a pleasure reading that.

Anyhow you deepen the point the thread I was linked to made, but I can honestly say, I personally have learned 95% of everything I know about java from looking at code, friends code, classmates code, teachers code, youtube video code, online forums code and so on. Most I've learned from such code that was complete and compilable. I guess we could say not all people prefer to or bother to take time to understand why something works as long as it just works. I, however as you said, reflect upon the code before me, pick it apart, apply parts in different contexts, go back to my old projects and if I am able, to other peoples projects and snip the parts I need as that is the best way for me to learn. An example:

This project I'm doing, the one you've helped me with already :) Is one I got stuck with over and over and over again, and I'm 2 years past the initial deadline, simply because I did not understand jdbc, syntax, logic or even initially the term. I scrapped the project 4 times and gave up because no one would show me how and what to write and where. Then two weeks ago I found some youtube video from an indian sounding guy about jdbc syntax and populating jTables. He and his complete compileable example of a jdbc connection with a jtable is the only reason I am able to now complete this project and in the process I have not only learned a lot about jdbc but also many different items of interest and their syntax. Without that indian dood, I would not have been able to ever get this project finished or even well on its way nor had the pleasure of learning so much in the process. Not only that, but my confidence in my abilities to do java has increased exponentially because of this.

I can see a person being satisfied and relieved with a spoon-fed answer, but I would figure these people who don't take time to understand what they were shown, are the people sitting in their mandatory java class when they have zero passion and zero patience for programming, and would just like to be done with it. I don't personally figure myself gimping their learning process when they have no intention of learning in the first place.

If they want to learn java, they'll take time to understand even spoon-fed code like I do. However this is of course my humble personal opinion, and I won't step out of line and spoon feed people again, you can be assured of that :)
• 03-15-2012, 10:57 PM
pbrockway2
Re: Testing which 'int' is highest?
Quote:

I would figure these people who don't take time to understand what they were shown, are the people sitting in their mandatory java class when they have zero passion and zero patience for programming, and would just like to be done with it. I don't personally figure myself gimping their learning process when they have no intention of learning in the first place.
Oh yes - I'm not so silly as to imagine that the idealised poster I described in my previous post necessarily exists. They might - that is they might be keen and willing and merely need to be prodded and focussed. Or they might not - they may be as you described them. Who knows? On the internet nobody knows you're a lazy indifferent student. (But stick around! You'll soon find out there are plenty of clues.)

For me it's a "methodological" assumption. If the student/poster/whoever is indifferent and only wants teh codez, that's no fun. If you can ask a question and see them face in the right direction in order to answer it, that's amazing. It suits *me*, therefore to be optimistic and assume the best. Also it avoids error: I would prefer to pass by a dozen code beggars than to spoil the opportunity to learn of even one who was willing.

On the value of "finding things" and learning from them, I wonder if this might be a generational thing. (Of course, I'm assuming here about our relative ages). Personally, I read manuals. And only once I more or less understand the general terrain do I cast about for useful examples. Or it might just be personal preference.

Quote:

I won't step out of line and spoon feed people again, you can be assured of that :)
I don't know if the "rules" of the site address that. Or, for that matter, what they *do* address, because I haven't read them. It's a matter of more or less common agreement like the ethos of any community. Forums like this run on "ego" - I laugh at myself every time I glance at the "reputation", "thanks" or whatever! - and spoonfeeding is cheap. And then there's the real buzz to be obtained when you are able to put yourself in the poster's mindset and are the one who has explained some point (what references are, how to wrestle and tame the NullPointerException...) where others have failed.
• 03-15-2012, 11:05 PM
Norm
Re: Testing which 'int' is highest?
You really should be writing a book. Or at a tutorial.
• 03-16-2012, 01:14 AM
MonkeyMan
Re: Testing which 'int' is highest?
I agree with the above poster. Sir, you have the gift of silver tongue...err...fingers?