Results 1 to 18 of 18
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Testing which 'int' is highest?

  1. #1
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default 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.

  2. #2
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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.

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

    Default Re: Testing which 'int' is highest?

    A problem with the posted code: it assumes there is a positive number in the list.

  4. #4
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default 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?

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

    Default Re: Testing which 'int' is highest?

    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.

  6. #6
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default 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?

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

    Default 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.

  8. #8
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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);
        }
    
    }

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

    Default 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
    Last edited by Norm; 03-15-2012 at 01:31 AM.

  10. #10
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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.

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

    Default 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.


    being too lazy to open a compiler
    You'll never know.
    Last edited by Norm; 03-15-2012 at 01:38 AM.

  12. #12
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default 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!

  13. #13
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  15. #15
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default 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 :)

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Testing which 'int' is highest?

    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.

    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.
    Norm and yellowledbet like this.

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

    Default Re: Testing which 'int' is highest?

    You really should be writing a book. Or at a tutorial.

  18. #18
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Testing which 'int' is highest?

    I agree with the above poster. Sir, you have the gift of silver tongue...err...fingers?

Similar Threads

  1. Junit testing, testing list<e> interface
    By mackavelirip in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 06:08 AM
  2. Highest class of java?
    By waqar100 in forum New To Java
    Replies: 2
    Last Post: 06-11-2010, 04:37 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. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 08:14 AM
  5. The highest number
    By I-Shine in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 05:05 AM

Posting Permissions

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