Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mattroseb8wk5;
- import java.util.Scanner;
- // testing insert filter
- public class Day28A {
- public static void main(String[] args) {
- Day28A callMethods = new Day28A();
- Scanner sc = new Scanner(System.in);
- String txt1 = "";
- double num1 = 0;
- int num2 = 0;
- String userInput;
- boolean isValid = true;
- System.out.print("Enter text: ");
- userInput = sc.nextLine();
- if (callMethods.checkString(userInput)) {
- txt1 = userInput;
- } else {
- isValid = false;
- }
- userInput = "";
- System.out.print("Enter decimal number: ");
- userInput = sc.nextLine();
- if (callMethods.checkDouble(userInput)) {
- num1 = Double.parseDouble(userInput);
- } else {
- isValid = false;
- }
- userInput = "";
- System.out.print("Enter whole number: ");
- userInput = sc.nextLine();
- if (callMethods.checkString(userInput)) {
- num2 = Integer.parseInt(userInput);
- } else {
- isValid = false;
- }
- userInput = "";
- if (isValid) {
- System.out.println("String: " + txt1);
- System.out.println("Double: " + num1);
- System.out.println("Integer: " + num2);
- } else {
- System.out.println("One of the user input is invalid");
- }
- }
- public boolean checkString(String newValue) {
- boolean isValid = true;
- String resStr = newValue;
- if (newValue.trim().isEmpty()) {
- isValid = false;
- }
- return isValid;
- }
- public boolean checkDouble(String newValue) {
- boolean isValid = true;
- double resDbl = 0.0;
- try {
- resDbl = Double.parseDouble(newValue);
- } catch (Exception e) {
- isValid = false;
- }
- return isValid;
- }
- public boolean checkInt(String newValue) {
- boolean isValid = true;
- int resInt = 0;
- try {
- resInt = Integer.parseInt(newValue);
- } catch (Exception e) {
- isValid = false;
- }
- return isValid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment