data structures - Something is not working right with my java program -


i have developed average waiting time program has user enter processes, arrival time, etc. tells how long take looks has error , not sure how fix it. can me. error line of code is:

scanner sc = new scanner(system.in); 

the error reads exception in thread "main" java.lang.error: unresolved compilation problem: duplicate local variable sc

here code package project3;

import java.util.random; import java.util.scanner;  public class averagawaitingtime {      static int min(int b[], int a[], int tbt, int r, int n,int large[]) {              int j = 0;              int min = tbt;                 int l=0;//finding larger number of process in queue               (int = n - 1; >= 0; i--) {                  if (b[i] < min && b[i] > 0 && r >= a[i]) {                      min = b[i];                         l++;                     j = i;                  }              }                 if(large[0]<l)                     large[0]=l;              return j;         }       static int shortesfinishtime(int n,int p[],int at[],int bt[],int bt2[],int wt[],int tat[])     {           int tbt = 0, large[] = {0};              (int = 0; < n; i++) {                  tbt = tbt + bt[i];              }               int time[] = new int[tbt];              int k = 0;              int q2 = 0;                system.out.println("gantt chart");              system.out.print("|");              //bt[0] = bt[0] - 1;                (int = 0; < tbt; i++) {                  int q = min(bt, at, tbt, i, n,large);                  if (q != q2) {                      system.out.print(" p[" + p[q] + "]\t|");                      time[k++] = i;                      wt[q] = i;                      tat[q] = + bt[q];                  }                  bt[q] = bt[q] - 1;                  q2 = q;              }              time[k] = tbt;              system.out.println();              system.out.print("0\t");              (int = 0; <= k; i++) {                  system.out.print(time[i] + "\t");              }                 double awt=0;//average waiting time                  for(int i=0;i<n;i++)                 {                      awt=awt+wt[i];                 }                 awt=awt/n;                 system.out.println("\n average waiting time"+awt);                  return large[0];//returning max number of process in queue @ same time      }      static int shortestsizetime(int n,int process[],int ptime[],int wtime[])     {         int temp, total=0;         float avg=0;          for(int i=0;i<n-1;i++)         {             for(int j=i+1;j<n;j++)           {                              if(ptime[i]>ptime[j])                 {                     temp = ptime[i];                     ptime[i] = ptime[j];                     ptime[j] = temp;                     temp = process[i];                     process[i] = process[j];                     process[j] = temp;                 }             }         }          wtime[0] = 0;         for(int i=1;i<n;i++)         {             wtime[i] = wtime[i-1]+ptime[i-1];             total = total + wtime[i];         }         avg = (float)total/n;         system.out.println("p_id p_time w_time");         for(int i=0;i<n;i++)         {             system.out.println(process[i]+"\t"+ptime[i]+"\t"+wtime[i]);         }         system.out.println("total waiting time: "+total);         system.out.println("average waiting time: "+avg);          return n-1;     }      public static void main(string argv[])     {         //variable declaration         int n=100;//number of processes         int p[]=new int[n];         int a[]=new int[n];//to store arrival time of 100 processes         int b[]=new int[n];//to store service/burst/processing time of 100 processes         int b2[]=new int[n];//to store service/burst/processing time of 100 processes         int w[]=new int[n];//to store waiting time of 100 processes         int tat[]=new int[n];//to store turaround time of 100 processes         int i,j;         int quanta;//time slice round robin         double round_robin,fcfs;          scanner sc = new scanner(system.in);         system.out.println("enter no of processes");          n=sc.nextint();          system.out.println("enter arrival time");         for(i=0;i<=n;i++);          {             a[i]= sc.nextint();         }          system.out.println("enter burst time");         for(i=0;i<=n;i++)         {             b[i]=sc.nextint();             if(b[i]<0){ b[i]=b2[i]=-1*b[i];}          }         random r= new random(); //to generate random number         //generating randomly arrival time 0 100          for(i=0;i<n;i++)         {             p[i]=i+1;             a[i]=r.nextint()%101; //randomly number between 0 100 inclusive         }          //randomly burst/processing times 0 100]         for(i=0;i<n;i++);         {             b[i]=(int)r.nextint()%101; //random number 0 100 inclusive             b2[i]=b2[i];              if(b[i]<0){ b[i]=b2[i]=-1*b[i];}         }         string ans1="";                 {             int c;             string ans="y";             scanner sc = new scanner(system.in);             system.out.println("enter 1: shortestfinish time algorithm \n 2: shortest size algorithm ");             c=sc.nextint();              if(c==1)             {             system.out.println("shortest finish time:");             //calling shortest finish time algorithm              system.out.println("longest number of processes in queue:"+shortesfinishtime(n,p,a,b,b2,w,tat));         }             else             {                 system.out.println("shortest size:");                 //calling shortest size algorithm                  system.out.println("longest number of process in queue:"+shortestsizetime(n,p,b,w));              }             system.out.println("do want continue(y/n)");             ans1 = sc.next();         }while(ans1.equals("y"));           } } 

thank you

in main method declare new scanner sc twice. once initially, , second time in loop.

to remedy in loop remove line:

 scanner sc = new scanner(system.in); 

as have declared above


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -