#Issue with arrays

1 messages · Page 1 of 1 (latest)

runic stone
#

Hi, I'm learning java and I was making a program where you can set the numbers for an array, and then after that you can modify the index position of the array, everything works until the last point where I need to print the array again, no error occurs, it just loads forever, and the program is running, I'm using VS Code, the code for this program is below.

import java.util.Arrays;
import java.util.Scanner;

public class editarrays {
    public static void main(String[] args) {
        int[] array = new int[5];

        for (int i = 0; i < array.length; i++) {
            Scanner number = new Scanner(System.in);
            System.out.println("Enter number for index " + i);
            array[i] = number.nextInt();
        }

        //for (int count = 0; count < array.length; count++) {
          //  System.out.println(array[count]);
        //}

        System.out.println(Arrays.toString(array));

        Scanner indexToModify = new Scanner(System.in);
        System.out.println("Enter the index number you want to modify: ");
        Scanner setInt = new Scanner(System.in);
        System.out.println("Enter the new value for index position " + indexToModify.nextInt());
        array[indexToModify.nextInt()] = setInt.nextInt();
        System.out.println(Arrays.toString(array));
    }
}
sinful hawkBOT
#

<@&987246399047479336> please have a look, thanks.

hard mural
#

You shouldn't declare multiple scanners.

heavy gate
#
  1. Don't create more than one Scanner (or any other consumer of System.in). Just create it at the top and reuse it
  2. use Integer.parseInt(scanner.nextLine()) instead of scanner.nextInt(). For obscure reasons, this is a bad idea with interactive input.
  3. This is wrong - System.out.println("Enter the new value for index position " + indexToModify.nextInt()). You need to 'remember' the value returned from Integer.parseInt(scanner.nextLine()) if you're going to use it to index into the array.
runic stone
#

Right okay I'll try this out thank you guys

#

All of your suggestions work, however when I declare the scanner this always happen, I don't get an error but it highlights orange and says "Recourse leak: 'scan' is never closed"

#

How do I fix this?

heavy gate
#

A Scanner consuming System.in shouldn't be closed. What IDE are you using?

runic stone
#

I'm not really using an IDE

#

I'm using VS Code

#

More of a code editor

heavy gate
#

Ahh... Yeah. It's not really a Java IDE and is giving bad advice.

runic stone
#

Oh

#

What should I use instead?

heavy gate
#

IntelliJ Community is free. IntelliJ is by far the dominant IDE.

runic stone
#

Alright thanks, I'll check it out

lean knot
#

just follow its guide and u'll be just fine

bitter raft
#

no one says its not possible

#

its just very suboptimal

#

and i can guarantee u that if ur not an experienced user, u will just run into the next issue caused by VSC

#

just like what has been mentioned above

#

suggesting to close a scanner tied to System.in is bad advice given by VSC

#

proper IDEs wont do that

#

and ur setup guide wont get rid of that flawed advice given by VSC

lean knot
#

vscode doesn't work by itself or randomly

bitter raft
#

the overall experience in using VSC for java development compared to other IDEs is subpar, which is why people tend to suggest using other stuff.
ofc everyone is free to use whichever tool they prefer

hard mural
lean knot
#

the fact is the ide isn't originally for java

#

but it can execute java programs, including small ones

#

it uses sonarlint and checkstyle

#

are those poor ?

hard mural
#

The implementation/results are not ideal. Everyone's free to use their desired IDE, but when someone is struggling and can pick between multiple options I personally wouldn't recommend the worse option.

bitter raft
#

OP came here asking why their IDE is suggesting to close the scanner

#

people told them its bad-advice given by an IDE that generally is subpar for Java and that they might have a better experience switching to intellij or eclipse

#

thats all that has been said

#

i dont see how what ur saying fits into that

hard mural
lean knot
#

never mind

runic stone
#

Guys I said I would just check it out 😢

#

No need to argue pepe_laugh

bitter raft
#

👍

heavy gate
#

Sonarlint (the free offering) gives some terrible advice. It's not a good tool for a beginner.