Java exercise

  

 

package ersatz;

import java.io.FileNotFoundException;

public class Ersatz {
  public static void main(String[] args) throws FileNotFoundException {
    try {
      register("bb", "123321", "b@b.b");
      System.out.println("success");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  public static void register(String name, String password, String email) {
    if (!(name != null && password != null && email != null)) {
      throw new NullPointerException("argument cannot be null");
    }
    if (!(name.length() >= 2 && name.length() <= 4)) {
      throw new RuntimeException(String.format("%s invalid", name.length()));
    }

    if (!(password.length() == 6 && isDigital(password))) {
      throw new RuntimeException(String.format("%s invalid", password));
    }

    int i = email.indexOf("@");
    int j = email.indexOf(".");
    if (!(i > 0 && i < j)) {
      throw new RuntimeException(String.format("%s invalid", email));
    }
  }

  public static boolean isDigital(String s) {
    char[] chars = s.toCharArray();
    for (char value : chars) {
      if (!(value >= '0' && value <= '9')) {
        return false;
      }
    }
    return true;
  }
}

  

 

package com.gibe;

public class Ersatz {
  public static void main(String[] args) {
    String s = "abcdefg";
    try {
      s = reverse(s, 0, 3);
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    System.out.println(s);
  }

  public static String reverse(String s, int commenceIndex, int closureIndex) {
    if (!(s != null && commenceIndex >= 0 && closureIndex < s.length() && commenceIndex <= closureIndex)) {
      throw new RuntimeException("arguments wrong !");
    }

    char[] chars = s.toCharArray();
    char c;
    for (int i = commenceIndex, j = closureIndex; i < j; i++, --j) {
      c = chars[i];
      chars[i] = chars[j];
      chars[j] = c;
    }
    return new String(chars);
    //    while (commenceIndex < closureIndex) {
//      c = chars[commenceIndex];
//      chars[commenceIndex] = chars[closureIndex];
//      chars[closureIndex] = c;
//      commenceIndex++;
//      closureIndex--;
//    }
//    return String.valueOf(chars);
  }

  public static String reverse(String s) {
    return reverse(s, 0, s.length() - 1);
  }
}

  

package com.gibe;

public class Ersatz {
  public static void main(String[] args) {
    String name = "Willian Jefferson Clinton";
    print(name);
  }

  public static void print(String s) {
    if (s == null) {
      System.out.println("null input");
      return;
    }
    String[] strs = s.split(" ");
    if (strs.length != 3) {
      System.out.printf("invalid %s",s);
      return;
    }
    String format = String.format("%s, %s, .%c", strs[2], strs[0], strs[1].toUpperCase().charAt(0));
    System.out.println(format);
  }
}

  

package com.gibe;

public class Ersatz {
  public static void main(String[] args) {
    String str = "abcHsp U 1234";
    count(str);
  }

  public static void count(String s) {
    if (s == null) {
      System.out.println("null input");
      return;
    }
    int nums = 0;
    int lowers = 0;
    int uppers = 0;
    int others = 0;
    for (int i = 0; i < s.length(); ++i) {
      if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
        nums++;
      } else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
        lowers++;
      } else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
        uppers++;
      } else {
        others++;
      }
    }
    System.out.println("nums : " + nums);
    System.out.println("lowers : " + lowers);
    System.out.println("uppers : " + uppers);
    System.out.println("others : " + others);
  }
}

  

 

posted @ 2021-07-15 11:36  ascertain  阅读(36)  评论(0编辑  收藏  举报