In: Computer Science
Download and review the PrintNumbers.java program provided in the homework files. This program contains a method which is designed to read in a number from the user and print it to the screen. The parameter to the method specifies whether the number might contain decimals.
There are two helper methods- one that reads in an integer and one that reads in a double.
Run the program. As written, it works as long as the user enters what they are supposed to enter! But if the user enters in a String, or a number with decimals when an integer number is required, the program crashes.
You will modify this program so that when the user enters bad input, the program gives an error message and then allows the user to try again.
Bad input is defined as: non-numeric input or numeric input with a decimal when a decimal is not allowed.
To modify the program, add one or more of each of the following to either the printNumbers method or the two helper methods:
The revised program should run without crashing when the user enters valid or invalid input. A sample output is provided in the homework files.
For full credit, reduce duplicated/repeated code!
Given code: PrintNumbers.java
import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class PrintNumbers { private static Scanner scan = new Scanner(System.in); public static void main(String[] args) { printNumber(true); printNumber(false); } public static void printNumber(boolean mayContainsDecimals) { System.out.println("Enter a number " + (mayContainsDecimals ? "possibly with" : "without") + " decimals:"); if (mayContainsDecimals) { double d = readDouble(); System.out.println("Your number is: " + d); } else { int n = readInteger(); System.out.println("Your non-decimal number is: " + n); } } private static int readInteger() { int number = Integer.parseInt(scan.nextLine()); return number; } private static double readDouble() { double number = Double.parseDouble(scan.nextLine()); return number; } }
Here is code in java for above problem:
package com.company;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class PrintNumbers {
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
printNumber(true);
printNumber(false);
}
public static void printNumber(boolean mayContainsDecimals) {
while(true) {
try {
System.out.println("Enter a number " + (mayContainsDecimals ? "possibly with" : "without") + " decimals:");
if (mayContainsDecimals) {
double d = readDouble();
System.out.println("Your number is: " + d);
} else {
int n = readInteger();
System.out.println("Your non-decimal number is: " + n);
}
}catch (Exception e){
System.out.println("Invalid input..! Try Again");
continue;
}
break;
}
}
private static int readInteger() {
int number = Integer.parseInt(scan.nextLine());
return number;
}
private static double readDouble() {
double number = Double.parseDouble(scan.nextLine());
return number;
}
}
Output: