Purchase Solution

Java method readInt

Not what you're looking for?

Ask Custom Question

Provide a Java class named "Input" containing the method "readInt" (prototype given below) that displays the prompt string, reads an integer, and tests whether it is between the supplied minimum and maximum. If not, it prints an error message and repeats the entire process.

public static int readInt(Scanner in, String prompt, String error, int min, int max)

Use the following class as the main class for testing the implementation.

import java.util.Scanner;

/**
This program prints how old you'll be next year.
*/
public class AgePrinter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age = Input.readInt(in, "Please enter your age",
"Illegal Input--try again", 1, 150);
System.out.println("Next year, you'll be " + (age + 1));
}
}

Purchase this Solution

Solution Summary

Provided code has been tested with command line version of Java binaries from Java SDK v1.7.0_u3 .

Solution Preview

Please rename the attached 470518-AgePrinter.java to AgePrinter.java before compiling and executing it.

A sample session with given program looks like following.

$ java AgePrinter
Please enter your age
200
Illegal Input--try again
Please enter your age
-1
Illegal Input--try again
Please enter your age
99
Next year, you'll be ...

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Basic Networking Questions

This quiz consists of some basic networking questions.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.