Please help in understanding the requirements.

This is how I understand it,
first, from standard input (I'm will need to use Scanner) read in 100 integers.
2nd, put all the 100 integers in a file (using printwriter in my case)
3rd, use that file to catch the exceptions

(ArrayIndexOutOfBoundsException) Write a program that meets the fol lowing requirements:

■ Read a file name on one line from Standard Input. Filename will not have mistakes.
■ Use that file to read 100 integers.
■ Keep reading from standard input until EOF or when the user enters -1.
■ Read integers from standard input, You need to capture both input mistmatch exceptions and array index out of bounds exception. If an existing index is entered output "index: value, ".
■ You must capture the exception ArrayIndexOutOfBoundsException. If the previous otuput was not an Exception output a new line first. Then output "Exception: index is Out of Bounds" where index is the number just read.
■ If the user enters mismatch format data after the file name, catch the InputMismatchException and display "Exception: Enter an integer (-1 to quit)\n"; Once again start the exception with a newline if the previous output was not an Exception.
■You will never print out a blank line, if the last line has legal data (i.e., index value,space) then terminated that line with a newline.

Recommended Answers

All 4 Replies

what exactly is not clear about these requirements? it's a step by step description of what you need to do.

Im confuse because it says read a file then again read an integer from standard input. So do i read an integer, then write it in a file? Then access the file then capture the exception? Or read in the integers, capture any mismatch exception before putting it in a file?

The requirement seems incomplete to me, but I think it means:

read an array of 100 numbers from a named file
then read numbers from stdin. If each number matches, write an index/value message to the output file, OR, if the number causes an Exception then write an exception message to the file. Keep reading numbers from stdin, and writing one line per number (one of those 2 messages), until the user enters -1

Maybe the numbers the user enters are intended to be used as indexes into the array of 100 numbers - it seems to imply that, but is woefully hopeless at saying it.

■ Read a file name on one line from Standard Input. Filename will not have mistakes.

read a filename that you enter using an instance of Scanner. This has to be a correct filename, so, if you type C:/numbers.txt, that file actually has to exist, and there have to be 100 integers in it.

■ Use that file to read 100 integers.

so: iterate over the file, read all the numbers.

■ Keep reading from standard input until EOF or when the user enters -1.

see, now this makes no sense at all. when these 100 numbers are read, you must continue to provide numbers through scanner. now, I can see how entering -1 might be used to end the input of numbers, but eof ... ?
since you are no longer reading from a file, you can not possibly reach end of file.

■ Read integers from standard input, You need to capture both input mistmatch exceptions and array index out of bounds exception. If an existing index is entered output "index: value, ".

so far, no array has been mentioned yet, so this makes very little sense as well.

■ You must capture the exception ArrayIndexOutOfBoundsException. If the previous otuput was not an Exception output a new line first. Then output "Exception: index is Out of Bounds" where index is the number just read.

see previous point. there still is no array, so how are you supposed to catch an arrayindexoutofboundsexception ? there is no array (yet) there are no bounds. there must be something in the assignment that states the size of the array, which it doesn't.

■ If the user enters mismatch format data after the file name, catch the InputMismatchException and display "Exception: Enter an integer (-1 to quit)\n"; Once again start the exception with a newline if the previous output was not an Exception.

ok, so, if you input a double, or a String, you can't cast it to an int, here you can get an InputMismatchException

■You will never print out a blank line, if the last line has legal data (i.e., index value,space) then terminated that line with a newline.

so, you either print the value at that index, or an error message.

Summary - my guess
1. you need to create a txt file containing 100 lines, each line containing one number. (an integer)
2. in your application, you'll need to create an array of integers, size: 100
you instantiate a scanner object, and through that instance of scanner, you read the name of the file.
read this file, and iterate over the resultlist. each line must be parsed to an integer, which you store in the array.

when this is done, you go back to your application. you'll start an "endless loop", which only breaks after entering -1.
so: valid inputs are numbers going from 0 to 99.
now, each time you enter something, you'll need to use Integer.parseInt to parse it to an index. there are four possible ways this can end:
1. your input is a number in the valid range: 0 - 99.
at which point you display the value in the array using your input as index.
2. your input is -1. Here you end your loop, and stop reading numbers and printing lines
3. your input is a numeric value, not within the range -1 - 99. you still just try to get the value at that index as you did in (1.). only, since the index doesn't exist, your application will throw an ArrayIndexOutOfBoundsException. so, put the printing the value in a try block, and catch this exception. your assignment tells you what to print.
4. your input is not a numeric value. let's say you enter "five", which is a String, and can not be parsed to an integer. so, when you try to parse it to an int, a InputMismatchException will be thrown. so, add a catch clause for this type of exception as well.

This sums up most of it. Have fun.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.