java - Function to return Maximum Value in Array works only when array has one item -


i created function identify maximum value @ array. function call works when array has 1 item. can check code below , tell me think?

output receive

the highest grade student is: grade: 0.0

output needed (example)

the highest grade student is: 976 grade: 90.0

highest grade function:

public static string highestgrade(student[] d)    {    // function checks highest grade , returns corresponding id     // d array of student data type                     double max = d[0].getscore();  // assigning max value reference     int gradecounter = 1; // counter     string topid = "";  // emplty string      // looping through array     for(  ; gradecounter< d.length; gradecounter++ )     {        if( d[gradecounter].getid() != "")         // checking if there id assigned        {   // comparing score @ index max value            if(d[gradecounter].getscore() > max)            {   // if score higher max value, max updated               max = d[gradecounter].getscore();               topid=d[gradecounter].getid();             }         }      }     return topid; // returning id corresponds highest grade } 

print report function

public static void printreport(student[] c) {     system.out.print("\n ***class report*** \n\n");     // looping through array , printing both id , grade     for(int idcounter = 0; idcounter<c.length; idcounter++ )     {         if( c[idcounter].getid() != "")         {             system.out.print("id: ");              system.out.print(c[idcounter].getid());               system.out.print(" grade: ");             system.out.println(c[idcounter].getgrade());         }     }  //*******this part has issue*************      // providing user id having highest grade             system.out.print("\nthe highest grade student is: ");        // assigning variable function call of highestgrade id         string studenthighestgrade=highestgrade(c);      // printing variable  provide id     system.out.print(studenthighestgrade);      // providing user  grade corresponds id     system.out.print(" grade: ");        // declaring , initializing variable     double valueofhighestgrade = 0.0;      // looping through array highest grade     // corresponds id     for(int idcounter = 0; idcounter<c.length; idcounter++ )     {      // if id @ index =idcounter equals highest id      // grade (score) @ index , assign       // valueofhighestgrade       if(c[idcounter].getid().equals(studenthighestgrade))       {           valueofhighestgrade=c[idcounter].getscore();       }      }     // printing highest grade (score)     system.out.print(valueofhighestgrade);      countgrades( c);      system.out.print("\n ***end of report*** \n"); } 

if have 1 student in array, doing int gradecounter = 1; // counter not value of student id,

so before loop in highestgrade do

topid = d[0].getid();  

not sure why doing if (c[idcounter].getid() != "") though


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -