Results 1 to 14 of 14
  1. #1
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

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

    Java 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;
    		  }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Printing out the highest value in the array

    You will need to carefully consider what the initial value of highest should be.

  4. #4
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Printing out the highest value in the array

    Quote Originally Posted by grrttrdsll View Post
    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).

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Printing out the highest value in the array

    Quote Originally Posted by grrttrdsll View Post
    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.

  7. #7
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing out the highest value in the array

    What would that command be?

  8. #8
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

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

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Printing out the highest value in the array

    What methods does the List have?

  10. #10
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

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

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,760
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Printing out the highest value in the array

    Quote Originally Posted by jim829 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,760
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Printing out the highest value in the array

    Quote Originally Posted by jim829 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. how to display the highest mark holder in an array
    By blackbonzai075 in forum New To Java
    Replies: 3
    Last Post: 01-03-2013, 11:09 PM
  2. Getting/Printing the Highest to lowest
    By micolord in forum New To Java
    Replies: 12
    Last Post: 09-14-2011, 04:44 PM
  3. Replies: 6
    Last Post: 07-28-2010, 09:21 AM
  4. Array highest grade
    By Cdlove in forum New To Java
    Replies: 5
    Last Post: 05-05-2010, 09:25 PM
  5. Replies: 21
    Last Post: 11-05-2008, 02:31 PM

Posting Permissions

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