Logic error in Java code to add hours and minutes -


can me find error in logic? have following 2 java files (main , time):

/*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */  import java.util.scanner; import java.io.*; /**  *  * @author dave  */ public class main {      /**      * @param args command line arguments      */     public static void main(string[] args) throws ioexception {          //array of class names         string[] classlist = new string[29];          //create file , scanner class list         file file = new file("c:\\users\\dave\\documents\\netbeansprojects\\tutordata\\src\\classes.txt");         scanner scan = new scanner(file);          int index = 0;         while(scan.hasnext()){             string s = scan.nextline();             classlist[index] = s;             index++;         }          //for(string s : classlist) system.out.println(s);          //parallel of array of total times         //set each time 0 hours , 0 minutes         time t = new time(0,0);         time[] total = new time[29];         for(int = 0; < 29; i++){             total[i] = t;         }          file data = new file("c:\\users\\dave\\documents\\netbeansprojects\\tutordata\\src\\tutordata.csv");         scan = new scanner(data);          index = 0;         while(scan.hasnext()){             string s = scan.nextline();             system.out.print(s + " ");             string[] tokens = s.split(",");             system.out.print(tokens[3]+ " " + tokens[5]+" ");             //split time token hours , minutes             string[] timetoken = tokens[3].split(":");             time timetoadd = new time(integer.parseint(timetoken[0]),integer.parseint(timetoken[1]));             system.out.print(" " + timetoadd.tostring() + " ");             //get class token             string classtoken = tokens[5];              //get time far class , add timetoadd             switch (classtoken) {                 case "itis 1212":                     total[0] = total[0].add(timetoadd);                     system.out.println("new total time: " +total[0].tostring());                     break;                 case "itis 1213":                     total[1] = total[1].add(timetoadd);                     system.out.println("new total time: " +total[1]);                     break;                 case "itis 2110":                     total[2] = total[2].add(timetoadd);                     system.out.println("new total time: " +total[2]);                     break;                 case "itis 2300":                     total[3] = total[3].add(timetoadd);                     system.out.println("new total time: " +total[3]);                     break;                 case "itis 3110":                     total[4] = total[4].add(timetoadd);                     system.out.println("new total time: " +total[4]);                     break;                 case "itis 3130":                     total[5] = total[5].add(timetoadd);                     system.out.println("new total time: " +total[5]);                     break;                 case "itis 3200":                     total[6] = total[6].add(timetoadd);                     system.out.println("new total time: " +total[6]);                     break;                 case "itcs 1212":                     total[7] = total[7].add(timetoadd);                     system.out.println("new total time: " +total[7]);                     break;                 case "itcs 1213":                     total[8] = total[8].add(timetoadd);                     system.out.println("new total time: " +total[8]);                     break;                 case "itcs 2175":                     total[9] = total[9].add(timetoadd);                     system.out.println("new total time: " +total[9]);                     break;                 case "itcs 2214":                     total[10] = total[10].add(timetoadd);                     system.out.println("new total time: " +total[10]);                     break;                 case "itcs 2215":                     total[11] = total[11].add(timetoadd);                     system.out.println("new total time: " +total[11]);                     break;                 case "itcs 3146":                     total [12] = total[12].add(timetoadd);                     system.out.println("new total time: " +total[12]);                     break;                 case "itcs 3152":                     total[13] = total[13].add(timetoadd);                     system.out.println("new total time: " +total[13]);                     break;                 case "itcs 3155":                     total[14] = total[14].add(timetoadd);                     system.out.println("new total time: " +total[14]);                     break;                 case "itcs 3160":                     total[15] = total[15].add(timetoadd);                     system.out.println("new total time: " +total[15]);                     break;                 case "itcs 3166":                     total[16] = total[16].add(timetoadd);                     system.out.println("new total time: " +total[16]);                     break;                 case "itcs 3181":                     total[17] = total[17].add(timetoadd);                     system.out.println("new total time: " + total[17]);                     break;                 case "itcs 4102":                     total [18] = total[18].add(timetoadd);                     system.out.println("new total time: " + total[18]);                     break;                 case "itcs 4120":                     total[19] = total[19].add(timetoadd);                     system.out.println("new total time: " +total[19]);                     break;                 case "itcs 4166":                     total[20] = total[20].add(timetoadd);                     system.out.println("new total time: " +total[20]);                     break;                 case "itcs 4180":                     total[21] = total[21].add(timetoadd);                     system.out.println("new total time: " +total[21]);                     break;                 case "math 1120":                     total[22] = total[22].add(timetoadd);                     system.out.println("new total time: " +total[22]);                     break;                 case "math 1241":                     total[23] = total[23].add(timetoadd);                     system.out.println("new total time: " +total[23]);                     break;                 case "math 1242":                     total[24] = total[24].add(timetoadd);                     system.out.println("new total time: " +total[24]);                     break;                 case "math 2164":                     total[25] = total[25].add(timetoadd);                     system.out.println("new total time: " +total[25]);                     break;                 case "stat 1220":                     total[26] = total[26].add(timetoadd);                     system.out.println("new total time: " +total[26]);                     break;                 case "stat 2122":                     total[27] = total[27].add(timetoadd);                     system.out.println("new total time: " +total[27]);                     break;                 case "stat 2223":                     total[28] = total[28].add(timetoadd);                     system.out.println("new total time: " +total[28]);                     break;             }             timetoadd = new time(0,0);             index++;         }          for(int = 0; < 29; i++) {             system.out.println(classlist[i] + " " + total[i].tostring());         }          scan.close();       }  }  /*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */  /**  *  * @author dave  */ public class time {      private int hour;     private int min;      public time(int hour, int min) {         this.hour = hour;         this.min = min;     }      public int gethour() {         return hour;     }      public void sethour(int hour) {         this.hour = hour;     }      public int getmin() {         return min;     }      public void setmin(int min) {         this.min = min;     }       public time add(time t) {         this.min += t.min;         if (this.min > 59) {             this.hour += this.min/60;             this.min = this.min%60;         }         this.hour += t.hour;          return this;      }      @override     public string tostring() {         return hour + ":" + min;     }    } 

and following 2 data files:

8/26/2013,3:30 pm,3:45 pm,0:15,mj,itis 2110,,,it infrastructure i, 8/27/2013,2:00 pm,4:00 pm,2:00,df,itcs 4120,,,intro graphics, 8/28/2013,9:00 am,10:30 am,1:30,df,itcs 3146,,,modern operating systems, 8/28/2013,10:30 am,12:00 pm,1:30,df,itcs 2214,,,data structures, 8/28/2013,2:00 pm,3:35 pm,1:35,"ap",itcs 2214,,,"induction, algorithm complexities", 

itis 1212 itis 1213 itis 2110 itis 2300 itis 3110 itis 3130 itis 3200 itcs 1212 itcs 1213 itcs 2175 itcs 2214 itcs 2215 itcs 3146 itcs 3152 itcs 3155 itcs 3160 itcs 3166 itcs 3181 itcs 4102 itcs 4120 itcs 4166 itcs 4180 math 1120 math 1241 math 1242 math 2164 stat 1220 stat 2122 stat 2223 

however output list of classes , total times same. can find error? thanks.

the error seems here

time t = new time(0,0); time[] total = new time[29]; for(int = 0; < 29; i++){     total[i] = t; } 

every place within array points same (not equal) time object have 1 single instance of class time. if change line to

total[i] = new time(0,0); 

than have 29 different instances of class time.

edit:

additionally, line timetoadd = new time(0,0); seems useless, because it's located @ end of while block , @ beginning of next iteration pointer set new object - there's no use set values 0.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -