Question

In: Computer Science

Java program Bounds checking Make an array {4, 6, 2, 88, 5}, ask the user what...

Java program

Bounds checking

Make an array {4, 6, 2, 88, 5}, ask the user what number they would like to change. If their pick is out of bounds, tell them. Otherwise ask them what they would like to change the number to. After the change, display all the numbers with a for loop

Solutions

Expert Solution

import java.util.Scanner;

public class ChangeArrayElement {

    public static void print(int[] arr) {
        for(int i = 0; i < arr.length; ++i) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] numbers = {4, 6, 2, 88, 5};
        System.out.print("Array is: ");
        print(numbers);
        System.out.print("Which number would you like to change? ");
        int n = in.nextInt();
        boolean found = false;
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == n) {
                System.out.print("Enter new value: ");
                numbers[i] = in.nextInt();
                found = true;
                break;
            }
        }
        if (!found) {
            System.out.println(n + " is not found in the array");
        } else {
            System.out.print("New Array is: ");
            print(numbers);
        }
    }
}


Related Solutions

Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
write a MIPS program to ask user to input the number of elements of array
write a MIPS program to ask user to input the number of elements of array
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
C++ Write a program to declare an array of double of size 25, ask the user...
C++ Write a program to declare an array of double of size 25, ask the user to input all array elements from the keyboard, then write a function named out_of_order that will test this array for the condition a[0] >= a[1] >= a[2] >= ... > a[24] The function returns a -1 if the elements are not out of order, otherwise it returns the index of the first element that is out of order. Explain what you do to avoid...
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
In Java please Your program will sort a two dimensional array (5 * 4) based on...
In Java please Your program will sort a two dimensional array (5 * 4) based on the following: The entire array should be sorted using bubble sort based on the 1st column in ascending order and display the entire array. Reset the array to its original contents. The entire array should again be sorted using selection sort based on the 2nd column in descending order and display the entire array. Reset the array to its original contents. The entire array...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then: fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1). compute the sum of elements at the second row. compute the sum of elements at the third column. compute...
Write a spell checking program (java) which uses a dictionary of words (input by the user...
Write a spell checking program (java) which uses a dictionary of words (input by the user as a string) to find misspelled words in a second string, the test string. Your program should prompt the user for the input string and the dictionary string. A valid dictionary string contains an alphabetized list of words. Functional requirements: For each word in the input string, your program should search the dictionary string for the given word. If the word is not in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT