Question

For Java - Write a program that creates an array of 10 integers. Ask the user...

For Java - Write a program that creates an array of 10 integers. Ask the user to enter the subscript (index) of an element, then displays the element located at that subscript. If the subscript entered is out of bounds (less than 0 or greater than length - 1), display the message "Out of Bounds".

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code is as Follows:

Main.java

***************************************************************************************************************************************

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
int array[] = new int[]{10,34,56,78,90,12,34,56,78,100}; //creates an array of 10 integers
System.out.print("Enter the subscript (index) of an element:"); //prompt for the index or subscript
try{
int subscript = sc.nextInt(); //input the subscript
if(subscript<0 || subscript>array.length-1){ //if subscript entered is less than 0 or greater than length - 1
System.out.println( "Out of Bounds"); //display the message "Out of Bounds".
}
else{
System.out.println("The element located at "+subscript+" subscript is:"+array[subscript]); //displays the element located at that subscript
}
}catch(Exception e){ //if any exception occurs
e.printStackTrace();
}
   }
}

***********************************************************************************************************************************

Screenshot of the code:

Main.java 1 import java.util.Scanner; 2 public class Main 3- { public static void main(String[] args) { Scanner sc = new Scan

Output:

Input Enter the subscript (index) of an element: 8 The element located at 8 subscript is:78 ... Program finished with exit coEnter the subscript (index) of an element:-1 Out of Bounds ... Program finished with exit code o Press ENTER to exit console.Enter the subscript (index) of an element:100 Out of Bounds ... Program finished with exit code 0 Press ENTER to exit console

Add a comment
Know the answer?
Add Answer to:
For Java - Write a program that creates an array of 10 integers. Ask the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help wrting a java program that creates an array with 20 randomly chosen integers....

    I need help wrting a java program that creates an array with 20 randomly chosen integers. The program should prompt the user for an index of the array and display the corresponding element value. If the specified index is out of bounds,display the message "Out of Bounds".

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...

  • (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...

    (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly chosen integers. ■ Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds. Following modifications: * Use either a Scanner object or JOptionPane.showInputDialog() for input. * Use either System.out.println() or JOptionPane.showMessageDialog() for output. * In addition to ArrayIndexOutOfBoundsException, also catch InputMismatchException or NumberFormatException, and...

  • Write a program that will create an array of ten integers, allow the user to enter...

    Write a program that will create an array of ten integers, allow the user to enter those integers, and eventually search through the array based on index position: 1. Declare the array. You cannot assign elements to an array until it has first been created. 2. Next, run a for loop so that the user can enter an integer to save in each index position of the array. Since you this array holds ten elements , the index position starts...

  • Write a program that lets the user enter 10 values into an array. The program should...

    Write a program that lets the user enter 10 values into an array. The program should then display the largest and the smallest values stored in the array. The program should display the following message to the user at the beginning "This program will ask you to enter ten values. Then it will determine the largest and smallest of the values you entered.Please enter 10 integers separated by spaces: " And then after user entered the numbers, it should display...

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

  • Write a program in Java. Ask the user how many integers that he/she wants to enter....

    Write a program in Java. Ask the user how many integers that he/she wants to enter. Using a for loop, ask the user for that many integers. Then, display all those integers. view required output Standard Input                 20ENTER 500ENTER 400ENTER 300ENTER 550ENTER 600ENTER 700ENTER -3ENTER 0ENTER 50ENTER 60ENTER 40ENTER 13ENTER 14ENTER 80ENTER 90ENTER 100ENTER 777ENTER 1024ENTER 20ENTER 22 How many integers do you have? (Max 20)\n Enter element for subscript 0\n Enter element for subscript 1\n Enter element for...

  • Write a java program that declares 10 element array (of type integers), creates and initializes the...

    Write a java program that declares 10 element array (of type integers), creates and initializes the array, and perform the sum of elements of the array using for loop.   public class SumArray { public static void main (String[], args) { } // end of main } // end of SumArray class

  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • 1. Write a program to add positive numbers. Ask the user to enter numbers greater than...

    1. Write a program to add positive numbers. Ask the user to enter numbers greater than zero. If a 0, is entered stop requesting numbers and display the total. If a number is entered that is less than 0, display the message ‘Number must be greater than 0’, then ask for another number to be entered. Repeat until a number greater than 0 or 0 is entered. use math lab so I can copy the answers

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT