HarrJ

B8 Day 26 use old value if user skips

Oct 10th, 2022 (edited)
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package mattroseb8wk5;
  2. import java.util.Scanner;
  3.  
  4. // tinetestingg sa file na ito yung
  5. // checkString, checkDouble, checkInt
  6. public class Day26C {
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.         Day26C callMe = new Day26C();
  10.        
  11.         String[] dummyValues = {"Sinusuri","Testing","963.42","158"};
  12.         // imagine dummyValues array as result of String[] getRows(int searchId)
  13.         String userInput;
  14.        
  15.         String txt1;
  16.         String txt2;
  17.         double num1;
  18.         int num2;
  19.        
  20.         System.out.println("Enter text or number per prompt");
  21.         System.out.println("Press ENTER to skip");
  22.         System.out.print("Text 1 >> ");
  23.         userInput = sc.nextLine();
  24.         txt1 = callMe.checkString(userInput, dummyValues[0]);
  25.         userInput = "";
  26.        
  27.         System.out.print("Text 2 >> ");
  28.         userInput = sc.nextLine();
  29.         txt2 = callMe.checkString(userInput, dummyValues[1]);
  30.         userInput = "";
  31.        
  32.         System.out.print("double >> ");
  33.         userInput = sc.nextLine();
  34.         num1 = callMe.checkDouble(userInput, dummyValues[2]);
  35.         userInput = "";
  36.        
  37.         System.out.print("int >> ");
  38.         userInput = sc.nextLine();
  39.         num2 = callMe.checkInt(userInput, dummyValues[3]);
  40.         userInput = "";
  41.        
  42.         System.out.println(String.format("%s | %s", txt1, dummyValues[0]));
  43.         System.out.println(String.format("%s | %s", txt2, dummyValues[1]));
  44.         System.out.println(String.format("%f | %s", num1, dummyValues[2]));
  45.         System.out.println(String.format("%d | %s", num2, dummyValues[3]));
  46.        
  47.     }
  48.  
  49.     public String checkString(String newValue, String oldValue) {
  50.         String resStr = newValue;
  51.         if (newValue.trim().isEmpty()) {
  52.             resStr = oldValue;
  53.         }
  54.         return resStr;
  55.     }
  56.    
  57.     public double checkDouble(String newValue, String oldValue) {
  58.         double resDbl = 0.0;
  59.         try {
  60.             resDbl = Double.parseDouble(newValue);
  61.         } catch (Exception e) {
  62.             resDbl = Double.parseDouble(oldValue);
  63.         }
  64.         return resDbl;
  65.     }
  66.    
  67.     public int checkInt(String newValue, String oldValue) {
  68.         int resInt = 0;
  69.         try {
  70.             resInt = Integer.parseInt(newValue);
  71.         } catch (Exception e) {
  72.             resInt = Integer.parseInt(oldValue);
  73.         }
  74.         return resInt;
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment