0

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

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Why exactly do you re-post an answer that was given almost three minutes ago?
:-) I started to answer this question before 5 minutes. But formatting took more time.
0

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

8 Comments

Please do not call an array a List. They are fundamentally different.
@Turing85 I just copied that print statement in the original question. The output might not be for a technical user. I think the word "list" is being used as a normal word, not as a reference to the structure in programming.
This is incorrect. A List is a dynamic datastructure. An array is not. Anyone using some kind of imperative programming language should know the difference (e.g. OP). If you do not want to write "array" (for the nontechnical end-user), you can avoid this by writing something like "Here are the elements you entered: ".
I know the difference between a list and an array. But the word list is not unique to programming, it is also defined as "a number of connected items or names written or printed consecutively, typically one below the other." Using the word was the choice of the person who asked the question, I didn't see a reason to change it to answer the question. Apparently none of the other people who answered did either. @Turing85
I totally get your point. But you confuse people, who are programmers. They will jump to conculsions that may not hold. This is the reason I proposed a mostly neutral formulation (which does not make use of the words "list" or "array") to avoid misunderstandings.
|

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.