arrays - How to search the highest value with Java -
i have writtin below code search highest value in 1 dimensional array. it's not working due error. below code:
import java.io.*; public class thehigest{ int max = 0; int[] value = new int[5]; bufferedreader objinput = new bufferedreader(new inputstreamreader(system.in)); public static void main(string[]args){ thehigest obj6 = new thehigest(); obj6.input(); } void input(){ try{ for(int i=0;i<=4;i++){ system.out.println("==========================="); system.out.print("value input-"+(i+1)); value[i]= integer.parseint(objinput.readline()); system.out.println("==========================="); } } catch(exception e){ system.out.println("error "+ e); } } }
you have not yet implemented functionality searching highest element in array. can add little piece of code in input function itself. there no need sorting. it'll cause nlogn whereas can better traversing array once. cost o(n).
void input(){ try{ for(int i=0;i<=4;i++){ system.out.println("==========================="); system.out.print("value input-"+(i+1)); value[i]= integer.parseint(objinput.readline()); system.out.println("==========================="); } // searching highest element in array int highest = value[0]; for(int i=1;i<=4;i++){ if(value[i]>highest){ highest = value[i]; } } system.out.println("the highest :: "+ highest); } catch(exception e){ system.out.println("error "+ e); } } }
Comments
Post a Comment