Advertisement
StefanTobler

Assignment 5

Oct 28th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1.  import java.io.*;
  2.  import static java.lang.System.*;
  3.  import java.util.Random;
  4.  import java.util.Scanner;
  5.  import java.lang.Math;
  6.  
  7.  class Main{
  8.  
  9.  
  10.       public static void main (String str[]) throws IOException {
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.println("Enter the first String:");
  13.         String fst = scan.nextLine();
  14.         System.out.println("Enter the replacement String:");
  15.         String snd = scan.nextLine();
  16.         int flag = 0;
  17.         CharSequence s1 = "%";
  18.         CharSequence s2 = snd.substring(0);
  19.        
  20.         for(int i = 0; i < fst.length(); i ++)
  21.         {
  22.         if (fst.charAt(i) == '%')
  23.             {
  24.               if (fst.matches("^.*[^a-zA-Z% ].*$") && flag == 0)
  25.               {
  26.                 System.out.println("Error: Incorrect characters");
  27.                 flag += 99;
  28.               }    
  29.               flag++;
  30.             }
  31.         }
  32.          if (flag == 1)
  33.               {
  34.                 System.out.print(fst.replace(s1 , s2));
  35.               }
  36.          else if (flag > 1 && flag < 98)
  37.            {
  38.                 System.out.println("Error: Incorrect characters");
  39.          }
  40.         if (flag == 0)
  41.           System.out.println("Error: no %");
  42.       }
  43.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement