首页 > > 详细

辅导file I/O、辅导java Scanner I/O程序、辅导java编程、讲解java报告 辅导Python编程|讲解R语言编程

? Objectives
o Create a program that reads a file and stores all integers contained in that file.
o To get practice in using Java programming concepts:
? Program arguments, file I/O, methods, arrays, wrapper objects, control structures
? Related SLOs
o Use standard libraries or packages
o Produce robust and secure programs using exception handling and extensive program testing.
? Instructions
o Due to the nature of this assignment, you should use wrapper objects instead of primitives.
o Download the start file for this assignment: LastnameFirstname01.java
o Also download the following files to the same folder you will be saving your Java program for this assignment:
? electricity.txt, notable.txt, 1000.txt
o Open LastnameFirstname01.java in jGRASP, change Lastname and Firstname to your own last and first name.
o Save the file and choose the option to Rename and Save.
o Change the comments at the top as appropriate.
o For the main(), ensure the following:
? The program will take in exactly 1 program argument, which will be a filename.
? Ensure there is exactly 1 program argument, otherwise the program should terminate with a meaningful message to the user.
o For the readFileReturnIntegers() method:
? This method will open a file handle to the filename parameter and store all integers contained in the file in an array and return that array.
? Use the array already created in the start file to store all integers being read in.
? Create the necessary File and Scanner variables to read in a file.
? Note that you will need to catch FileNotFoundException when a Scanner object is created using a File object.
? While reading the file, you will need to use the hasNextLine(), and next() methods.
? See the Scanner Class API for more details on Scanner methods.
? Note: You will be using next() instead of nextLine(). next() grabs one word at a time whereas nextLine() grabs an entire line at once.
? Loop through the file using the hasNextLine() method of your Scanner variable.
? Use the next() to get one word from the file, and store the word in a String variable. Then use the static method Integer.parseInt() to convert the String into an integer and store the integer in the array.
? You will need an integer variable that represents the index number of the array and increment it each time you store an integer in the array.
? If it is NOT an integer, your program will throw a NumberFormatException.
? Use another try/catch block to determine which words are integers and which are not.
? You will also need to catch NoSuchElementException for blank lines in the file.
? Both catch blocks may be empty and not contain any code since the program is only concerned with integers and should continue until the end of the file.
? A rough rundown of populating the array is as follows:
? while the file has lines:
? read in a word from the file;
? attempt to convert that word into an integer;
? store that integer in the array;
? When a NumberFormatException occurs, do nothing since that word was not an integer
When a NoSuchElementException occurs due to a newline, do nothing since that is not an integer
o For the printArrayAndIntegerCount() method:
? Print the name of the file and the number of integers in the file, and print all of the integers that are stored in the array.
? Your output should be similar to the output below.
? There will be null values in the array if the array was not filled up completely. As you loop through the array, use an if-statement to check if the value is not null, that is, =! null
? I recommend building a string that contains all of the information, then print that string at the end of the method.
o The main() method should be very short, as it only has error checking for program arguments and two method calls.
o Run your program with the example .txt files as a program argument.
o Be sure your code is commented and follows the coding standard for the class.
? Example Output (using each of the .txt files)
联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!