2

The output keeps showing 0, how can I change it? I want to print the user's array however my output takes the users input and print 0

public static void main(String[] args) {
    int i;
    int userArray[] = new int [50];

    Scanner lagoScan = new Scanner(System.in);

    System.out.print("Input Column of Array: ");
    int colInput = lagoScan.nextInt();

    for(i=1; i<= colInput; i++) {
        userArray [i] = lagoScan.nextInt();
    }
    System.out.println(userArray[i]);
}
2
  • 2
    in colInput you just have the number of columns. You are not accepting the data that you want to save. Accept the data, put it in array and then print output inside the for loop Commented Mar 2, 2020 at 6:41
  • 3
    What do you think the value of i is in System.out.println(userArray[i]);? Answer: It is greater than the last index that you have entered data for. Commented Mar 2, 2020 at 6:42

4 Answers 4

3

Your loop currently starts at the second index of the array, and you print the last index past colInput+1 (which you never set) so you get the default value of 0. Also, instead of hardcoding the length of 50 you should use the user provided value to declare the array. Finally, use Arrays.toString(int[]) to print the entire array (and limit variable scope when possible). Like,

int colInput = lagoScan.nextInt(); 
int[] userArray = new int[colInput];
for (int i = 0; i < colInput; i++) {
    userArray[i] = lagoScan.nextInt();
}
System.out.println(Arrays.toString(userArray));
Sign up to request clarification or add additional context in comments.

Comments

0

this code asks the user to enter 3 numbers and then print it out, I still don't know how to make it print all the numbers at once not individual, I think another for statement. I hope it helps. +don't mind the "enter the date.."

import java.util.Scanner;

public class Main { public static void main(String[] args) {

     Scanner cs= new Scanner(System.in);
    
     int[] date= new int[3];
    
     System.out.println("Enter the date for today: ");
     
    
     for(int i=0;i<=2;i++){
         
     date[i]=cs.nextInt(); 


     System.out.println(date[i]);
     
     }
}

}

Comments

-1

Try this

public static void main(String[] args) {
                Scanner lagoScan = new Scanner(System.in);

            System.out.print("Input Number of Elements in Array: ");
            int colInput = lagoScan.nextInt();    
            int userArray[] = new int [colInput];

                for(int i=0; i< colInput; i++) {
                System.out.print("Input "+i+"th Element:");
                 userArray [i] = lagoScan.nextInt();
                }
                System.out.println(Arrays.toString(userArray));
        }

1 Comment

@ScaryWombat how about now !!
-1

There is a bug in the code if I understand your intention properly.

System.out.println(userArray[i]); needs to be inside the for loop reading the input from the Scanner and copying it to your userArray.

Since your i variable's scope exists outside the loop, it points to the end of scanned inputs i.e. index of userArray where nothing has been stored yet, it gives the default value as 0.

You're fortunate that this does not raise a fault/exception first (since you have initialized it to a fixed capacity of 50 and the scanned input size might have been less than 50 in your tests). If your scanned input were larger, you could have been accessing out-of-bounds in your userArray after the loop was done modifying i.

3 Comments

Java does not segmentation fault ("not usually anyway"). OP is lucky to not receive an ArrayIndexOutOfBoundsException.
Thanks, @ElliottFrisch for the correction (and edited the answer). I was slightly out of touch with Java while writing this but aimed at explaining the generic bug.
Thank for the help! I realize it now

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.