I'm trying to store the values inputted on the for-loop structure that I will need for later use, but it can only be recognized inside the for loop structure. I need the stored values to be recognize for the rest of the program but somehow I'm getting an error "cannot find symbol"
public class TryArray {
public static void main(String args[]) throws IOException {
BufferedReader inpt = new BufferedReader(new InputStreamReader(System. in ));
int[] arrayCrit = new int[5];
String[] crits = new String[5];
for (int i = 0; i < crits.length; i++) {
System.out.print("Criteria: ");
crits[i] = inpt.readLine();
for (int j = 0; j < arrayCrit.length; j++) {
System.out.print("Percentage: ");
arrayCrit[j] = Integer.parseInt(inpt.readLine());
}
}
System.out.println(crits[i] + arrayCrit[j])
}
}
Edit: Yes, I can move the print output inside the for-loop, but I need to input all the values first before showing all of it. Please I need help.
to show all you have to make another
loop
to read, after the insertionor I misunderstood your question?
What you're currently doing will overwrite the values entered for the previous criteria when you get the input for the next. I believe you want to have a
2d
array forarrayCrit
to store all the percentages for each criteria. Only in such a scenario it makes sense to have 2for
loops.1) Add
2)You are using nested for loops
Remove nested for-loop as
3)
can not find symbol
You want to print criteria and percentage at same time, as you have initialized both arrays with size 5, then you can directly print
4)You can use following program which is enhanced version of your program
Useful link
You have to define i and j outside the loop. Otherwise, you can only use them inside the loop:
Hope this will help.