HarrJ

B8 Day 28Value Checking

Oct 12th, 2022
1,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. package mattroseb8wk5;
  2. import java.util.Scanner;
  3.  
  4. // testing insert filter
  5. public class Day28A {
  6.     public static void main(String[] args) {
  7.         Day28A callMethods = new Day28A();
  8.         Scanner sc = new Scanner(System.in);
  9.         String txt1 = "";
  10.         double num1 = 0;
  11.         int num2 = 0;
  12.        
  13.         String userInput;
  14.         boolean isValid = true;
  15.        
  16.         System.out.print("Enter text: ");
  17.         userInput = sc.nextLine();
  18.         if (callMethods.checkString(userInput)) {
  19.             txt1 = userInput;
  20.         } else {
  21.             isValid = false;
  22.         }
  23.         userInput = "";
  24.        
  25.         System.out.print("Enter decimal number: ");
  26.         userInput = sc.nextLine();
  27.         if (callMethods.checkDouble(userInput)) {
  28.             num1 = Double.parseDouble(userInput);
  29.         } else {
  30.             isValid = false;
  31.         }
  32.         userInput = "";
  33.        
  34.         System.out.print("Enter whole number: ");
  35.         userInput = sc.nextLine();
  36.         if (callMethods.checkString(userInput)) {
  37.             num2 = Integer.parseInt(userInput);
  38.         } else {
  39.             isValid = false;
  40.         }
  41.         userInput = "";
  42.         if (isValid) {
  43.             System.out.println("String: " + txt1);
  44.             System.out.println("Double: " + num1);
  45.             System.out.println("Integer: " + num2);
  46.         } else {
  47.             System.out.println("One of the user input is invalid");
  48.         }
  49.     }
  50.  
  51.     public boolean checkString(String newValue) {
  52.         boolean isValid = true;
  53.         String resStr = newValue;
  54.         if (newValue.trim().isEmpty()) {
  55.             isValid = false;
  56.         }
  57.         return isValid;
  58.     }
  59.    
  60.     public boolean checkDouble(String newValue) {
  61.         boolean isValid = true;
  62.         double resDbl = 0.0;
  63.         try {
  64.             resDbl = Double.parseDouble(newValue);
  65.         } catch (Exception e) {
  66.             isValid = false;
  67.         }
  68.         return isValid;
  69.     }
  70.    
  71.     public boolean checkInt(String newValue) {
  72.         boolean isValid = true;
  73.         int resInt = 0;
  74.         try {
  75.             resInt = Integer.parseInt(newValue);
  76.         } catch (Exception e) {
  77.             isValid = false;
  78.         }
  79.         return isValid;
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment