HarrJ

B5 Day 28 user input checker

Sep 7th, 2022 (edited)
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. // combining 25C and 27B to make a checker
  4. public class Main {
  5.     public static void main(String[] args) {
  6.        
  7.         Main call25A = new Main();
  8.         Scanner sc = new Scanner(System.in);
  9.         String txt1 = "";
  10.         String txt2 = "";
  11.         double num1 = 0;
  12.         double num2 = 0;
  13.         String userInput;
  14.         boolean isValid = true; // isang false lang ang kelangan
  15.        
  16.         System.out.print("Enter txt1 >> ");
  17.         userInput = sc.nextLine();
  18.         if (userInput == null || userInput.length() == 0) {
  19.             isValid = false;
  20.         } else {
  21.             txt1 = userInput;
  22.         }
  23.        
  24.         System.out.print("Enter txt2 >> ");
  25.         userInput = sc.nextLine();
  26.         if (userInput == null || userInput.length() == 0) {
  27.             isValid = false;
  28.         } else {
  29.             txt2 = userInput;
  30.         }
  31.        
  32.         System.out.print("Enter num1 >> ");
  33.         userInput = sc.nextLine();
  34.         if (userInput == null || userInput.length() == 0
  35.             || call25A.isNumeric(userInput) == false) {
  36.             isValid = false;
  37.         } else {
  38.             num1 = Double.parseDouble(userInput);
  39.         }
  40.        
  41.         System.out.print("Enter num2 >> ");
  42.         userInput = sc.nextLine();
  43.         if (userInput == null || userInput.length() == 0
  44.             || call25A.isNumeric(userInput) == false) {
  45.             isValid = false;
  46.         } else {
  47.             num2 = Double.parseDouble(userInput);
  48.         }
  49.  
  50.         if (isValid) {
  51.             System.out.println("txt1: " + txt1);
  52.             System.out.println("txt2: " + txt2);
  53.             System.out.println("num1: " + num1);
  54.             System.out.println("num2: " + num2);
  55.         } else {
  56.             System.out.println("One of the user input is invalid");
  57.         }
  58.     }
  59.  
  60.     public boolean isNumeric(String strNum) {
  61.         if (strNum == null) {
  62.             return false;
  63.         }
  64.         try {
  65.             double d = Double.parseDouble(strNum);
  66.         } catch (NumberFormatException nfe) {
  67.             return false;
  68.         }
  69.         return true;
  70.     }
  71. }
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment