Fund. CS II (CS152 2006S)

Homework 9: Reading Integers

Assigned: Friday, February 17, 2006
Due: 8:00 a.m., Monday, February 20, 2006

Summary: In this assignment, you will rewrite your readInt method.

Purposes: To help you further explore exceptions; To give you the opportunity to improve a method you will regularly call.

Contents:

Background

At this point in your Java career, you've regularly written programs that read integers. You've gone from explicitly writing a sequence of lines to using a readInt method that you've written. The basic readInt method is defined as follows:

public static int readInt(PrintWriter pw, BufferedReader br, String prompt)
  throws Exception
{
  pen.print(prompt);
  pen.flush();
  String response = br.readLine();
  return Integer.parseInt(response);
} // readInt(PrintWriter, BufferedReader, String)

Unfortunately, the standard readInt has a number of failings. For example, it does not reprompt the user if the user has entered something that does not seem to be an integer. It also refuses to accept perfectly reasonable responses, like one.

The Assignment

Rewrite readInt so that it is friendlier. In particular,

Submitting Your Work

When you are satisfied with your work, send me the code of readInt. You need not send your enclosing class or test methods.

Extra Credit

There are, as always, a variety of ways with which you can earn extra credit. One of the most natural ways is to support a wider variety of written descriptions of integers (such as one thousand two hundred and twelve or one hundred thousand).

 

History

Friday, 17 February 2006 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 9 08:31:10 2006.
The source to the document was last modified on Wed Feb 22 08:44:11 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Homework/hw.09.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu