简体   繁体   中英

How do I store values in an array within a for loop?

I'm trying to get a bunch of numbers as input, and output it as first an unsorted array, then a sorted one. How do I store the inputted values inside the array within the for loop? Right now, it only stores the last value inputted.

String strNumberOfValues, strValue;
int intNumberOfValues, intValue;
 Scanner input = new Scanner(System. in );
System.out.println("Please enter the number of values you would like to enter");
strNumberOfValues = input.nextLine();
intNumberOfValues = Integer.parseInt(strNumberOfValues);
 for (int i = 0; i < intNumberOfValues; i++) {
    System.out.println("Please enter value for index value of " + i);
    strValue = input.nextLine();
    intValue = Integer.parseInt(strValue);
    for (int j = 0; j == 0; j++) {
        int[] intArray = {
            intValue
        };
        System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray));

Declare and initialize your array before the loop starts:

intNumberOfValues = Integer.parseInt(strNumberOfValues);
int[] intArray = new int[intNumOfValues];
for (int i = 0; i < intNumberOfValues; i++) {
    System.out.println("Please enter value for index value of " + i);
    strValue = input.nextLine();
    intArray[i] = Integer.parseInt(strValue);
}
System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray));
. . .

Note that your inner loop is useless. It executes exactly one time and the loop index j is not used in the loop. All it does is limit the scope of the declaration for intArray even more, so the symbol is not defined even for the line that prints the array values. That print statement, by the way, should not execute until all the input values have been obtained, which is why I moved it to after the outer loop in my answer. (Note also that the variable intValue is no longer needed here and can be removed from the program unless used elsewhere.)

I'd also recommend, as a matter of style, that you avoid using the variable type as a prefix for your variable names.

You have to declare array first and then assign values based on index.

String strNumberOfValues, strValue;
int intNumberOfValues, intValue;

Scanner input = new Scanner(System. in );
System.out.println("Please enter the number of values you would like to enter");

strNumberOfValues = input.nextLine();
intNumberOfValues = Integer.parseInt(strNumberOfValues);

int [] intArray = new int[intNumberOfValues];

for (int i = 0; i < intNumberOfValues; i++) {
   System.out.println("Please enter value for index value of " + i);
   strValue = input.nextLine();
   intValue = Integer.parseInt(strValue);
   intArray[i] = intValue;
}
    System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray));

In your inner for loop you are redeclaring the array every time, so only the last value is actually saved. You have to declare the array beforehand with the size that the user inputted then populate it index by index in a single for loop:

String strNumberOfValues, strValue;
int intNumberOfValues, intValue;
Scanner input = new Scanner(System. in );
System.out.println("Please enter the number of values you would like to enter");
strNumberOfValues = input.nextLine();
intNumberOfValues = Integer.parseInt(strNumberOfValues);
int[] intArray = new int[intNumberOfValues];

for (int i = 0; i < intNumberOfValues; i++) {
    System.out.println("Please enter value for index value of " + i);
    strValue = input.nextLine();
    intValue = Integer.parseInt(strValue);
    intArray[i] = intValue;
}
System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM