Using an int from a for loop in another for loop. JAVA

64 Views Asked by At

I need to be able to use one for loop to fill an array and then use the last int given as input as the threshold value. Then I am trying to use another for loop to compare values in the array to the threshold value and if they are less than the value I will put them to output. I have the code nearly completed and while it may be chunky I have it working up until I make the comparison to the array, it is saying my maxValue int is not initialized even though it was assigned a value in the first loop. Please let me know where I am going wrong, I am lost.

import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int[] userValues = new int[20];   // List of integers from input
      int numVals;
      int maxValue;
      int i;
  
      // The first input is to set the number of values that will be inputted. 
      numVals = scnr.nextInt(); 

      // The first for loop that sets the values for the array.
      for (i = 0; i < numVals; i++) { 
         userValues[i] = scnr.nextInt();
      }
 
      // The second for loop that sets the max value. Probably a better way to do this.
      for (i = numVals - 1; i > numVals - 2; i--) {
         maxValue = userValues[i];  
      }
  
      // This third for loop is where the error is coming from. 
      for (i = 0; i < numVals; i++) {
         if (userValues[i] <= maxValue) {
            System.out.print(userValues[i] + ",");
         }
      }
  
  
     
   }
}

This is the error message I am receiving: LabProgram.java:22: error: variable maxValue might not have been initialized if (userValues[i] <= maxValue) {

2

There are 2 best solutions below

1
zjones22 On
import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int[] userValues = new int[20];   // List of integers from 
input
      int numVals;
      int maxValue = 199;
      int i;

      // The first input is to set the number of values that will 
be inputted. 
      numVals = scnr.nextInt(); 
     // I added the below int to pick up that last number that is meant to be the threshold number. 
      int newNumVals = numVals + 1;
      // The first for loop that sets the values for the array.
      for (i = 0; i < (newNumVals); i++) { 
         userValues[i] = scnr.nextInt();
      }

      // The second for loop that sets the max value. Probably a 
 better way to do this.
       for (i = newNumVals - 1; i >= newNumVals - 1; i--) {
          maxValue = userValues[i];  
     
      }
  
        
       for (i = 0; i < newNumVals-1; i++) {
          if (userValues[i] <= maxValue) {
            System.out.print(userValues[i] + ",");
         }
      }
   System.out.println("");
  


 
   }
}

I found a way to get around my problem, feel like I cheated and that there is probably a better way to make this work so any feedback would be great, but it passed the tests so it works for me.

1
Cameron Mayes On

one way you could simplify this code is to get the last item in the array instead of using the for loop. I've also moved around some variables so that they are instantiated with values. I also made the size of the array match the input + 1, so that if a user types in a bigger number than 20, the program won't crash.

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        // The first input is to set the number of values that will be inputted.
        int numVals = scnr.nextInt();
        // I added the below int to pick up that last number that is meant to be the threshold number.
        int newNumVals = numVals + 1;
        
        int[] userValues = new int[newNumVals];   // List of integers from input
        // The first for loop that sets the values for the array.
        for (int i = 0; i < newNumVals; i++) {
            userValues[i] = scnr.nextInt();
        }

        int maxValue = userValues[newNumVals - 1];

        for (int i = 0; i < newNumVals-1; i++) {
            if (userValues[i] <= maxValue) {
                System.out.print(userValues[i] + ",");
            }
        }
        System.out.println("");
    }
}

However, you could use the ArrayList class if you'd like to make the code even shorter.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        // The first input is to set the number of values that will be inputted.
        int numVals = scnr.nextInt();
        // I added the below int to pick up that last number that is meant to be the threshold number.
        int newNumVals = numVals + 1;

        List<Integer> userValues = new ArrayList<>();   // List of integers from input
        // The first for loop that sets the values for the array.
        for (int i = 0; i < newNumVals; i++) {
            userValues.add(scnr.nextInt());
        }

        int maxValue = userValues.get(userValues.size() - 1);

        userValues.stream().filter(val -> val <= maxValue).forEach(val -> System.out.print(val + ","));

        System.out.println("");
    }
}