什么是在 if else 语句中正确使用多个 charAt 的方式?

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

What is the correct way to use multiple charAt's in a if else statement?

问题

import java.util.Scanner;

public class nameInfo {

  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.println("Enter Job Name:");
    String jobName = console.next();

    if (Character.isLetter(jobName.charAt(0)) && Character.isLetter(jobName.charAt(1)) && Character.isLetter(jobName.charAt(jobName.length() - 2)) && Character.isLetter(jobName.charAt(jobName.length() - 1))) {
      System.out.println("Job name approved");
    } else {
      System.out.println(jobName + " is not a valid job name.");
    }
    System.out.println("part 1 End");
  }
}

Please note that the corrected code now follows proper Java syntax and function usage.

英文:

trying to check the user's input for the first and last two letters being a Letter datatype for a text-based game. I keep receiving a not a statement error. Not sure why any help would be appreciated.

import java.util.Scanner.*;

public class name info{

  public static void main(String[]args) {
    Scanner console= new Scanner(System.in);
    System.out.println("Enter Job Name:");
    String JobName = console.nextInt();

    if (isLetter(str.charAt(str.length-1))&&(isLetter(str.CharAt(1)){
      system.out.println ("Job name approved");
    }
    else {
     System.out.Println(JobName+" is not a valid job name.");
    }
    System.out.Println("part 1 End");
  }
}

error: ')' expected
error:not a statement

答案1

得分: 1

你的代码中有两个问题。首先,你必须在这一行之前加上一个 "if":

(isLetter(str.charAt(str.length-1))&&isLetter(str.CharAt(1))

所以代码是:

import java.util.Scanner;

public class EmploymentInfo {

  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.println("请输入您的姓氏:");
    String LastName = console.nextLine();

    if (foundedSpaces(LastName) < 2) { //cond1
      if (isLetter(LastName.charAt(LastName.length() - 1)) && isLetter(LastName.charAt(0))) { //cond2
        //如果条件1和条件2都满足要做什么
      } else {
        //如果只有条件1满足要做什么
      }//结束条件2的if语句
    } else {
      //如果条件1不满足要做什么
    }//结束条件1的if语句
    //其他代码
  }

  public static int foundedSpaces(String s) {
    char space = ' ';
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s.charAt(i) == space) {
        count++;
      }
    }
    return count;
  }

  public static boolean isLetter(char c) {
    return Character.isLetter(c);
  }
}

另一个问题是使用了 indexOf。你想要检查用户输入是否 不超过两个空格,但是 indexOf 方法 返回在该字符串中指定字符第一次出现的索引参见 Oracle 网站)。

如下所示,这是一个计算字符串中空格数量的方法:

public static int foundedSpaces(String s) {
  char space = ' ';
  int count = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == space) {
      count++;
    }
  }
  return count;
}

你可以用 foundedSpaces(LastName) < 2 替换 LastName.indexOf(" ") < 2

另一个问题是使用了 java.util.Scanner 的 nextInt() 方法:这个方法 将输入的下一个标记扫描为一个整数参见此链接),但是你想要一个字符串。请将其替换为 nextLine()

英文:

There are two problem in your code. First of all, you have to put an "if" before the line:

(isLetter(str.charAt(str.length-1))&amp;&amp;(isLetter(str.CharAt(1))

So the code is:

import java.util.Scanner.*;

public class EmploymentInfo{

  public static void main(String[]args) {
    Scanner console= new Scanner(System.in);
    System.out.println(&quot;Please enter your last name:&quot;);
    String LastName = console.nextInt();

    if(LastName.indexOf(&quot; &quot;)&lt;2){ //cond1
      if (isLetter(str.charAt(str.length-1))&amp;&amp;(isLetter(str.CharAt(1)){ //cond2
        //What to do if condition 1 &amp; 2 are verified
      }else{
        //What to do if only cond 1 is verified
      }//end if cond2
    }else {
        //what if cond 1 is NOT verified
    }//end if cond1
    //other code
  }
}

Another problem is the use of indexOf. You want to check if the user input not uses more than two spaces, but indexOf Returns the index within this string of the first occurrence of the specified character (see Oracle site)


As suggested, here a method to find the number of spaces in your string:

public static int foundedSpaces(String s){
  char space = &#39; &#39;;
  int count = 0;
  for (int i = 0; i &lt; s.length(); i++) {
      if (s.charAt(i) == space) {
          count++;
      }
  }
  return count;
}

You can can call foundedSpaces(LastName)&lt;2 instead of LastName.indexOf(&quot; &quot;)&lt;2

Another problem is the use of java.util.Scanner.nextInt(): this method scans the next token of the input as an int (see this), but you want a string. Replace it with nextLine().

答案2

得分: 0

你用来检查条件的逻辑是不正确的。然而,我强烈建议在这里使用正则表达式选项与 String#matches

String LastName = console.nextInt();
if (lastName.matches("(?i)(?!.* .* .* )[A-Z]{2}")) {
    System.out.println("Last name approved");
} else {
    System.out.Println(LastName + " is not a valid last name.");
}
英文:

The logic you are using to check the conditions is incorrect. However, I would strongly advise using a regex option with String#matches here:

String LastName = console.nextInt();
if (lastName.matches(&quot;(?i)(?!.* .* .* )[A-Z]{2}&quot;)) {
    System.out.println(&quot;Last name approved&quot;);
else {
    System.out.Println(LastName + &quot; is not a valid last name.&quot;);
}

huangapple
  • 本文由 发表于 2020年7月26日 13:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096546.html
匿名

发表评论

匿名网友

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

确定