测试帐号中是否只有一个连字符的存在。

huangapple go评论96阅读模式
英文:

Test for the presence of only one hyphen in the account number

问题

我想使用只有字符串方法而没有for循环来重写以下代码。

  1. private static boolean isAccountFormatCorrect(String name) {
  2. int m = name.length() - name.replace("-", "").length();
  3. if (m >= 2) {
  4. throw new BadAccountInputException("Only one hyphen allowed in account number");
  5. }
  6. return true;
  7. }
英文:

I would like to rewrite the following code using only String methods, without for loops.

  1. // Run the QuestionGenerator and test for the error condition
  2. // indicated. If the account number has a valid format
  3. // according to the requirement set out in the question generator, return
  4. // true, otherwise false.
  5. private static boolean isAccountFormatCorrect(String name) {
  6. int m = 0;
  7. for(int i = 0; i < name.length(); i++) {
  8. char a = name.charAt(i);
  9. if (a == '-') {
  10. m++;
  11. }
  12. }
  13. if (m >= 2) {
  14. throw new BadAccountInputException("Only one hyphen allowed in account number");
  15. }
  16. return true;
  17. }

答案1

得分: 1

Compare the first index of a hyphen with the last index where it is found. If they are equal, then there was only one.

  1. private static boolean isAccountFormatCorrect(String name) {
  2. if(name.indexOf('-') != name.lastIndexOf('-')){
  3. throw new BadAccountInputException("Only one hyphen allowed in account number");
  4. }
  5. return true;
  6. }

However, it seems you want to return a boolean indicating whether or not the account is valid, in which case there is no need to throw exceptions.

  1. private static boolean isAccountFormatCorrect(String name) {
  2. return name.indexOf('-') == name.lastIndexOf('-');
  3. }

If you need to ensure there is exactly one hyphen, you will need to check that the index is not -1.

  1. private static boolean isAccountFormatCorrect(String name) {
  2. int idx = name.indexOf('-');
  3. return idx != -1 && idx == name.lastIndexOf('-');
  4. }
英文:

Compare the first index of a hyphen with the last index where it is found. If they are equal, then there was only one.

  1. private static boolean isAccountFormatCorrect(String name) {
  2. if(name.indexOf('-') != name.lastIndexOf('-')){
  3. throw new BadAccountInputException("Only one hyphen allowed in account number");
  4. }
  5. return true;
  6. }

However, it seems you want to return a boolean indicating whether or not the account is valid, in which case there is no need to throw exceptions.

  1. private static boolean isAccountFormatCorrect(String name) {
  2. return name.indexOf('-') == name.lastIndexOf('-');
  3. }

If you need to ensure there exactly one hyphen, you will need to check that the index of not -1.

  1. private static boolean isAccountFormatCorrect(String name) {
  2. int idx = name.indexOf('-');
  3. return idx != -1 && idx == name.lastIndexOf('-');
  4. }

huangapple
  • 本文由 发表于 2020年7月23日 02:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63040635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定