import java.util.*; import java.util.Random; public class arraychange{ //Scanner - declare it globally so method can see it static Scanner input = new Scanner(System.in); public static void main(String[] args) { System.out.print("What size array would you like to initialize? (at least 4) "); //ask for the input but make sure it is an integer int size = input.nextInt(); //declare array int[] array = new int[ size ]; Random randomNumbers = new Random(); for (int num = 0; num <array.length; num++ ) array[num] = randomNumbers.nextInt(50); System.out.printf("The values in the array are:\n"); for (int value = 0; value < array.length; value++ ) \ System.out.printf("value[ %2d ] =%4d\n", value, array[ value ]); System.out.printf("Enter the index of the value you would like to change: "); int edit = input.nextInt(); array[ edit ] = edit; [B]System.out.printf("The value sent is%4d", array[ edit ]); [/B] System.out.printf("\nEnter a new value (1-50) :"); int change = input.nextInt(); array[ edit ] = change; System.out.printf("The values in the array are:\n"); for (int value = 0; value < array.length; value++ ) System.out.printf("value[ %2d ] =%4d\n", value, array[ value ]); } }
Why does the value sent display the index and not the random number for that index? How would you make it do that? Also how/where would I add a try, catch statement to make sure the array size at the beginning is an int greater then 4 and not a double?