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
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
ADVERTISEMENT