Well obviously the initial values you assign to largest and smallest are wrong. Check out the Integer class.