In: Computer Science
How can the classes be modified to satisfy the comments & example tester:
public class InvalidIntegerException extends Exception{
// Write a class for an InvalidIntegerException here
//Constructor that takes no arguments
public InvalidIntegerException (){
super(); }
//Constructor that takes a string message
public InvalidIntegerException (String message){
super(message); }
}
import java.io.InputStream; import java.io.IOException; public class Parser { private InputStream in; public static final int CHAR_ZERO = (int) '0'; public Parser (InputStream in) { this.in = in; } // Complete the following method as described in the assignment public int readInt () { //read characters from the InputStream in (an instance variable for the Parser class). // Specifically, your method should read the successive characters from in until the next whitespace character or end of stream is encountered the method Character.isWhitespace(char ch) will probably be helpful here // If those characters constitute an integer—i.e., are all digits 0 through 9, possibly starting with the - character—then readInt() should return the int whose decimal representation corresponds to these characters. //If a non-numerical character is encountered, readInt() should do the following: //Continue reading from in until the next whitespace character is read or the end of the stream is reached. //Throw an InvalidIntegerException with a message indicating the first non-numerical character encountered in in. } }
import java.io.InputStream;
import java.io.IOException;
public class ParserTester {
public static void main (String[] args) {
Parser p = new Parser(System.in);
int x = 0;
// Modify the loop below so that the code also catches
// InvalidIntegerExceptions and prints the appropriate error message: Could not read integer followed by the details of the caught exception.
// InvalidIntegerException should not break out of the main loop in the program—after an InvalidIntegerException, the user should be able to continue entering more values.
//Note that an InvalidIntegerException should not break out of the main loop in the program—after an InvalidIntegerException, the user should be able to continue entering more values.
while (true) {
try {
System.out.print("Enter a number (-1 to exit): ");
x = p.readInt();
System.out.println("x = " + x);
if (x == -1) {
System.out.println("Goodbye!");
return;
}
}
catch (IOException e) {
System.out.println("Reading error: " + e);
return;
}
}
}
}
When tested, for instance:
Enter a number (-1 to exit): 3857
x = 3857
Enter a number (-1 to exit): -4
x = -4
Enter a number (-1 to exit): 2m2
Could not read integer. InvalidIntegerException: Non-numerical character 'm' encountered.
Enter a number (-1 to exit): 13-4
Could not read integer. InvalidIntegerException: Non-numerical character '-' encountered.
Enter a number (-1 to exit): -ry24x
Could not read integer. InvalidIntegerException: Symbol '-' followed by non-numerical character 'r' encountered.
Enter a number (-1 to exit): 200
x = 200
Enter a number (-1 to exit): -1
x = -1
Goodbye!
Answer :
Code follows :
// ParserTester.java
import java.io.InputStream;
import java.io.IOException;
public class ParserTester {
public static void main (String[] args) {
Parser p = new Parser(System.in);
int x = 0;
// Modify the loop below so that the code also catches
// InvalidIntegerExceptions and prints the appropriate error
message: Could not read integer followed by the details of the
caught exception.
// InvalidIntegerException should not break out of the main loop in
the program—after an InvalidIntegerException, the user should be
able to continue entering more values.
//Note that an InvalidIntegerException should not break out of the
main loop in the program—after an InvalidIntegerException, the user
should be able to continue entering more values.
while (true) {
try {
System.out.print("Enter a number (-1 to exit): ");
x = p.readInt();
System.out.println("x = " + x);
if (x == -1) {
System.out.println("Goodbye!");
return;
}
}
catch (IOException e) {
System.out.println("Reading error: " + e);
return;
}
catch (InvalidIntegerException e) {
System.out.println("Could not read integer. " + e);
}
}
}
}
// Parser.java
import java.io.InputStream;
import java.io.IOException;
public class Parser {
private InputStream in;
public static final int CHAR_ZERO = (int) '0';
public Parser (InputStream in) {
this.in = in;
}
public int readInt () throws InvalidIntegerException,
IOException {
//read characters from the InputStream in (an instance variable for
the Parser class).
// Specifically, your method should read the successive characters
from in until the next whitespace character or end of stream is
encountered the method Character.isWhitespace(char ch) will
probably be helpful here
// If those characters constitute an integer—i.e., are all digits 0
through 9, possibly starting with the - character—then readInt()
should return the int whose decimal representation corresponds to
these characters.
//If a non-numerical character is encountered, readInt() should do
the following:
//Continue reading from in until the next whitespace character is
read or the end of the stream is reached.
//Throw an InvalidIntegerException with a message indicating the
first non-numerical character encountered in in.
int ch;
String s = "";
String badChar = "";
boolean firstch = true, firstminus = false;
while (true) {
ch = in.read ();
if (firstch) {
if ((Character.isDigit(ch)) || (ch == '-') ) {
if (ch == '-')
firstminus = true;
s += (char) ch;
firstch = false;
continue;
}
}
else {
if (Character.isDigit(ch)) {
s += (char) ch;
continue;
}
}
if ((ch == -1) || (Character.isWhitespace ((char) ch)) )
break;
if (badChar == "")
badChar += (char) ch;
}
if (badChar == "")
return (Integer.parseInt (s));
else if (firstminus)
throw (new InvalidIntegerException ("Symbol '-' followed by non
numerical character '" + badChar + "' encountered."));
else
throw (new InvalidIntegerException ("Non numerical character '" +
badChar + "' encountered."));
}
}
// Invalid integer exception
public class InvalidIntegerException extends Exception {
// Write a class for an InvalidIntegerException here
//Constructor that takes no arguments
public InvalidIntegerException (){
super();
}
//Constructor that takes a string message
public InvalidIntegerException (String message){
super(message);
}
}
I hope this answer is helpful to you. If you like the answer Please Upvote(Thums Up), I'm need of it, Thank you.