文件读入最大子数组

package kai;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;

public class newFindMax {
public static void main(String[] args) {
//"C:\\Users\\Administrator\\Desktop\\kkk.txt"
//判断文件路径输入是否正确

FindArrayList(getfile());
}
private static BigInteger FindArrayList(File file) {
ArrayList<String> arrayList = new ArrayList<>();
try {
InputStreamReader input = new InputStreamReader(new FileInputStream(file));
BufferedReader bf = new BufferedReader(input);
// 按行读取字符串
String str;
while ((str = bf.readLine()) != null) {
arrayList.add(str);
}
bf.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对ArrayList中存储的字符串进行处理
int count=0;
int count2=0;
ArrayList<String> number = new ArrayList<>();
int length = arrayList.size();
for (int i = 0; i < length; i++) {
String [] s = arrayList.get(i).split(" ");
int width=s.length;
for(int j=0;j<width;j++) {
if(!s[j].equals("")&&s[j].matches("^-?\\d+$")) {
number.add(s[j]);
count++;
}
count2++;
}

}
System.out.println("本次共识别"+count+"个数据;有"+(count2-count)+"个非数字数据不能识别");
int numbersize=number.size();
BigInteger q=new BigInteger("-99999999999999999999999999999999999999999999");
for(int i=1;i<numbersize;i++) {
ArrayList<String> newNumber=new ArrayList<String>();
for(int j=0;j<number.size();j++) {
newNumber.add(number.get((i+j)%numbersize));
}

q= max(selectArray(newNumber),q);

}

System.out.println(q);
return q;
}
public static BigInteger selectArray(ArrayList<String> number) {
BigInteger ans=new BigInteger((String)number.get(0));
for(int i = 1;i < number.size(); i++){
BigInteger big1=new BigInteger((String)number.get(i));
BigInteger big2=new BigInteger((String)number.get(i-1));
BigInteger big3=big1.add(big2);
if(big3.compareTo(big1)>=0) {
number.set(i, big3.toString());
}

}

for(int i=0;i<number.size();i++) {
BigInteger big=new BigInteger((String)number.get(i));
ans=max(ans,big);
}

return ans;

}

private static BigInteger max(BigInteger big1, BigInteger ans) {
// TODO Auto-generated method stub
if(big1.compareTo(ans)>=0) {
return big1;
}
else return ans;

}
public static File getfile() {
System.out.println("请输入文件路径");
Scanner sc=new Scanner(System.in);
while(true) {
String line=sc.nextLine();
File kk=new File(line);
if(!kk.isFile()) {
System.out.println("输入的不是文件路径,请重新输入");
}
else if(kk.isFile()) {
return kk;
}

}
}

}

上一篇:一位数组从文件中获取时(求出最大子数组的和并且正确处理错误的信息)


下一篇:Rabin简单加解密