Question: JAVA question: Need help with code! Need to write a method with these specifications: initArray() will ask the user for a maximum number of integers

JAVA question: Need help with code!

Need to write a method with these specifications:

initArray() will ask the user for a maximum number of integers expected, create an array of integers of that size, read at most that many integers from the keyboard using a Scanner object (ignore extra input beyond the size of the array), and return the initialized array. The user must enter a positive integer greater than 0 for the size of the array. Keep asking the user for a positive integer greater than 0 until a valid value is entered. When interacting with the user, make sure you use the given strings from PA6Strings.java to show error messages, prompts, etc.

If the user indicates EOF (see next sentence) or a non-integer input while the size is not determined yet, the program should exit with exit status 1 [System.exit(1);]. If the user hits +D (Unix/Mac) or +Z (DOS/Windows) to indicate EOF (no more input) or any non-integer before the entire array has been filled (but after the size has been set), return an array resized with only the values that were entered (no extra/unfilled array slots). Hint: Use System.arraycopy().

This is what i have so far:

JAVA question: Need help with code! Need to write a method with

Need help with cases when user enters non-integer as input when size is set and also when size is not set

Also, when i put in a value less than or equal to zero, the program terminates when its supposed to loop back and keep asking user for a valid input until one is entered

Would really appreciate the help!! Thanks so much!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!