下面java程序哪出错了?
程序功能:统计文件中各个字符串(ip地址)出现的次数代码如下:importjava.io.BufferedReader;importjava.io.BufferedWri...
程序功能:统计文件中各个字符串(ip地址)出现的次数
代码如下:import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class IpDistribution {
public static void main(String[] args) {
BufferedReader br1 = null;
BufferedReader br2 = null;
List<String> ips = new ArrayList<String>();
List<Integer> cishu = new ArrayList<Integer>();
try {
String line = null;
String temp = null;
String xx = null;
int m = 0;
br1 = new BufferedReader(new FileReader("F:\\guyi\\test.ips"));
br2 = new BufferedReader(new FileReader("F:\\guyi\\test.ips"));
while ((xx = br1.readLine()) != null) {
temp = xx;
if (!ips.contains(xx)) {
ips.add(temp);
while ((line = br2.readLine()) != null) {
if (line.matches(temp)) {
m++;
}
}
cishu.add(m);
m=0;
}
}
for (int i = 0; i < cishu.size(); i++) {
System.out.println(cishu.get(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
test.ips文件如下:
202.195.120.2
202.195.120.5
202.195.120.4
202.195.120.2
202.195.120.2
202.195.120.5
202.195.120.4
202.195.120.4
上面程序结果为3 0 0,请问怎么修改 使得程序结果变成3 2 3 展开
代码如下:import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class IpDistribution {
public static void main(String[] args) {
BufferedReader br1 = null;
BufferedReader br2 = null;
List<String> ips = new ArrayList<String>();
List<Integer> cishu = new ArrayList<Integer>();
try {
String line = null;
String temp = null;
String xx = null;
int m = 0;
br1 = new BufferedReader(new FileReader("F:\\guyi\\test.ips"));
br2 = new BufferedReader(new FileReader("F:\\guyi\\test.ips"));
while ((xx = br1.readLine()) != null) {
temp = xx;
if (!ips.contains(xx)) {
ips.add(temp);
while ((line = br2.readLine()) != null) {
if (line.matches(temp)) {
m++;
}
}
cishu.add(m);
m=0;
}
}
for (int i = 0; i < cishu.size(); i++) {
System.out.println(cishu.get(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
test.ips文件如下:
202.195.120.2
202.195.120.5
202.195.120.4
202.195.120.2
202.195.120.2
202.195.120.5
202.195.120.4
202.195.120.4
上面程序结果为3 0 0,请问怎么修改 使得程序结果变成3 2 3 展开
4个回答
展开全部
容器改用map,只写main方法部分:
public static void main(String[] args) {
BufferedReader br1 = null;
Map<String, Integer> ipCount = new HashMap<String, Integer>();
try {
String line = null;
Integer count = null;
br1 = new BufferedReader(new FileReader("F:\\guyi\\test.ips"));
//计数
while ((line = br1.readLine()) != null) {
count = ipCount.get(line);
ipCount.put(line, count==null ? count : count++);
}
//遍历ipCount map
Set<Entry<String, Integer>> ips = ipCount.entrySet();
for(Entry<String, Integer> ip : ips)
System.out.println(String.format("%s ---> %d", ip.getKey(), ip.getValue().intValue()));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
BufferedReader br1 = null;
Map<String, Integer> ipCount = new HashMap<String, Integer>();
try {
String line = null;
Integer count = null;
br1 = new BufferedReader(new FileReader("F:\\guyi\\test.ips"));
//计数
while ((line = br1.readLine()) != null) {
count = ipCount.get(line);
ipCount.put(line, count==null ? count : count++);
}
//遍历ipCount map
Set<Entry<String, Integer>> ips = ipCount.entrySet();
for(Entry<String, Integer> ip : ips)
System.out.println(String.format("%s ---> %d", ip.getKey(), ip.getValue().intValue()));
} catch (IOException e) {
e.printStackTrace();
}
}
2010-07-11
展开全部
br1 = new BufferedReader(new FileReader("F:\\guyi\\test.ips"));
把它改成br1 = new BufferedReader(shuty FileReader("F:\\bbs\\test.hti
"));就行了,如果还不可以,我不知道了。
把它改成br1 = new BufferedReader(shuty FileReader("F:\\bbs\\test.hti
"));就行了,如果还不可以,我不知道了。
参考资料: Java应用程序
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把IP地址202.195.120.5改为202.199.156这是我家的,不知道对你合不合适。
或者添加System:may be(System.out.println(cishu.get(i));可能行,但是要小心导致电脑奔溃额。
或者添加System:may be(System.out.println(cishu.get(i));可能行,但是要小心导致电脑奔溃额。
参考资料: 我家的JAVA绝对牛B
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把br2 = new BufferedReader(new FileReader("C:\\test.ips"));剪切到ips.add(temp);下面试试吧;第一次循环内部while已经把文件读到结尾了,而你内部while判断条件又是(line = br2.readLine()) != null,所以之后内部while都跳过了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询