Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m hoping someone can tell me why my code isn\'t working properly. everything

ID: 3718740 • Letter: I

Question

I'm hoping someone can tell me why my code isn't working properly. everything seems to run fine but my "total" array int at the very end gives me an error that it isnt initialized. I'm unsure of how i can initialize it properly. I have considered just creating and calling a new method which seems like it could work but i'd rather not redew a large chunk of what i've already done thus far. I have also considered removing the while loop and only having the for loop that calls a method at the end but that doesn't seem quite right and I couldnt get it in there properly. Below is the also the full assignment discription:

Using the above input file of orders, rewrite the Spacely Sprocket program to produce a summary report for all 50 sprockets produced. Rather than using 50 separate accumulator variables, you must use a single array to represent the 50 different accumulators. Use the array and loops in your program to summarize the orders and output the report. Don't forget to initialize each of the array locations to 0 (because they are accumulators). For full credit, be sure to display the report in a Scrolling Pane in a Window.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sprocketswitharrays;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
*
* @author brett
*/
public class SprocketsWithArrays {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

JFrame jf;
JTextArea jta;
JScrollPane jsp;
  
jf = new JFrame();
jta = new JTextArea();

jsp = new JScrollPane(jta);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
  
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
jf.add(jsp);
jf.setVisible(true);

int snum, quantity, z = 0;

InputFile novemberOrders;
novemberOrders = new InputFile("novemberOrders.txt");
  

while (z < 1000 && !novemberOrders.eof()){
int[] total;   
total = new int [1000];
snum = novemberOrders.readInt();
quantity = novemberOrders.readInt();

total[snum-1] = total[snum-1] + quantity;

}
for (int i = 0; i < 1000;){
jta.append((i+1) + " " + total[i] + " ");
i++;

}

}

}

Explanation / Answer

From the code snippet that you provided i have copied the last while and for loop code below

while (z < 1000 && !novemberOrders.eof()){
int[] total;   
total = new int [1000];

snum = novemberOrders.readInt();
quantity = novemberOrders.readInt();

total[snum-1] = total[snum-1] + quantity;

}
for (int i = 0; i < 1000;){
jta.append((i+1) + " " + total[i] + " ");
i++;

}

From the above code, you have declared the in array of total inside the while loop.

As you see that variables you declare inside the while loop cannot be accessed outside to that loop. But you have tried to access the variable total inside the for loop so which it throws the exception as total is not initialized.

As when i see your code you are doing arithmetic operation on the inta array total inside the while loop, and if you declared the total variable inside the loop each time the loop runs the total variable get reset to new object of int[]. so there is no purpose of declaring inside the while loop

In order to fix this issue please move the total variable int array initialization outside the while loop so that it will be accessable in both while and for loop.

The corrected code is below

int[] total;   
total = new int [1000];

while (z < 1000 && !novemberOrders.eof()){

snum = novemberOrders.readInt();
quantity = novemberOrders.readInt();

total[snum-1] = total[snum-1] + quantity;

}
for (int i = 0; i < 1000;){
jta.append((i+1) + " " + total[i] + " ");
i++;

}

And the full code after modification

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sprocketswitharrays;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
*
* @author brett
*/
public class SprocketsWithArrays {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

JFrame jf;
JTextArea jta;
JScrollPane jsp;
  
jf = new JFrame();
jta = new JTextArea();

jsp = new JScrollPane(jta);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
  
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
jf.add(jsp);
jf.setVisible(true);

int snum, quantity, z = 0;

InputFile novemberOrders;
novemberOrders = new InputFile("novemberOrders.txt");

int[] total;   
total = new int [1000];

while (z < 1000 && !novemberOrders.eof()){

snum = novemberOrders.readInt();
quantity = novemberOrders.readInt();

total[snum-1] = total[snum-1] + quantity;

}
for (int i = 0; i < 1000;){
jta.append((i+1) + " " + total[i] + " ");
i++;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote