-1
private static int[] arr;
public static void inputarrays() {
    Scanner scan = new Scanner(System.in);

    System.out.println("length of an array");

    int x = scan.nextInt();
    arr = new int[x];

    System.out.println("values of an array");
    for(int n = 0; n < x; x++) {
        arr[n] = scan.nextInt();
    }
}

I looked up how to write code for getting an user input and creating an array like this one: Java - Creating an array from user input

It looks like my code is same as on the link's one which means it should work correctly. However, the scanner never closes when I enter the values of an array. I tried scan.close(), but it did not work. Also, I cannot use try&exception for this one.

7
  • 4
    Change x++ to n++ Commented Dec 16, 2018 at 18:21
  • I changed it but the scanner still does not stop Commented Dec 16, 2018 at 18:28
  • Add this line: scan.nextLine(); after each scan.nextInt(); Commented Dec 16, 2018 at 18:30
  • and thank you for editing, I would follow the format next time Commented Dec 16, 2018 at 18:30
  • it sill does not work should I make a new array inside of the method instead of declaring it in the data field? Commented Dec 16, 2018 at 18:34

2 Answers 2

1

This method is working, try it:

private static int[] arr;
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("length of an array");
    int x = scan.nextInt();
    scan.nextLine();
    arr = new int[x];
    System.out.println("values of an array");
    for(int n=0; n<x; n++){
        arr[n]=scan.nextInt();
        scan.nextLine();
    }
}
  1. changed x++ to n++, because n is the index of the loop
  2. added scan.nextLine(); after each call to scan.nextInt() so that each input line is fully consumed.
Sign up to request clarification or add additional context in comments.

6 Comments

I had to declare arr in the data field so that is why I put it outside of the method. If I keep in that way, does it gonna keep that error?
The nextLine()s are redundant here.
The arr is declared in the question on the first line.
@Ivar I thought so, but 1 out of 2 times I run the code without the nextLine(); in InteliJ, the loop does not stop.
@Ivar there is a problem for sure when you mix inputs with nextLine() and nextInt(). In the case where only nextInt() is used I also never had no problems. But it could happen and just to be on the safe side...
|
-2

You used n for your loop so you have to go for n++ and not x++.

private static int[] arr;
public static void inputarrays() {
    Scanner scan = new Scanner(System.in);

    System.out.println("length of an array");

    int x = scan.nextInt();
    arr = new int[x];
    System.out.println("values of an array");
    for(int n = 0; n < x; n++) {
        arr[n] = scan.nextInt();
    }
}

It should work this way.

1 Comment

this answer has nothing that hasn't already been said in other answers.

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.