Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取文本文件中的句子并将其放入数组列表中?_Java_String_Vector_Arraylist - Fatal编程技术网

Java 如何获取文本文件中的句子并将其放入数组列表中?

Java 如何获取文本文件中的句子并将其放入数组列表中?,java,string,vector,arraylist,Java,String,Vector,Arraylist,假设文本文件包含: He is a boy . She is sick . Ali is playing . We are eating . The dog is barking . He and his brother are running . 如何将所有句子按以下格式放入数组列表: He is is a a boy boy . She is is sick sick . 等等 我使用数组列表来避免继续读取文本文件以执行某些任务,这可能吗? 或者我需要用向量来做 这应该可以。我没有测试

假设文本文件包含:

He is a boy .
She is sick .
Ali is playing .
We are eating .
The dog is barking .
He and his brother are running .
如何将所有句子按以下格式放入数组列表:

He is
is a 
a boy
boy .
She is
is sick
sick .
等等

我使用数组列表来避免继续读取文本文件以执行某些任务,这可能吗?
或者我需要用向量来做

这应该可以。我没有测试它。这只是一行代码,你需要一些东西来扫描你的输入并使用每行代码

String line = "He is a boy .";
String[] arr = line.split(" ");
ArrayList<String> newLines = ArrayList<String>();
for (int i = 1; i < arr.length; i++){
  newLines.add(arr[i-1]+" "+arr[i]);
}
String line=“他是个男孩。”;
字符串[]arr=line.split(“”);
ArrayList换行符=ArrayList();
对于(int i=1;i
这应该可以。我没有测试它。这只是一行代码,你需要一些东西来扫描你的输入并使用每行代码

String line = "He is a boy .";
String[] arr = line.split(" ");
ArrayList<String> newLines = ArrayList<String>();
for (int i = 1; i < arr.length; i++){
  newLines.add(arr[i-1]+" "+arr[i]);
}
String line=“他是个男孩。”;
字符串[]arr=line.split(“”);
ArrayList换行符=ArrayList();
对于(int i=1;i
除了Arthur所说的,您还可以使用类从文件中读取输入:

Scanner fileScanner = null;
try {
    fileScanner = new Scanner(new File("yourtextfile.txt"));
} catch (FileNotFoundException e) {
    e.printStackTrace();  
}
while (fileScanner.hasNextLine()) {
    // Some additional code for storing the output to an array around here? :]
    System.out.println(fileScanner.nextLine());
}

除了Arthur所说的,您还可以使用类从文件中读取输入:

Scanner fileScanner = null;
try {
    fileScanner = new Scanner(new File("yourtextfile.txt"));
} catch (FileNotFoundException e) {
    e.printStackTrace();  
}
while (fileScanner.hasNextLine()) {
    // Some additional code for storing the output to an array around here? :]
    System.out.println(fileScanner.nextLine());
}

同样,如果您不想从文件中读取,而是从用户输入中读取,则可以使用此选项

ArrayList<String> uInput = new ArrayList<String>(Arrays.asList(scan.nextLine().split("\\s+")));
ArrayList uInput=new ArrayList(Arrays.asList(scan.nextLine().split(\\s+)));

同样,如果您不想从文件中读取,而是从用户输入中读取,则可以使用此选项

ArrayList<String> uInput = new ArrayList<String>(Arrays.asList(scan.nextLine().split("\\s+")));
ArrayList uInput=new ArrayList(Arrays.asList(scan.nextLine().split(\\s+)));

是,使用
阵列列表
。我会用扫描仪来读取文件。让我们先来看看你是怎么想的。你对句子有一个奇怪的定义……但是有可能按照我上面提到的方式把它放到ArrayList中吗?它是如何工作的?是的,使用
ArrayList
。我会用扫描仪来读取文件。让我们先来看看你是怎么想的。你对句子有一个奇怪的定义……但是有可能按照我上面提到的方式把它放到ArrayList中吗?它是如何工作的?