0

所以我一直在尝试编写代码来查找n 个字符串中的常用字符。该代码在 IntelliJ IDEA 和 onlinegdb(具有交互式控制台)上运行良好,但它不适用于其他编译器并给我这个错误:

>Exception in thread "main" java.lang.NullPointerException
    at Main.commonCharacters(Main.java:18)
    at Main.main(GFG.java:47)

如果不是作业,那会很好,但是我们必须提交代码的网站没有交互式控制台,它给了我运行时错误

将不胜感激任何帮助。

import java.io.*;
import java.util.*;
import java.lang.*;
public class GFG {
  static int MAX_CHART = 26;
  public static void commonCharacters(String str[], int n)
  {
    Boolean[] prim = new Boolean[MAX_CHART];
    Arrays.fill(prim, new Boolean(true));
    for (int i = 0; i < n; i++) {

        Boolean[] sec = new Boolean[MAX_CHART];
        Arrays.fill(sec, Boolean.FALSE);

        for (int j = 0; j < str[i].length(); j++)
        {

            if (prim[str[i].charAt(j) - 'A'])// the compiler says the **problem** is here
               sec[str[i].charAt(j) - 'A'] = true;
        }

        System.arraycopy(sec, 0, prim, 0, MAX_CHART);
    }

    for (int i = 0; i < 26; i++)
        if (prim[i]){
            System.out.print(Character.toChars(i + 65));
        }
}

 public static void main(String[] args)
        throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    Scanner input = new Scanner(System.in);
    int n = input.nextInt();// number of strings
    String[] str = new String[n];

    for (int i=0;i < n;i++){
        str[i]=reader.readLine();//getting strings as input
    }
    n = str.length;
    commonCharacters(str, n);
     }
 }

4

0 回答 0