联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2018-09-05 03:40


Objectives

oCreate a program that reads a file and stores all integers contained in that file.

oTo get practice in using Java programming concepts:

Program arguments, file I/O, methods, arrays, wrapper objects, control structures

Related SLOs

oUse standard libraries or packages

oProduce robust and secure programs using exception handling and extensive program testing.

Instructions

oDue to the nature of this assignment, you should use wrapper objects instead of primitives.

oDownload the start file for this assignment: LastnameFirstname01.java

oAlso download the following files to the same folder you will be saving your Java program for this assignment:

electricity.txt, notable.txt, 1000.txt

oOpen LastnameFirstname01.java in jGRASP, change Lastname and Firstname to your own last and first name.

oSave the file and choose the option to Rename and Save.

oChange the comments at the top as appropriate.

oFor 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.

oFor 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

oFor 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.

oThe main() method should be very short, as it only has error checking for program arguments and two method calls.

oRun your program with the example .txt files as a program argument.

oBe sure your code is commented and follows the coding standard for the class.

•Example Output (using each of the .txt files)


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp