无与伦比的类型:char和String

时间:2018-07-01 13:06:46

标签: java

import java.util.*;
import java.lang.*;
public class MyClass {
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int count = 0;
    String inputEntry = in .nextLine();
    String inputCharacter = in .nextLine();
    if (inputCharacter.length() != 1) {
      System.out.println("Please enter single character");
    } else {
      for (int i = 0; i < inputEntry.length(); i++) {
        if (inputEntry.charAt(i) == inputCharacter) {
          count++;
        }
      }
      System.out.println("There is " + count + " occurence(s) of '" + inputCharacter + "' in " + inputEntry);
    }
  }
}

以上代码在第if(inputEntry.charAt(i)==inputCharacter)行给出了错误。如何解决这个问题。该错误表明无法比较的数据类型:char和string

2 个答案:

答案 0 :(得分:1)

字符串和字符不可比。

在Java中,“ a”(字符串)不等于'a'(字符)。

Scanner's nextLine()将输入作为字符串。 使用sc.next().charAt(0)接受一个字符作为输入。

下面是工作代码。

import java.util.*;
import java.lang.*;
public class MyClass
{
    public static void main(String args[])                                                                      //scn=in
                                                                                                               // str=inputEntry
                                                                                                               // c=inputCharacter
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String");
        String inputEntry = in.nextLine();
        System.out.println("Enter charcter to know its occurrence");
        char inputCharacter = in.next().charAt(0);
        int count =0;


            for(int i=0;i<inputEntry.length();i++)
            {
                char ch = inputEntry.charAt(i);
                if(ch==inputCharacter)
                {
                    count++;
                }
            } 
            System.out.println("count is:"+count);
            System.out.println("There is "+count+" occurence(s) of '"+inputCharacter+"' in "+inputEntry);
        }


}

答案 1 :(得分:0)

您必须更改

char inputCharacter = in.nextLine()

收件人

char inputCharacter = in.nextLine().charAt(0);

由于inputCharacterchar而不是String

这是修改后的代码:-

import java.util.*;
import java.lang.*;

public class MyClass {
  public static void main(String args[]) // scn=in
                                         // str=inputEntry
                                         // c=inputCharacter
  {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter String");
    String inputEntry = in.nextLine();
    System.out.println("Enter charcter to know its occurrence");
    char inputCharacter = in.nextLine().charAt(0);
    int count = 0;
                           // not needed that if statement. 

    for (int i = 0; i < inputEntry.length(); i++) {
      char ch = inputEntry.charAt(i);
      if (ch == inputCharacter) {
        count++;
      }
    }
    System.out.println("count is:" + count);
    System.out.println("There is " + count + " occurence(s) of '" + inputCharacter + "' in " + inputEntry);

  }
}

输出:-

Enter String
hello
Enter charcter to know its occurrence
l
count is:2
There is 2 occurence(s) of 'l' in hello