Advertisement
Evyatar12

dowhile

Jan 19th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. package my.pack;
  2.  
  3. import java.util.Scanner;
  4. import java.util.function.Function;
  5.  
  6. public class David1 {
  7.    
  8.     public static void main(String[] args) {
  9.         String inputString = "hello";
  10.        
  11.         doWhile(new Function<String, Boolean>() {
  12.  
  13.             @Override
  14.             public Boolean apply(String t) {
  15.                 if (t == null)
  16.                     return false;
  17.                
  18.                 return t.equals(inputString);
  19.             }
  20.         }, "input string should be " + inputString + ".");
  21.     }
  22.    
  23.     public static void doWhile(Function<String, Boolean> conditionChecker, String errMsg) {
  24.         Scanner s = new Scanner(System.in);
  25.        
  26.         String next = null;
  27.        
  28.         do {
  29.             if (next != null)
  30.                 System.err.println(errMsg);
  31.            
  32.             next = s.nextLine();
  33.         }
  34.         while(!conditionChecker.apply(next));
  35.     }
  36.    
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement