Question
I want you to make this Java code bulletproof, by making it output an error message if the user inputs a letter or symbol, preferably
I want you to make this Java code bulletproof, by making it output an error message if the user inputs a letter or symbol, preferably use toString, parseInt, and a try catch. Thank you.
/*This program reads a positive integer and then checks to see whether or not
the integer is divisible by 11. For that divisibility test, use the following
algorithm, based on one given by the mathematician Lewis Carroll.*/
public class Divisible_name
{
public static void main (String[] args)
{
//Declares variables
int numberIn, numberB, numberC, numberD, numberDup;
numberIn = 0;
//While loop which works when user input is not 99999
while (numberIn != 99999)
{
System.out.println ("Enter a positive number or enter '99999' to end the program: ");
numberIn = In.getInt ();
numberDup = numberIn;
//While loop which implements the formula, and determines the output
while (numberIn > 9 && numberIn != 0 && numberIn > 0 && numberIn != 99999)
{
numberB = numberIn / 10;
numberC = numberIn % 10;
numberIn = numberB - numberC;
System.out.println (numberIn);
}
//If input is 99999, program end message is printed
if (numberIn == 99999)
System.out.println ("The program has ended.");
//If input is 0 number is divisible by 11
if (numberIn == 0)
{
System.out.println ("The number is divisible by 11.");
}
//Outputs a space, so that the else does not include the sentinel
else if (numberIn == 99999)
{
System.out.println ("");
}
//If none of the other cases occur, the number is not divisible
else
{
System.out.println ("The number is NOT divisible by 11.");
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
To make the Java code more robust and handle cases where the user inputs a letter or symbol we can modify the code to use trycatch blocks and the pars...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started