Results 1 to 18 of 18
Thread: Testing which 'int' is highest?
- 03-15-2012, 12:22 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
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!Last edited by Etimer; 03-15-2012 at 12:37 AM.
- 03-15-2012, 12:38 AM #2
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
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.
Java 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); }Last edited by MonkeyMan; 03-15-2012 at 12:43 AM.
- 03-15-2012, 12:47 AM #3
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, 12:52 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
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, 12:55 AM #5
Re: Testing which 'int' is 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.need is to know which int is the highest.
You could either save the value itself or the index into the array of the value.
- 03-15-2012, 12:58 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
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, 01:08 AM #7
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, 01:26 AM #8
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
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.
Java 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, 01:28 AM #9
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.htmlLast edited by Norm; 03-15-2012 at 01:31 AM.
- 03-15-2012, 01:33 AM #10
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
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, 01:36 AM #11
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.
Your code has 0 comments.
You'll never know.being too lazy to open a compilerLast edited by Norm; 03-15-2012 at 01:38 AM.
- 03-15-2012, 01:39 AM #12
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
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, 01:45 AM #13
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
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, 02:35 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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.Last edited by pbrockway2; 03-15-2012 at 02:37 AM.
- 03-15-2012, 04:18 PM #15
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
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, 09:57 PM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: Testing which 'int' is highest?
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.)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.
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.
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.I won't step out of line and spoon feed people again, you can be assured of that :)
- 03-15-2012, 10:05 PM #17
Re: Testing which 'int' is highest?
You really should be writing a book. Or at a tutorial.
- 03-16-2012, 12:14 AM #18
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Similar Threads
-
Junit testing, testing list<e> interface
By mackavelirip in forum New To JavaReplies: 0Last Post: 10-05-2011, 06:08 AM -
Highest class of java?
By waqar100 in forum New To JavaReplies: 2Last Post: 06-11-2010, 04:37 AM -
cant get highest and lowest to display
By tracey in forum New To JavaReplies: 2Last Post: 05-16-2010, 08:48 AM -
Finding the highest number
By jigglywiggly in forum New To JavaReplies: 7Last Post: 11-04-2008, 08:14 AM -
The highest number
By I-Shine in forum Java AppletsReplies: 3Last Post: 02-13-2008, 05:05 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks