-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.

Ivar
  • 4,655
  • 12
  • 45
  • 50
  • 4
    Change x++ to n++ – forpas Dec 16 '18 at 18:21
  • I changed it but the scanner still does not stop – 판다하나만 Dec 16 '18 at 18:28
  • Add this line: `scan.nextLine();` after each `scan.nextInt();` – forpas Dec 16 '18 at 18:30
  • and thank you for editing, I would follow the format next time – 판다하나만 Dec 16 '18 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? – 판다하나만 Dec 16 '18 at 18:34
  • See the code in my answer. – forpas Dec 16 '18 at 18:38
  • [Your code works fine if you change the `x++` to `n++`](https://tio.run/##hVHNToQwEL73KSacIESi7k304NFET5zMhkNlu2wRpqQdkI3h2XGw7Eo0Wecyab@/mUwle3lV7d4n3bTGElT8TjrSdfJorTy6VPwBskIiKptObfdW6wKKWjoHL1IjfAoBXK3VvSQFjiQxQSNtc5DWph71sgXsjd5Bw@IwI6ux/GaWLmIvWGoJBMcdHgDVx@krzI6OVJNojFLxw/efpqOEJ0GqMQxqhSUdwOyBPeS8WrCWMAsG9p4jElQDPSGFUXrGWbEkz8sM@T9pvaw75X6nnRR7Y8M5cF7mOuV2DwO3OF4vvaRuMb8w1lxn7FmjWoPj5RlfTWf9bFAYJL6Au4MAYvCHT8j4g4TMiRbbUYximjbiRtyKjfgC). – Ivar Dec 16 '18 at 18:52

2 Answers2

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.
forpas
  • 117,400
  • 9
  • 23
  • 54
  • 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? – 판다하나만 Dec 16 '18 at 18:47
  • The `nextLine()`s are redundant here. – Ivar Dec 16 '18 at 18:52
  • The `arr` _is_ declared in the question on the first line. – Ivar Dec 16 '18 at 18:54
  • @Ivar I thought so, but 1 out of 2 times I run the code without the nextLine(); in InteliJ, the loop does not stop. – forpas Dec 16 '18 at 18:58
  • @forpas I know that's the case [if you use `nextLine` after `next` or `nextFoo`](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). Besides that, I personally never had trouble in IntelliJ myself. (But that doesn't mean that others don't of course.) – Ivar Dec 16 '18 at 19:07
  • 1
    @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... – forpas Dec 16 '18 at 19:09
-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.