Java Array Storing Values

69,226

Solution 1

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]);
    }
}

Solution 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?

Share:
69,226
user2768501
Author by

user2768501

Updated on February 10, 2020

Comments

  • user2768501
    user2768501 about 4 years

    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.