码迷,mamicode.com
首页 > 编程语言 > 详细

返回整数数组中最大数组的和(续,文件读入)

时间:2020-02-28 20:48:26      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:mat   run   lse   缓冲   als   height   split   package   equals   

这篇博客是在之前那片博客的基础上添加了新的要求和改进

但是本题的思想和学习的知识点是从舍友那里学的,本身文件读取就是自己的弱点。

题目要求:

1.要求数组从文件读取

2.如果输入的数组很大,并且有很多大的数字,就会产生比较大的结果(参考一下数的溢出),请保证你的程序能正常输出

3.另外如果输入文件的参数有错误,这个程序应该能正常退出,并显示相应的错误信息,任何输入错误都不能导致你的程序崩溃

做题思路:

1.文件读取,使用InputStreamReader类按着字符流读取数和BufferedReader类从字符输入流中读取文本并缓冲字符。

2.使用BigInteger来解决大数字溢出的问题      相关学习:https://blog.csdn.net/qq_41668547/article/details/87628618

3.使用try_catch机制,可控出现错误的可能

源代码:

package java_study;

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 array {
    public static void main(String[] args) {
    
        FindArrayList(getfile());
    }
    private static BigInteger FindArrayList(File file) {
        //使用BigInteger读取文件
        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("00000000000000000000000000000000000000000");  
        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("整形子数组和的最大值为:");
         
             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;
           }
         
    }
    }

}

文件截图:

技术图片

 

 代码截图:

技术图片

 

返回整数数组中最大数组的和(续,文件读入)

标签:mat   run   lse   缓冲   als   height   split   package   equals   

原文地址:https://www.cnblogs.com/hhjing/p/12378728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!