即使在将int转换为String之后,Java流也会给出“无法比较的类型:int和String”

时间:2022-04-26 16:22:37

I have a Java class that squares all the numbers between 0 and a given number. Each squared number is then converted to a string using String.valueOf(number). A given digit is also converted to a String. The squared number and the given digit (both converted to strings) are then passed into a function that is supposed to count the number of times that the digit appears in the squared number as String using a stream. However, when this comparison takes place Java gives me the error:

我有一个Java类,它将0和给定数字之间的所有数字都对齐。然后使用String.valueOf(number)将每个平方数转换为字符串。给定的数字也会转换为String。然后将平方数和给定数字(都转换为字符串)传递给一个函数,该函数应该使用流来计算数字在平方数中出现的字符串的次数。但是,当这种比较发生时,Java给了我错误:

incomparable types: int and String
      int count = squareString.chars().filter(ch -> ch == criticalDigit).count();

Why does this stream given me this error when the ints have already been converted to strings and how can I successfully count the number of times a string digit appears in a string int?

当int已经转换为字符串时,为什么这个流给我这个错误?如何成功计算字符串数字出现在字符串int中的次数?

The code I currently have is:

我目前的代码是:

import java.util.stream.*;

public class CountDig {

    public static int nbDig(int n, int d) {
      int[] squaredNumbers = new int[n];
      int number = 0;
      String strV = String.valueOf(d);
      int totalCount = 0;
      for (int i = 0; i < n; i++) {
        number = i * 2;
        String squaredString = String.valueOf(number);
        totalCount += occurrenceChecker(squaredString, strV);
    }
      return totalCount;
    }

    public static int occurrenceChecker(String squareString, String criticalDigit) {
      int count = squareString.chars().filter(ch -> ch == criticalDigit).count();
      return count;
    }

}

2 个解决方案

#1


1  

chars() return IntStream So ch is integer in Lambda expression

chars()返回IntStream因此ch是Lambda表达式中的整数

If d on method nbDig is in '0' to '9', below code works.

如果方法nbDig上的d为“0”到“9”,则下面的代码有效。

squareString.chars().filter(ch -> ch == criticalDigit.charAt(0)).count()

If not you should change algorithm.

如果不是,你应该改变算法。


ok If the d can be multiple digit. below code help you.

好的如果d可以是多位数。下面的代码帮助你。

squareString.chars().filter(ch -> criticalDigit.indexOf(ch) != -1).count()

#2


1  

First of all you're trying to return an int (count) from a function with a String return type:

首先,您尝试从具有String返回类型的函数返回int(count):

public static String occurrenceChecker(String squareString, String criticalDigit)

public static String occurrenceChecker(String squareString,String criticalDigit)

Second in this line:

第二行:

  int count = squareString.chars().filter(ch -> ch == criticalDigit).count();

criticalDigit is a String and ch is a int. (chars() returns "an IntStream of char values from this sequence")

criticalDigit是一个String,ch是一个int。 (chars()返回“来自此序列的char值的IntStream”)

An alternate method would use the indexOf function to count the a given String:

另一种方法是使用indexOf函数来计算给定的String:

public static String occurrenceChecker(String squareString, String criticalDigit) {
  int temp = 0;
  int count = 0;
  while(squareString.indexOf(criticalDigit, temp) != -1) {
      count++;
      temp = squareString.indexOf(criticalDigit, temp) + criticalDigit.length();
  }
  return Integer.toString(count);
} 

This is assuming that you are wanting to return the String representation of the result

这假设您要返回结果的String表示形式

#1


1  

chars() return IntStream So ch is integer in Lambda expression

chars()返回IntStream因此ch是Lambda表达式中的整数

If d on method nbDig is in '0' to '9', below code works.

如果方法nbDig上的d为“0”到“9”,则下面的代码有效。

squareString.chars().filter(ch -> ch == criticalDigit.charAt(0)).count()

If not you should change algorithm.

如果不是,你应该改变算法。


ok If the d can be multiple digit. below code help you.

好的如果d可以是多位数。下面的代码帮助你。

squareString.chars().filter(ch -> criticalDigit.indexOf(ch) != -1).count()

#2


1  

First of all you're trying to return an int (count) from a function with a String return type:

首先,您尝试从具有String返回类型的函数返回int(count):

public static String occurrenceChecker(String squareString, String criticalDigit)

public static String occurrenceChecker(String squareString,String criticalDigit)

Second in this line:

第二行:

  int count = squareString.chars().filter(ch -> ch == criticalDigit).count();

criticalDigit is a String and ch is a int. (chars() returns "an IntStream of char values from this sequence")

criticalDigit是一个String,ch是一个int。 (chars()返回“来自此序列的char值的IntStream”)

An alternate method would use the indexOf function to count the a given String:

另一种方法是使用indexOf函数来计算给定的String:

public static String occurrenceChecker(String squareString, String criticalDigit) {
  int temp = 0;
  int count = 0;
  while(squareString.indexOf(criticalDigit, temp) != -1) {
      count++;
      temp = squareString.indexOf(criticalDigit, temp) + criticalDigit.length();
  }
  return Integer.toString(count);
} 

This is assuming that you are wanting to return the String representation of the result

这假设您要返回结果的String表示形式