aspose file tools*
The moose likes JDBC and the fly likes please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "please help" Watch "please help" New topic
Author

please help

DarrenColm Flynn
Greenhorn

Joined: Jun 02, 2004
Posts: 1
whats wrong with my code. im getting an error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at OSAspect1.main(OSAspect1.java:7)

any and all help appreciated. heres my code:


import java.util.*;
class OSAspect1
{
public static void main(String[] args)
{
System.out.println("1=FCCS 2=SHORTEST FIRST 3=PRIORITY 4=ROUND ROBIN");
String choice=args[0];
String BufferString;
int numberProcess=0;
if (choice.equals("1"))
{
System.out.println("First come,first served");
numberProcess=Integer.parseInt(args[1]);
int[] CpuBursts=new int[numberProcess];
for (int i=0;i<numberProcess ;i++ )
{
CpuBursts[i]=Integer.parseInt(args[i+2]);
System.out.println("CpuBursts["+i+"]= "+CpuBursts[i]);
}
int starttime=0;
int endtime=0;
int average=0; // waiting time is the time for the n-1 process to start ie 0+24+27 for process 24 ,3,3
for (int h=0;h<numberProcess ;h++ )
{
average=average+endtime;
endtime=endtime+CpuBursts[h];
System.out.println("Process "+(h+1)+" "+"priority= 1 CpuBursts= "+CpuBursts[h]+ " Start time = " +starttime+" end time="+endtime);
starttime=endtime;
}
System.out.println("average time in whole seconds is "+(average/numberProcess)+" secs");
}

if (choice.equals("2"))
{
System.out.println("Shortest job first");
numberProcess=Integer.parseInt(args[1]);
int[] Processes=new int[numberProcess];
System.out.println("numberprocess is equal to "+numberProcess);
int[] CpuBursts=new int[numberProcess];
for (int i=0;i<numberProcess ;i++ )
{
Processes[i]=i;
CpuBursts[i]=Integer.parseInt(args[i+2]);
System.out.println("CpuBursts["+i+"]= "+CpuBursts[i]);
}
for (int y=0;y<numberProcess ;y++ ) // sort algorithm
{
for (int t=0;t<numberProcess;t++ )
{
if (CpuBursts[y]<CpuBursts[t])
{
int temp=CpuBursts[y];
CpuBursts[y]=CpuBursts[t];
CpuBursts[t]=temp;
temp=Processes[y];
Processes[y]=Processes[t];
Processes[t]=temp;
}
}
}
int starttime=0;
int endtime=0;
double average=0;
for (int h=0;h<numberProcess ;h++ )
{
endtime=endtime+CpuBursts[h];
System.out.println((h+1)+" place "+"Process"+ (Processes[h]+1)+" "+"priority= 1 CpuBursts= "+CpuBursts[h]+ " Start time = " +starttime+" end time="+endtime);
average=average+starttime;
starttime=endtime;
}
System.out.println("average time in whole seconds is "+(average/numberProcess)+" secs");
}

if (choice.equals("3"))
{
System.out.println("Piority first");
numberProcess=Integer.parseInt(args[1]);
System.out.println("numberprocess is equal to "+numberProcess);
int[] Processes=new int[numberProcess];
int[] CpuBursts=new int[numberProcess];
for (int i=0;i<numberProcess ;i++ )
{
Processes[i]=i;
CpuBursts[i]=Integer.parseInt(args[i+2]);
System.out.println("CpuBursts["+i+"]= "+CpuBursts[i]);
}
int[] priority=new int[numberProcess];
for (int r=0;r<numberProcess ;r++ )
{
System.out.println("enter priority for process "+(r+1)+"and press enter");
priority[r]=Console.readInt();
}
for (int y=0;y<numberProcess ;y++ ) // sorting
{
for (int t=0;t<numberProcess;t++ )
{
if (priority[y]<priority[t])
{
int temp=priority[y];
priority[y]=priority[t];
priority[t]=temp;
temp=CpuBursts[y];
CpuBursts[y]=CpuBursts[t];
CpuBursts[t]=temp;
temp=Processes[y];
Processes[y]=Processes[t];
Processes[t]=temp;
}
}
}
int starttime=0;
int endtime=0;
double totalsum=0;
for (int h=0;h<numberProcess ;h++ )
{
endtime=endtime+CpuBursts[h];
System.out.println((h+1)+" place "+"Process"+ (Processes[h]+1)+" "+"priority= "+priority[h]+ " CpuBursts= "+CpuBursts[h]+ " Start time = " +starttime+" end time="+endtime);
totalsum=totalsum+starttime;
starttime=endtime;
}
System.out.println("average time in whole seconds is "+(totalsum/numberProcess)+" secs");
}

if (choice.equals("4"))
{
System.out.println("Round Robin first");
System.out.println("enter TimeQuantum and press enter");
int TimeQuantum=Console.readInt();

numberProcess=Integer.parseInt(args[1]);
System.out.println("numberprocess is equal to "+numberProcess);
int temp=1+(numberProcess+10);
int[] Processes=new int[numberProcess];
int[] CpuBursts=new int[numberProcess];
int[] TempCpuBursts=new int[temp];
int[] arraylocation=new int[temp];
int finishtime[]=new int[temp];
for (int i=0;i<numberProcess ;i++ )
{
Processes[i]=i;
TempCpuBursts[i]=Integer.parseInt(args[i+2]);
CpuBursts[i]=Integer.parseInt(args[i+2]);
System.out.println("CpuBursts["+i+"]= "+CpuBursts[i]);
}
int starttime=0;
int endtime=0;System.out.println("Processes");
for (int i=0;i<numberProcess ;i++ )
{
BufferString=""+(i+1)+" ";
System.out.print(BufferString.substring(0,8));
}
System.out.println("");
for (int i=0;i<numberProcess ;i++ )
{
BufferString=""+CpuBursts[i]+" ";
System.out.print(BufferString.substring(0,8));
}
int fifty=0;
System.out.println("");
System.out.println("Start=");
// round robin
int highest=0;
highest=CpuBursts[0];
for (int f=0;f<numberProcess;f++ )
{
if (CpuBursts[f]>highest)
{
highest=CpuBursts[f];
}
}
int divide= highest/TimeQuantum;

int WaitingTime[]=new int[numberProcess];
for (int u=0;u<divide+1;u++ )
{
System.out.println("");
for (int i=0;i<numberProcess ;i++ )
{
if (CpuBursts[i]>=TimeQuantum)
{
BufferString=""+fifty+" ";
System.out.print(BufferString.substring(0,8));
fifty=fifty+TimeQuantum;
if (CpuBursts[i]==TimeQuantum)
{
finishtime[i]=fifty-TempCpuBursts[i];

}
CpuBursts[i]=CpuBursts[i]-TimeQuantum;
}
else if (CpuBursts[i]==0)
{
System.out.print(" ");
}
else if (CpuBursts[i]<TimeQuantum)
{
BufferString=""+fifty+" ";
System.out.print(BufferString.substring(0,8));
fifty=fifty+CpuBursts[i];
finishtime[i]=fifty-TempCpuBursts[i];
CpuBursts[i]=0;
}
}
}
finishtime[numberProcess+1]=fifty;
double total=0;
for (int g=0;g<numberProcess ;g++ )
{
total=total+finishtime[g];


}
System.out.println("Average waiting time= "+(total/numberProcess));
}
}
}
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104

import java.util.*;
class OSAspect1
{
public static void main(String[] args)
{
System.out.println("1=FCCS 2=SHORTEST FIRST 3=PRIORITY 4=ROUND ROBIN");
String choice=args[0];

Line 7 would seem to be: String choice=args[0];
And this line would indeed result in an ArrayOutOfBoundsException if you ran the java program with no arguments.
 
wood burning stoves
 
subject: please help