2

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.

1
  • I appreciated all the help guys, thanks. Commented Oct 16, 2013 at 10:55

5 Answers 5

2

Hope this will help.

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("Enter Criteria: ");
        crits[i] = inpt.readLine();
        System.out.print("Enter Percentage: ");
        arrayCrit[i] = Integer.parseInt(inpt.readLine());
    }
    // Printing the values
    for (int i = 0; i < crits.length; i++) {
        System.out.println("Criteria :" + crits[i] + " Percentage: "
                + arrayCrit[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

to show all you have to make another loop to read, after the insertion

import java.io.*;

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());
     }
    }
    for(int i=0; i<crits.length; i++){
        for (int j=0; j<arrayCrit.length; j++){

            System.out.println(crits[i] + arrayCrit[j])
            }
        }
    }
}

or I misunderstood your question?

2 Comments

Why you are using two for loops to print.
because in the input are in two loops too. Probably should be outside, to be a 5x5, but just keep the coherence.
0

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 for arrayCrit to store all the percentages for each criteria. Only in such a scenario it makes sense to have 2 for loops.

String[] crits = new String[5];
int[][] arrayCrit = new int[5][5]; // The 2d array

for (int i = 0; i < crits.length; i++) {
    System.out.print("Criteria: ");
    crits[i] = inpt.readLine();
    for (int j = 0; j < arrayCrit[i].length; j++) {
        System.out.print("Percentage: ");
        arrayCrit[i][j] = Integer.parseInt(inpt.readLine()); // Inputting different set of percentage for each criteria
    }
}

// Displaying each criteria with its own percentages
for (int i = 0; i < crits.length; i++) {
    System.out.print("For Criteria: " + crits[i]+" ---> ");
    for (int j = 0; j < arrayCrit[i].length; j++) {
        System.out.print("Percentage " + j + " : " + arrayCrit[i][j] + "\t");
    }
    System.out.println();
}

Comments

0

1) Add

import java.io.*;  

2)You are using nested for loops

for(int i=0; i<crits.length; i++){ //this will execute 5 times
    System.out.print("Criteria: ");
    crits[i]=inpt.readLine();
 for (int j=0; j<arrayCrit.length; j++){ //this will execute 5 times for each i = 25 times
    System.out.print("Percentage: ");
    arrayCrit[j]=Integer.parseInt(inpt.readLine());
 }
}

Remove nested for-loop as

 for(int i=0; i<crits.length; i++){ //this will execute 5 times
    System.out.print("Criteria: ");
    crits[i]=inpt.readLine();
 }
 for (int j=0; j<arrayCrit.length; j++){ //this will execute 5 times 
    System.out.print("Percentage: ");
    arrayCrit[j]=Integer.parseInt(inpt.readLine());
 }  

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

 for (int i = 0; i < 5; i++) {    
   System.out.println(crits[i] + arrayCrit[i]);
}   

4)You can use following program which is enhanced version of your program

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;


public class TestArray {
public static void main(String args[]) throws IOException {
    BufferedReader
    inpt = new BufferedReader(new InputStreamReader(System. in ));

    System.out.println("How many records?");//ask for how many records
    int n = Integer.parseInt(inpt.readLine());// store in n

    int[] arrayCrit = new int[n];//create array with size n
    String[] crits = new String[n];

        //**as you mentioned in edit you want to take all the input before printing**      
    for (int i = 0; i < n; i++) 
    {
      System.out.print("Criteria: ");
      crits[i] = inpt.readLine();

      System.out.print("Percentage: ");
      arrayCrit[i] = Integer.parseInt(inpt.readLine());      
    }
        //print both arrays
    System.out.println("Criteria  \t  Percentage");
    for (int i = 0; i < n; i++) 
    {
        System.out.println(crits[i] + "\t"+arrayCrit[i]);
    }       
  }
}

Useful link

Comments

-1

You have to define i and j outside the loop. Otherwise, you can only use them inside the loop:

int i;
int j;

int[] arrayCrit = new int[5];
String[] crits = new String[5];

for (i = 0; i < crits.length; i++) {
  System.out.print("Criteria: ");
  crits[i] = inpt.readLine();
  for (j = 0; j < arrayCrit.length; j++) {
    System.out.print("Percentage: ");
    arrayCrit[j] = Integer.parseInt(inpt.readLine());
  }
}

3 Comments

I really don't think that the OP wants this.
Can you use i and j outside the loop if you define them inside the loop? Doesn't it give a compilation error? At least I am getting it!
No you cannot use them, but this is still not the correct answer to the problem. Better would be to propose printing the values inside the loop (so for each element) or printing the whole arrays (if that is what the OP wants). What your "fix" would do is to print the last element in both arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.