java - 通过来自外部文件java的二维数组操作字符串和整数

标签 java arrays io

我正在尝试设计一个程序,从外部文件获取数据,将变量存储到数组,然后允许操作。样本输入:

   String1 intA1 intA2
   String2 intB1 intB2
   String3 intC1 intC2
   String4 intD1 intD2
   String5 intE1 intE2

我希望能够从数组中获取这些值并按如下方式操作它们; 对于每个字符串,我希望能够采用 StringX 并计算((intX1+ intX2)/) 对于每个 int 列,我希望能够执行以下操作(intA1 + intB1 + intC1 + intD1 + intE1)

这就是我目前所掌握的,有什么建议吗? **请注意,我的类(class)中尚未教授 Java 命名约定。

public class 2D_Array {
  public static void inputstream(){
    File file = new File("data.txt");  
    try (FileInputStream fis = new FileInputStream(file)) {  
        int content;  
        while ((content = fis.read()) != -1) {  
            readLines("data.txt");
            FivebyThree();
            System.out.print((char) content);  
        }   
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
  }
  public static int FivebyThree() throws IOException {
    Scanner sc = new Scanner(new File("data.txt"));
    int[] arr = new int[10];
    while(sc.hasNextLine()) {

        String line[] = sc.nextLine().split("\\s");
        int ele = Integer.parseInt(line[1]);
        int index = Integer.parseInt(line[0]);
        arr[index] = ele;

    }
    int sum = 0;
    for(int i = 0; i<arr.length; i++) {
        sum += arr[i];
        System.out.print(arr[i] + "\t");
    }
    System.out.println("\nSum : " + sum);
    return sum;
  }
  public static String[] readLines(String filename) throws IOException {
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    List<String> lines = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) 
        {
            lines.add(line);
        }

    return lines.toArray(new String[lines.size()]);
        }

    /*  int[][] FivebyThree = new int[5][3];
        int row, col;
        for (row =0; row < 5; row++) {
            for(col = 0; col < 3; col++) {
                System.out.printf( "%7d", FivebyThree[row][col]);
            }
            System.out.println();*/


  public static void main(String[] args)throws IOException {

   inputstream();

  }
}

最佳答案

我发现您读取了 data.txt 两次,并且根本不使用第一次读取的结果。我不明白,你想用 String 做什么,但是拥有二维数组并计算 int 列的总和非常简单:

public class Array_2D {
    static final class Item {
        final String str;
        final int val1;
        final int val2;

        Item(String str, int val1, int val2) {
            this.str = str;
            this.val1 = val1;
            this.val2 = val2;
        }
    }

    private static List<Item> readFile(Reader reader) throws IOException {
        try (BufferedReader in = new BufferedReader(reader)) {
            List<Item> content = new ArrayList<>();
            String str;

            while ((str = in.readLine()) != null) {
                String[] parts = str.split(" ");
                content.add(new Item(parts[0], Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
            }

            return content;
        }
    }

    private static void FivebyThree(List<Item> content) {
        StringBuilder buf = new StringBuilder();
        int sum1 = 0;
        int sum2 = 0;

        for (Item item : content) {
            // TODO do what you want with item.str
            sum1 += item.val1;
            sum2 += item.val2;
        }

        System.out.println("str: " + buf);
        System.out.println("sum1: " + sum1);
        System.out.println("sum2: " + sum2);
    }


    public static void main(String[] args) throws IOException {
        List<Item> content = readFile(new InputStreamReader(Array_2D.class.getResourceAsStream("data.txt")));
        FivebyThree(content);
    }
}

关于java - 通过来自外部文件java的二维数组操作字符串和整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46376951/

相关文章:

java - 简单的密码和 I/O

javascript - 将变量从 jsp 页面传递到 servlet

java - <c :forEach not rendering with tomcat 7

java - 如何在 freemarker 模板中获取主机名?

php - 正则表达式为 PHP 数组的每个项目添加后缀

c# - 如何使用最少的锁定访问文件

java - Java 中的线程安全多线程

c++ - 从 std::array 获取对原始数组的引用

arrays - 如何将&mut ndarray::Array传递给函数并对其执行按元素的算术运算?

c# - .xml、.ini 或 .txt 哪个读取速度快?