Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mattroseb8wk5;
- import java.util.Scanner;
- // tinetestingg sa file na ito yung
- // checkString, checkDouble, checkInt
- public class Day26C {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Day26C callMe = new Day26C();
- String[] dummyValues = {"Sinusuri","Testing","963.42","158"};
- // imagine dummyValues array as result of String[] getRows(int searchId)
- String userInput;
- String txt1;
- String txt2;
- double num1;
- int num2;
- System.out.println("Enter text or number per prompt");
- System.out.println("Press ENTER to skip");
- System.out.print("Text 1 >> ");
- userInput = sc.nextLine();
- txt1 = callMe.checkString(userInput, dummyValues[0]);
- userInput = "";
- System.out.print("Text 2 >> ");
- userInput = sc.nextLine();
- txt2 = callMe.checkString(userInput, dummyValues[1]);
- userInput = "";
- System.out.print("double >> ");
- userInput = sc.nextLine();
- num1 = callMe.checkDouble(userInput, dummyValues[2]);
- userInput = "";
- System.out.print("int >> ");
- userInput = sc.nextLine();
- num2 = callMe.checkInt(userInput, dummyValues[3]);
- userInput = "";
- System.out.println(String.format("%s | %s", txt1, dummyValues[0]));
- System.out.println(String.format("%s | %s", txt2, dummyValues[1]));
- System.out.println(String.format("%f | %s", num1, dummyValues[2]));
- System.out.println(String.format("%d | %s", num2, dummyValues[3]));
- }
- public String checkString(String newValue, String oldValue) {
- String resStr = newValue;
- if (newValue.trim().isEmpty()) {
- resStr = oldValue;
- }
- return resStr;
- }
- public double checkDouble(String newValue, String oldValue) {
- double resDbl = 0.0;
- try {
- resDbl = Double.parseDouble(newValue);
- } catch (Exception e) {
- resDbl = Double.parseDouble(oldValue);
- }
- return resDbl;
- }
- public int checkInt(String newValue, String oldValue) {
- int resInt = 0;
- try {
- resInt = Integer.parseInt(newValue);
- } catch (Exception e) {
- resInt = Integer.parseInt(oldValue);
- }
- return resInt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment