cannot find symbol error in line 15 and 18 - java

I can't fix my code. It always says "cannot find symbol" error. I tried to search for some solution but still doesn't work.
import java.util.*;
public class NPP{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int numOfJobs;
float waitingTimeAve = 0, turnaroundTimeAve = 0;
System.out.println("Enter number of jobs : ");
numOfJobs = scan.nextInt();
Job[] job = new Job[numOfJobs]; // Line 15
for(int i = 0; i < numOfJobs; i++ ){
job[i] = new Job(); // Line 18
System.out.println("For job " + (i+1));
System.out.print("Enter arrival time: ");
job[i].setArrivalTime(scan.nextInt());
System.out.print("Enter burst time: ");
job[i].setBurstTime(scan.nextInt());
System.out.print("Enter priority: ");
job[i].setPriority(scan.nextInt());
System.out.println();
}
System.out.println("Type go.");
scan.next();
int jobNum = 0, jobAt = 1, jobBt = 2, jobPrio = 3;
int currentTime;
int [][]timeline = new int[numOfJobs][4];

import java.util.*;
public class NPP{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int numOfJobs;
float waitingTimeAve = 0, turnaroundTimeAve = 0;
System.out.println("Enter number of jobs : ");
numOfJobs = scan.nextInt();
Job[] job = new Job[numOfJobs]; //LINE15
for(int i = 0; i < numOfJobs; i++ ){
job[i] = new Job(); //LINE18
System.out.println("For job " + (i+1));
System.out.print("Enter arrival time: ");
job[i].setArrivalTime(scan.nextInt());
System.out.print("Enter burst time: ");
job[i].setBurstTime(scan.nextInt());
System.out.print("Enter priority: ");
job[i].setPriority(scan.nextInt());
System.out.println();
}
System.out.println("Type go.");
scan.next();
int jobNum = 0, jobAt = 1, jobBt = 2, jobPrio = 3;
int currentTime;
int [][]timeline = new int[numOfJobs][4];
for(int i = 0; i < numOfJobs; i ++){
timeline[i][jobNum] = i;
timeline[i][jobAt] = job[i].getArrivalTime();
timeline[i][jobBt] = job[i].getBurstTime();
timeline[i][jobPrio] = job[i].getPriority();
}
Arrays.sort(timeline, new Comparator<int[]>(){
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[jobAt], o2[jobAt]);
}
});
currentTime = timeline[0][jobAt];
int [][]ganttchart = new int [100][2];
int g = 0;
for(int i = 0; i < numOfJobs; i++){
int n = 0;
int temp = i;
if(timeline[i][jobAt] > currentTime){
int idle = timeline[i][jobAt] - currentTime;
currentTime += idle;
}
while(timeline[i][jobAt] <= currentTime){
n++;
i++;
if(i == numOfJobs){
break;
}
}
i = temp;
if(n > 1){
for(int j = i; j < n+i; j++){
for(int k = j; k < n+i; k++){
if(timeline[j][jobPrio] > timeline[k][jobPrio]){
int temp1 = timeline[j][jobNum];
int temp2 = timeline[j][jobAt];
int temp3 = timeline[j][jobBt];
int temp4 = timeline[j][jobPrio];
timeline[j][jobNum] = timeline[k][jobNum];
timeline[j][jobAt] = timeline[k][jobAt];
timeline[j][jobBt] = timeline[k][jobBt];
timeline[j][jobPrio] = timeline[k][jobPrio];
timeline[k][jobNum] = temp1;
timeline[k][jobAt] = temp2;
timeline[k][jobBt] = temp3;
timeline[k][jobPrio] = temp4;
}
}
}
}
int temp4 = timeline[i][jobNum];
currentTime += job[temp4].getBurstTime();
job[temp4].setEndTime(currentTime);
ganttchart[g][0] = timeline[i][jobNum];
ganttchart[g][1] = currentTime;
g++;
}
for(int i = 0; i < numOfJobs; i++){
int wt, tt;
tt = job[i].getEndTime() - job[i].getArrivalTime();
job[i].setTurnaroundTime(tt);
turnaroundTimeAve +=tt;
wt = job[i].getTurnaroundTime() - job[i].getBurstTime();
job[i].setWaitingTime(wt);
waitingTimeAve+=wt;
}
turnaroundTimeAve = turnaroundTimeAve/numOfJobs;
waitingTimeAve = waitingTimeAve/numOfJobs;
for(int i = 0; i < numOfJobs; i++){
System.out.println("Job " + (i+1));
System.out.println("End time is " + job[i].getEndTime());
System.out.println("Turnaround time is " + job[i].getTurnaroundTime());
System.out.println("Waiting time is " + job[i].getWatingTime());
System.out.println();
}
System.out.println("Turnaround time average is " + turnaroundTimeAve);
System.out.println("Waiting time average is " + waitingTimeAve);
System.out.println("Gantt Chart: ");
for( int i = 0; i < g; i++){
System.out.println("Job " + (ganttchart[i][0]+1) + " end time of " + ganttchart[i][1]);
}
}
}

Related

How do I change the output of burst time

The code displays 0 instead of every 3rd input in the given values. What should I do in order to print out the 5 3 8 6 in the input?
The output should be like this (burst time reference only):
import java.util.Scanner;
public class PreemptivePriorityScheduling {
static int[] processId, arrivalTime, burstTime, priority, waitingTime, turnAroundTime;
static int n;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
n = input.nextInt();
processId = new int[n];
arrivalTime = new int[n];
burstTime = new int[n];
priority = new int[n];
waitingTime = new int[n];
turnAroundTime = new int[n];
getInput(input);
computeWaitingTime();
computeTurnAroundTime();
displayTable();
displayAverages();
}
public static void getInput(Scanner input) {
for (int i = 0; i < n; i++) {
System.out.print("Enter process ID, arrival time, burst time, and priority for process " + (i + 1) + ": ");
processId[i] = input.nextInt();
arrivalTime[i] = input.nextInt();
burstTime[i] = input.nextInt();
priority[i] = input.nextInt();
}
}
public static void computeWaitingTime() {
int complete = 0, minPriority, minIndex = 0;
for (int i = 0; i < n; i++) {
minPriority = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
if (arrivalTime[j] <= complete && priority[j] < minPriority && burstTime[j] > 0) {
minPriority = priority[j];
minIndex = j;
}
}
complete += burstTime[minIndex];
waitingTime[minIndex] = complete - arrivalTime[minIndex] - burstTime[minIndex];
burstTime[minIndex] = 0;
}
}
public static void computeTurnAroundTime() {
for (int i = 0; i < n; i++) {
turnAroundTime[i] = waitingTime[i] + burstTime[i];
}
}
public static void displayTable() {
System.out.println("Process ID\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurn Around Time");
for (int i = 0; i < n; i++) {
System.out.println(processId[i] + "\t\t" + arrivalTime[i] + "\t\t" + burstTime[i] + "\t\t" + priority[i] + "\t\t" + waitingTime[i] + "\t\t" + turnAroundTime[i]);
}
}
public static void displayAverages() {
int totalWaitingTime = 0, totalTurnAroundTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += waitingTime[i];
totalTurnAroundTime += turnAroundTime[i];
}
System.out.println("\nAverage Waiting Time: " + (float)totalWaitingTime / n);
System.out.println("Average Turn Around Time: " + (float)totalTurnAroundTime / n);
}
}

Runtime error on test 1 (Code Forces) but workes in netbeans

I am new to code forces, when i run this code in NetBeans its works correctly but when I submit it on code forces it gives me Runtime error on test 1. what is the wrong?
This is the problem
import java.util.Scanner;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String first = input.nextLine();
String second = input.nextLine();
input.close();
String first_line[] = first.split(" ");
String second_line[] = second.split(" ");
int first_numbers[] = new int[first_line.length];
int second_numbers[] = new int[second_line.length];
for (int i = 0; i < 2; i++) {
if (Integer.parseInt(first_line[i]) >= 1)
first_numbers[i] = Integer.parseInt(first_line[i]);
else
first_numbers[i] = 0;
}
for (int i = 0; i < second_line.length; i++)
if (Integer.parseInt(second_line[i]) >= 1)
second_numbers[i] = Integer.parseInt(second_line[i]);
else
second_numbers[i] = 0;
int x = 0;
try {
for (int i = 0; first_numbers[1] < second_numbers[i]; i++)
x++;
} catch (Exception ex) {
}
System.out.println(x);
}
What if you modify your last for loop as below
int x = 0;
for (int i=0; i<second_numbers.length; i++) {
if (first_numbers[1] < second_numbers[i])
x++;
}
why you split String int those
String first_line[] = first.split(" ");
String second_line[] = second.split(" ");
int first_numbers[] = new int[first_line.length];
int second_numbers[] = new int[second_line.length];
you can initialize int from the beginning and use it 2 int like the following
Scanner input = new Scanner(System.in);
int k = input.nextInt();
int n = input.nextInt();
int cout = 0;
int[] arr = new int[k];
and here you are my complete solution for this problem from 2014
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
int counter = 0, tk = 0;
int a[] = new int[n];
for(int i = 0; i < n; ++i){
a[i] = input.nextInt();
if((i + 1) == k){
tk = a[i];
}
}
for(int i = 0; i < n; ++i){
if(a[i] > 0 && a[i] >= tk){
counter++;
}
}
System.out.println(counter);
}
i wish i can help
if i can advice you
try to make it simple as much as you can in these problem at beginning.

How to set number range from user input into a 2d array

just wanting to know how do i set an input range for the scanner object, which then transfers the values and inputs them into a 2d array.
I was able to set a range for random number generator but i do not know how to apply the same concept to my scanner object
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int rows = 3;
int columns = 5;
System.out.println("Enter array elements : ");
int arry1[][] = new int[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns-2; j++)
{
arry1[i][j] = sc.nextInt();
}
}
arry1[0][3] = random.nextInt(50-10+1)+10;
arry1[1][3] = random.nextInt(50-10+1)+10;
arry1[2][3] = random.nextInt(50-10+1)+10;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
sum1 = arry1[0][0]+arry1[0][1]+arry1[0][2]+arry1[0][3];
sum2 = arry1[1][0]+arry1[1][1]+arry1[1][2]+arry1[1][3];
sum3 = arry1[2][0]+arry1[2][1]+arry1[2][2]+arry1[2][3];
arry1[0][4] = sum1;
arry1[1][4] = sum2;
arry1[2][4] = sum3;
System.out.print("DISPLAYING ARRAY:");
System.out.println();
for (int[] x1 : arry1) {
for (int y1 : x1) {
System.out.print(y1 + " ");
}
System.out.println();
}
}
}
Add checking if the value is within range.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int rows = 3;
int columns = 5;
System.out.println("Enter array elements : ");
int arry1[][] = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns - 2; j++) {
int input;
do {
input = sc.nextInt();
} while (input < 0 || input > 3);
arry1[i][j] = input;
}
}
arry1[0][3] = random.nextInt(50 - 10 + 1) + 10;
arry1[1][3] = random.nextInt(50 - 10 + 1) + 10;
arry1[2][3] = random.nextInt(50 - 10 + 1) + 10;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
sum1 = arry1[0][0] + arry1[0][1] + arry1[0][2] + arry1[0][3];
sum2 = arry1[1][0] + arry1[1][1] + arry1[1][2] + arry1[1][3];
sum3 = arry1[2][0] + arry1[2][1] + arry1[2][2] + arry1[2][3];
arry1[0][4] = sum1;
arry1[1][4] = sum2;
arry1[2][4] = sum3;
System.out.print("DISPLAYING ARRAY:");
System.out.println();
for (int[] x1 : arry1) {
for (int y1 : x1) {
System.out.print(y1 + " ");
}
System.out.println();
}
}

3rd inner while loop not working

In the blow code the inner 3rd while loop not working please tell me why ?
Here I tried with for loop by replacing the 3rd inner while loop it is working correctly but why not working with while loop .....? can you give me genuine reason...?
import java.util.Scanner;
public class MergeArray {
void arrayInitialization(Scanner arg) {
//entering test cases
System.out.println("enter test cases");
int t = arg.nextInt();
int k, l, i;
k = 0;
l = 0;
i = 0;
//outer while loop
while (t-- > 0) {
//initializing a1[]'s size
System.out.println("enter a1[]'s size");
int as1 = arg.nextInt();
int a1[] = new int[as1];
//inner while loop-1
while (as1-- > 0) {
System.out.println("enter a1[]'s elements");
a1[i] = arg.nextInt();
System.out.print(a1[i]);
i++;
}
i = 0;
//initializing a2[]'s size
System.out.println("enter a2[]'s size");
int as2 = arg.nextInt();
int a2[] = new int[as2];
//inner while loop-2
while (as2-- > 0) {
System.out.println("enter a2[]'s elements");
a2[i] = arg.nextInt();
System.out.print(a2[i]);
i++;
}
System.out.println();
int a3[] = new int[a1.length + a2.length];
int size = as1 + as2;
//inner while loop-3
while (size-- > 0) {
if (k < a1.length)
a3[l] = a1[k];
if (k < a2.length)
a3[l + 1] = a2[k];
k++;
l += 2;
}
for (int j = 0; j < (a1.length + a2.length); j++) {
System.out.print(a3[j]);
}
}
}
public static void main(String[] args) {
MergeArray ma = new MergeArray();
Scanner sc = new Scanner(System. in );
ma.arrayInitialization(sc);
}
}
I tried so much but not found solution. Here I am using while loop because I know that while loop will work fast instead of for loop.
It does not work because you are decrementing the sizes of as1 and as2. Which will be
int size = as1 + as2; // size = 0 + 0;
Instead you can make use of the array length e.g.
int size = as1.length + as2.length;
Murat K's Answer is right, but try to init the arrays like this:
//init a1
System.out.println("enter a1[]'s size");
int a1[] = new int[arg.nextInt()];
//fill a1
for (int i = 0; i < a1.length; i++) {
System.out.println("enter element " + i + " of a1[]");
a1[i] = arg.nextInt();
}
//init a2
System.out.println("enter a2[]'s size");
int a2[] = new int[arg.nextInt()];
//fill a2
for (int i = 0; i < a2.length; i++) {
System.out.println("enter element " + i + " of a2[]");
a2[i] = arg.nextInt();
}
//init a3
int a3[] = new int[a1.length + a2.length];
//merge a1 and a2 into a3
for (int i = 0; i < a1.length; i++) {
a3[i] = a1[i];
}
for (int i = 0; i < a2.length; i++) {
a3[a1.length + i] = a2[i];
}
//print
for (int i : a3) {
System.out.print(i);
}

