Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在ArrayList中查找最常见的字符串-当前返回null_Java_String_Arraylist - Fatal编程技术网

Java 在ArrayList中查找最常见的字符串-当前返回null

Java 在ArrayList中查找最常见的字符串-当前返回null,java,string,arraylist,Java,String,Arraylist,我对java还很陌生。我一直在研究如何在数组中找到最常见的字符串,但我的代码没有正常工作。我的错误是当我需要打印出最频繁的IP地址时,mostCommon打印出null 这是我的密码 public class Log_File_Analysis { private static ArrayList<String> ipAddress = new ArrayList<>(); private static String temp , mostCommon; int ma

我对java还很陌生。我一直在研究如何在数组中找到最常见的字符串,但我的代码没有正常工作。我的错误是当我需要打印出最频繁的IP地址时,
mostCommon
打印出
null

这是我的密码

public class Log_File_Analysis 
{

private static ArrayList<String> ipAddress = new ArrayList<>();
private static String temp , mostCommon;
int max = 0,  num = 0;


public String getMostUsedIpAddress()
{
     Collections.sort(ipAddress);   
    for (String string : ipAddress) 
    {
      if (string.equals(temp)) 
      {
        num++;
      } 
      else {
        if (num>max) 
        {
          max = num;
          mostCommon = string;
        }
        num = 1;
        temp = string;
      }
    }
    return mostCommon;
}

public static void main (String[] args)
{
    System.out.println("Enter a log file to be analyized");
    //Scanner keyboard = new Scanner(System.in);

    File filename = new File("small.log");              
    try
    {   
        Scanner data_store = new Scanner (filename);
        while(data_store.hasNext())
        {
            String line = data_store.nextLine(); 
            int begin = line.indexOf("[client ") + 8;
                int end = line.indexOf("]", begin);
            String ip = line.substring(begin, end);
            ipAddress.add(ip);
            System.out.println(ip);
        }
        data_store.close();
    }
    catch(FileNotFoundException e)
    {
        System.out.println("small.log was not found!");         
    }
    System.out.println(mostCommon);
}

}
公共类日志文件分析
{
私有静态ArrayList ipAddress=新ArrayList();
私有静态字符串临时值,MOSTCOMONM;
int max=0,num=0;
公共字符串getMostUsedIpAddress()
{
Collections.sort(ipAddress);
用于(字符串:ipAddress)
{
if(字符串等于(临时))
{
num++;
} 
否则{
如果(数值>最大值)
{
max=num;
mostCommon=字符串;
}
num=1;
温度=字符串;
}
}
返回最常见的;
}
公共静态void main(字符串[]args)
{
System.out.println(“输入要分析的日志文件”);
//扫描仪键盘=新扫描仪(System.in);
文件名=新文件(“small.log”);
尝试
{   
扫描仪数据存储=新扫描仪(文件名);
while(data_store.hasNext())
{
String line=data_store.nextLine();
int begin=line.indexOf(“[client”)+8;
int end=line.indexOf(“]”,begin);
字符串ip=行。子字符串(开始、结束);
ipAddress.add(ip);
系统输出打印项次(ip);
}
数据存储。关闭();
}
catch(filenotfounde异常)
{
System.out.println(“未找到small.log!”);
}
System.out.println(最常见);
}
}

请您帮助我理解我做错了什么。

您的代码中没有任何地方实际调用您的方法来确定最常见的值

您需要将其添加到
main()
方法的末尾

mostCommon = getMostUsedIpAddress();
System.out.println(mostCommon);

基本上,您已经完成了所有值的读取,因此现在需要调用您的方法来查找最常见的值,然后才能显示它。目前,您的代码正在打印
null
,因为您实际上没有在任何地方尝试设置
mostCommon
的值。

您似乎没有调用getMostUsedIpAddress方法

试着改变 System.out.println(最常见); 到 System.out.println(getMostUsedIpAddress())


因为您返回的是MOSTCOMON,而不是将其设置为变量

您没有初始化两个字符串temp和mostCommon。 这可能是主要问题!
它们通常初始化为字符串temp=“”;如果它们没有内容(就像将int值初始化为0)

它看起来不像是调用过的
getMostUsedIpAddress
,如果您想从
main
调用它,您还应该将其设置为
static
,因为
getMostUsedIpAddress
是一个实例,所以这不起作用method@WATTO谢谢你!!我不知道我怎么会错过那个lol:)