• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding max number in array  RSS feed

 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this has to be simple.  Trying to find the max number in my Student array in the student ID.
My intellisense error says "non-static variable ID cannot be referenced from a static content.

Can someone point me to a tutorial or something to help get some understanding.

 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You've defined and initialized Roster as an ArrayList of Students. Therefore, to access an individual Student you'd need to do Roster.get(i) and to get the Student's ID it would be Roster.get(i).ID. Even better, provide the Student class with a getter: getId(). Then it would be Roster.get(i).getId();

Note that Roster and ID should begin with a lower case letter.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using do while loop, but there are no while. Can you post the complete code?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also Student.ID is not a list, Roster is a list, so you'd end up with
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for getting me going in the right direction.  So Casey I did this.



Looked logical to me then I compiled and ran.....

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at Roster.main(Roster.java:28)
C:\Users\David\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

My Brain is melting..
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to be doing two things wrong. One is to use a completely empty List; when you try to find its first element (index=0) you can't and the JVM complains with that exception.
The other seems to be that you are only looking at the first element in the List; you shou‍ld look at them all.
Starting with MIN_VALUE and then reassigning whenever you find a larger i looks correct for finding the largest.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Note that if your roster list is empty then large will be MIN_VALUE after the loop.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to find the max number in my Student array 

Another way is to get the first number in the array and compare that against the other numbers int the array always saying a number if it is larger.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops in my previous post. I re-edited with the correction.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or for something completely different: have a static or instance field, maxID, initially set to 0. Whenever a new student is added, increase maxID and set the students ID to this value. Do not let the new ID depend in any way on the current roster.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!