In: Computer Science
How can the classes be modified to satisfy the comments:
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;
}
}
}
}
Here is the solution to above problem in Java. Please read the code comments for more information
JAVA CODE
import java.io.InputStream;
import java.io.IOException;
import java.util.*;
class Parser {
private InputStream in;
public static final int CHAR_ZERO = (int) '0';
public Parser()
{
}
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.
Scanner sc = new Scanner(System.in); //take input from User
char ch='_';
String integer="";
String input = sc.nextLine();
int index=0;
//repeat till space is not encountered
try {
while((ch!= ' ')&& index<input.length())
{
ch = input.charAt(index++);
//read the character
if(ch>='0'&&ch<='9') //if numeric value add to the
string
integer+=ch;
else if(ch==' ') //if space encountered break
break;
else //if something else
throw new IOException("Invalid input " + ch);
}
//print the valid integer
return Integer.parseInt(integer);
} catch(IOException e) {
System.out.println(e.getMessage()); //print the failing
character
return -1;
}
}
}
public class Main
{
public static void main(String [] args)
{
Parser p = new Parser();
int result=p.readInt();
if(result!=-1)
System.out.println("Integer is: " + result );
}
}
Screenshot of output