Did this for a java class I am taking. Its supposed to generate some random index and ask you for a new integer to place at that index of the array.
There is a weird issue with using console.close() at the end of the ChangeOne() method. I know its standard to close a Scanner instance when you are finished with it, so I don't get why in this case that is not applicable:
import java.util.*;
public class ChangeUp {
public static void main(String[] args)
{
int[] list = new int[6];
printArray(list);
for (int x=0; x<6; x++)
{
changeOne(list);
printArray(list);
}
}
public static void printArray(int[] somearray)
{
int arraylen = somearray.length;
for(int j=0; j < arraylen; j++)
{
System.out.println("Value: " + somearray[j]);
}
}
public static void changeOne(int[] somearray)
{
int arraylen = somearray.length;
Random r = new Random();
int index = r.nextInt(arraylen);
Scanner console = new Scanner(System.in);
System.out.printf("The chosen index equals %d. Enter a new integer for that index: \n", index);
while ( !console.hasNextInt())
{
console.next();
System.out.printf("The chosen index equals %d. Enter a new integer for that index: \n", index);
}
int v = console.nextInt();
somearray[index] = v;
console.close();
}
}