gpt4 book ai didi

java - 如何在分隔值的同时创建数组列表?

转载 作者:行者123 更新时间:2023-11-30 03:29:06 26 4
gpt4 key购买 nike

所以基本上我想做的是用java制作一个词汇练习游戏。我让 java 读取 .txt 文件并将其报告给控制台。数据之间用“|”分隔字符,因为我在 .txt 文件中使用逗号。

这是代码。

package sat.vocab.practice;

import java.io.FileReader;
import com.opencsv.CSVReader;
import java.util.Arrays;

public class SATVocabPractice {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
CSVReader reader = new CSVReader(new FileReader("C:/Users/Jordan/Documents/GitHub/SAT-Vocab-Practice--New-/src/sat/vocab/practice/Words.txt"), '|' , '"' , 0);

String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null) {
System.out.println(Arrays.toString(nextLine));
}
}
}
}

单词的格式如下。

Word | Type of Speach: Definition

labyrinth|n: 1. an intricate combination of paths or passages in which it is difficult to find one's way or to reach the exit. 2. any confusingly intricate state of things or events; a bewildering complex.
cower|v: 1. to crouch, as in fear or shame.

我们从代码中返回的数据格式为[ cower|v: 1. 蹲下,如害怕或羞愧。]

我需要将数据放入两个数组列表中,一个用于单词(在|之前),一个用于定义(在|之后)。

最佳答案

试试这个:

String[] array = nextLine.split("\\|");

此函数将按字符| 分割String。将其分配给新变量,并稍后使用将每个元素添加到单独的列表中。

数组[0]这是这个词

array[1] 这是定义。

关于java - 如何在分隔值的同时创建数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29461351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com