You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将TXT文件每行的两个值分别存入两个不同数组?

解决每行双值存入两个数组的问题

嘿,我看你已经搞定了读取txt文件的基础功能,现在卡在把每行的两个值分别存进不同数组对吧?咱们一步步来解决这个问题~

首先先说说你现有代码里的几个小问题:

  • 你在while循环里每次都执行preis = new int[anzahlTanstellen];,这会导致每次循环都新建一个空数组,之前读取的数据全丢了
  • 只定义了一个preis数组,没有第二个数组来存储每行的第二个值
  • 处理行的时候没有对应好数组的索引,直接用i来赋值容易出越界问题

修改方案(用动态列表更灵活)

因为如果不确定符合条件的行数(也就是zeilennummer > 5的行有多少),用ArrayList来动态存储会比固定长度的数组更稳妥,最后再转成你需要的数组就行。代码如下:

File file = new File(txt);
if (!file.canRead() || !file.isFile()) // 检查文件是否可读
    System.exit(0);

BufferedReader in = null;
// 定义两个动态列表来存每行的两个值,假设第一个是距离,第二个是价格
List<Integer> distanceList = new ArrayList<>();
List<Integer> priceList = new ArrayList<>();

try {
    in = new BufferedReader(new FileReader(txt));
    String zeile = null;
    int zeilennummer = 0;
    
    while ((zeile = in.readLine()) != null) {
        zeilennummer++;
        // 只处理第6行及以后的内容
        if (zeilennummer > 5) {
            String[] teilen = zeile.split(" "); // 按空格分割行内容
            // 确保每行确实有两个值,避免数组越界
            if (teilen.length >= 2) {
                int distance = Integer.parseInt(teilen[0]);
                int price = Integer.parseInt(teilen[1]);
                distanceList.add(distance);
                priceList.add(price);
                // 可以打印验证
                System.out.println("距离:" + distance + ",价格:" + price);
            }
        }
    }
    
    // 如果需要转成int数组的话
    int[] distanceArray = distanceList.stream().mapToInt(Integer::intValue).toArray();
    int[] priceArray = priceList.stream().mapToInt(Integer::intValue).toArray();
    
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

如果已经知道固定行数(anzahlTanstellen)

要是你确定anzahlTanstellen就是符合条件的行数,那可以直接初始化固定长度的数组,用一个索引变量来跟踪位置:

File file = new File(txt);
if (!file.canRead() || !file.isFile()) 
    System.exit(0);

BufferedReader in = null;
int[] distanceArray = new int[anzahlTanstellen];
int[] priceArray = new int[anzahlTanstellen];
int index = 0; // 用来跟踪数组的当前索引

try {
    in = new BufferedReader(new FileReader(txt));
    String zeile = null;
    int zeilennummer = 0;
    
    while ((zeile = in.readLine()) != null && index < anzahlTanstellen) {
        zeilennummer++;
        if (zeilennummer > 5) {
            String[] teilen = zeile.split(" ");
            if (teilen.length >= 2) {
                distanceArray[index] = Integer.parseInt(teilen[0]);
                priceArray[index] = Integer.parseInt(teilen[1]);
                System.out.println("距离:" + distanceArray[index] + ",价格:" + priceArray[index]);
                index++; // 索引后移
            }
        }
    }
    
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这样就能把每行的两个值分别存进两个不同的数组里啦~

内容的提问来源于stack exchange,提问作者Nils

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:52:50