Simple Java Fibonacci code issue

import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");
count = in.nextInt();
int[] fib = new int [count];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i<count; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
for(int i=0; i<count; i++)
{
System.out.print(fib[i] + " ");
}
}
}
This is my very simple Fib program, what i cant figure out is why it always stops one number short. For example:
run: Please enter number 6 1 1 2 3 5 8 BUILD SUCCESSFUL (total time: 5
seconds)
run: Please enter number 7 1 1 2 3 5 8 13 BUILD SUCCESSFUL (total
time: 5 seconds)
I thought in my FOR loops it should be "(int i=2; i <= count;"
but when i put in greater than or equal to in both, or either FOR loop it gives me an error
Any suggestions? i know its something easy i'm overlooking
Your code is giving correct output. but still if you need one more element try to initialize array with count + 1 and then have your loop running for i <= count
public static void main(String[] args) {
int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");
count = in.nextInt();
int[] fib = new int [count+1];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i <= count; i++){
fib[i] = fib[i-1] + fib[i-2];
}
for(int i=0; i <= count; i++){
System.out.print(fib[i] + " ");
}
}
}
Arrays are zero-based. This means, that (assuming count = 5) if you have the following array:
int[] fib = new int[5];
then you can access fib[0], fib[1], fib[2], fib[3] and fib[4]. So
for (int i = 0; i < 5; i++) {
System.out.print(fib[i] + " ");
}
would be fine. As it would access everything in fib, starting with index 0, and stopping with the last index smaller than 5, which is 4. However, if you do:
for (int i = 0; i <= 5; i++) {
System.out.print(fib[i] + " ");
}
then you will access the last index smaller than OR EQUAL TO 5, which is 5. But, as stated before, fib[5] is invalid. That's what gives you your error.
A simpler solution is to avoid needing an array in the first place and you don't need to get the size right.
public static void main(String[] args) {
System.out.println("Please enter a number");
Scanner in = new Scanner(System.in);
int count = in.nextInt();
long a = 1, b = 1;
for(int i = 0; i < count; i++) {
System.out.print(a + " ");
long c = a + b;
a = b;
b = c;
}
System.out.println();
}
There should be one more array element space for int fib[], thus the fib[count] could be stored.
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");
count = in.nextInt();
int[] fib = new int [count + 1];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i <= count; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
for(int i = 0; i<= count; i++)
{
System.out.print(fib[i] + " ");
}
}
}
public class Fibonacci
{
private int [] fibArray;
public Fibonacci()
{
}
public void Fibonacci()
{
fibArray = new int[0];
}
public void setFibonnaci(int size)
{
fibArray = new int[size];
if(fibArray.length == 1)
{
fibArray [0] = 0;
}
else if(fibArray.length == 2)
{
fibArray[0] = 0;
fibArray[1] = 1;
fibArray[2] = 2;
}
else
{
fibArray[1] = 1;
fibArray[0] = 0;
for(int x = 2; x < fibArray.length; x++)
{
fibArray [x] = fibArray[x-1] + fibArray[x-2];
}
}
}
public int getSequence(int number)
{
if(number -1 < fibArray.length)
{
return fibArray[number - 1];
}
return -1;
}
//check the test case for getFibo
public String toString()
{
String output = "";
for (int x = 0; x < fibArray.length; x++)
{
output += x + " - " + fibArray[x];
}
return output;
}
}
Late response but new to site and just trying to help. This fib class works 100%

Categories