# Printing out the highest value in the array

• 06-04-2013, 05:59 AM
grrttrdsll
Printing out the highest value in the array
I'm attempting (unsuccessfully) to print out the largest value that is passed into this array. How do I get it to do this (print out the largest value of i)?

Code:

```public static int pFactor(int numbers) {                           int max=0;                         int i;                         int n = Integer.parseInt(input);                         int pf;                     List<Integer> factors = new ArrayList<Integer>();                     for (i = 2; i <= n / i; i++) {                       while (n % i == 0) {                         factors.add(i);                               n /= i;                                             }                                         }                                         if (n > 1) {                       factors.add(n);                                                                                     }                                         return pf;                   }```
• 06-04-2013, 06:02 AM
Junky
Re: Printing out the highest value in the array
Declare a variable to store the highest value
Iterate over collection
Compare each value to highest
if it is higher reassign highest
• 06-04-2013, 06:03 AM
Junky
Re: Printing out the highest value in the array
You will need to carefully consider what the initial value of highest should be.
• 06-04-2013, 06:20 AM
grrttrdsll
Re: Printing out the highest value in the array
Isn't there just a command to print the the last value of the array (the last value is always the greatest in this array)?
• 06-04-2013, 06:30 AM
Fubarable
Re: Printing out the highest value in the array
Quote:

Originally Posted by grrttrdsll
Isn't there just a command to print the the last value of the array (the last value is always the greatest in this array)?

Yes, you could print out the last line of an array with a single line of code. Why not give it a try and show us what you come up with.

Note: the code you've posted in your first post doesn't have one bit of array work in it (not directly any way).
• 06-04-2013, 06:32 AM
Junky
Re: Printing out the highest value in the array
Quote:

Originally Posted by grrttrdsll
Isn't there just a command to print the the last value of the array (the last value is always the greatest in this array)?

It would be if the array was sorted but you never mentioned this.
• 06-04-2013, 06:54 AM
grrttrdsll
Re: Printing out the highest value in the array
What would that command be?
• 06-04-2013, 07:03 AM
grrttrdsll
Re: Printing out the highest value in the array
Derp...I just realized that this is a list (I'm just taking the professors code and implementing it per the homework assignment's instructions). Is there a simple way to return the last entry in the list? I basically just need the easiest way to return the last value.
• 06-04-2013, 07:08 AM
Junky
Re: Printing out the highest value in the array
What methods does the List have?
• 06-04-2013, 07:32 AM
grrttrdsll
Re: Printing out the highest value in the array
Its just dependent on the user's entered variable. It doesnt call on any other methods.
• 06-04-2013, 05:19 PM
jim829
Re: Printing out the highest value in the array
Not really. Just assign the first of the collection to highest. Then continue iterating from the second value.

Regards,
Jim
• 06-04-2013, 05:25 PM
JosAH
Re: Printing out the highest value in the array
Quote:

Originally Posted by jim829
Not really. Just assign the first of the collection to highest. Then continue iterating from the second value.

That's not needed; the way the OP stores the prime factors in the list is in increasing order, so the last one is always the largest element in the list.

kind regards,

Jos
• 06-04-2013, 05:32 PM
jim829
Re: Printing out the highest value in the array
I was considering the general case of a random list. I should have read the post more carefully instead of relying on the title. Mea Culpa.

Regards,
Jim
• 06-04-2013, 05:41 PM
JosAH
Re: Printing out the highest value in the array
Quote:

Originally Posted by jim829
I was considering the general case of a random list. I should have read the post more carefully instead of relying on the title. Mea Culpa.

Errare humanum est (or was it 'esse'? I can't remember; I never paid any attention during those lessons ... ;-)

kind regards,

Jos