Purchase Solution

Java program to find a value at an index in an integer array

Not what you're looking for?

Ask Custom Question

Could you help with this challenge?

Write a main class named ArrayProgram that generates an array of fifty integers, all ranging from 1 to 100. The program should then prompt the user to enter an array index so the number in the array at that index can be found, but the user might not enter an integer. That would throw an InputMismatchException. Take steps to scold the user and stop the program if this happens.

If the user does enter an integer, call a method named returnIndexValue to return the integer at the specified index in the array. This method will take the array and the desired index as arguments, but the index may be too big for the array. That would throw an ArrayIndexOutOfBoundsException. Deal with that possible scenario in returnIndexValue.

The main method should print the integer at the specified index if no exceptionsare thrown. Sample runs are shown below.

Run One
Enter index of array to display.
four
You must enter an integer for the index.
Program stopped.

Run Two

Enter index of array to display.
75
75 is too high.
Index must be 0-49.
Run Three
Enter index of array to display.
35
That index holds 26

Purchase this Solution

Solution Summary

Write a Java program to find a value at in index of a random generated integer array.

Purchase this Solution


Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Javscript Basics

Quiz on basics of javascript programming language.