%user
38.71
%util
32.65
0.00
29.60
0.01
0.00
7.11
Utilization#
72%
1%
63%
Utilization#
75.18%
--------------------------------------------------------------------------------------------
OutOfMemoryError#
--------------------------------------------------------------------------------------------
0150819-00:30:01
%user
38.71
%util
32.64
0.00
29.60
0.01
0.00
7.11
Utilization#
72%
1%
63%
Utilization#
75.22%
--------------------------------------------------------------------------------------------
OutOfMemoryError#
--------------------------------------------------------------------------------------------
0150819-01:00:01
%user
38.70
%util
32.64
0.00
29.60
0.01
0.00
7.11
Utilization#
72%
1%
63%
Utilization#
74.71%
希望能给出一个程序计算出各种数据的最大值、最小值和平均值,并将结果输入到新建txt文件中。这里只给出三组数据作为例子,实际是有多组类似数据的。
8 个解决方案
#1
readLine就行了啊,你是不会写吗?
#2
不会写,刚写java。可以帮我写一下代码吗?
#3
public static void main(String [] args) throws IOException{
File fileCreate = new File("G:\\","result.txt");
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\","myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
count = count + Integer.parseInt(sz[i]);
//System.out.println(sz[i]);
}
double result=(double)count/sz.length;
return result;
}
File fileCreate = new File("G:\\","result.txt");
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\","myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
count = count + Integer.parseInt(sz[i]);
//System.out.println(sz[i]);
}
double result=(double)count/sz.length;
return result;
}
#4
上楼的程序运行出现exception in thread main java.lang.NumberFormatException: for input string :" "错误,是怎么回事?
提示的是double value=splitAndResult(str); 和count = count + Integer.parseInt(sz[i]);出错。。。求修改
提示的是double value=splitAndResult(str); 和count = count + Integer.parseInt(sz[i]);出错。。。求修改
#5
路径写错了
#6
File fileCreate = new File("G:\\result.txt");
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
//count = count + Integer.parseInt(sz[i]);)
System.out.println(Integer.parseInt(sz[i].toString()));
}
double result=(double)count/sz.length;
return result;
}
再给你贴下,这个测试了
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
//count = count + Integer.parseInt(sz[i]);)
System.out.println(Integer.parseInt(sz[i].toString()));
}
double result=(double)count/sz.length;
return result;
}
再给你贴下,这个测试了
#7
谢谢,不过还是不行。
#8
我给的只是个例子,你的数据太乱我没看,没按你那些数据弄,
只是单纯的从myfile.txt文件里一行一行的提取整型数据并计算平均值写入到result.txt
剩下的你自己修改,自己打出来的代码才是自己的
#1
readLine就行了啊,你是不会写吗?
#2
不会写,刚写java。可以帮我写一下代码吗?
#3
public static void main(String [] args) throws IOException{
File fileCreate = new File("G:\\","result.txt");
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\","myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
count = count + Integer.parseInt(sz[i]);
//System.out.println(sz[i]);
}
double result=(double)count/sz.length;
return result;
}
File fileCreate = new File("G:\\","result.txt");
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\","myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
count = count + Integer.parseInt(sz[i]);
//System.out.println(sz[i]);
}
double result=(double)count/sz.length;
return result;
}
#4
上楼的程序运行出现exception in thread main java.lang.NumberFormatException: for input string :" "错误,是怎么回事?
提示的是double value=splitAndResult(str); 和count = count + Integer.parseInt(sz[i]);出错。。。求修改
提示的是double value=splitAndResult(str); 和count = count + Integer.parseInt(sz[i]);出错。。。求修改
#5
路径写错了
#6
File fileCreate = new File("G:\\result.txt");
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
//count = count + Integer.parseInt(sz[i]);)
System.out.println(Integer.parseInt(sz[i].toString()));
}
double result=(double)count/sz.length;
return result;
}
再给你贴下,这个测试了
if(!fileCreate.exists()){
fileCreate.createNewFile();
}
File fileRead = new File("G:\\myfile.txt");
BufferedReader bReader = new BufferedReader(new FileReader(fileRead));
FileWriter fileWrite = new FileWriter(fileCreate,true);
BufferedWriter bWriter = new BufferedWriter(fileWrite);
String str=null;
while ((str = bReader.readLine()) != null) {
double value=splitAndResult(str);
bWriter.write(value+" ");
}
bWriter.close();
}
public static double splitAndResult(String str){
System.out.println(str);
String [] sz=str.split(" ");
int count=0;
for(int i=0;i<sz.length;i++){
//count = count + Integer.parseInt(sz[i]);)
System.out.println(Integer.parseInt(sz[i].toString()));
}
double result=(double)count/sz.length;
return result;
}
再给你贴下,这个测试了
#7
谢谢,不过还是不行。
#8
我给的只是个例子,你的数据太乱我没看,没按你那些数据弄,
只是单纯的从myfile.txt文件里一行一行的提取整型数据并计算平均值写入到result.txt
剩下的你自己修改,自己打出来的代码才是自己的