这就是我想要做的:从 .txt 文件中获取值并将它们添加到数组中。我遇到一个问题,它说:
“不兼容的类型。找到:'java.lang.String',必需:'java.lang.String[]'”
Educate.txt 文件包含以下值: 洗脑信仰(还有更多)
这是到目前为止我的代码。扫描仪接受诸如“educate c”之类的字符串,“educate”是文件的名称。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class New {
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
FileInputStream fileByteStream = null; //input stream
FileOutputStream fileStream = null;
char characterCheck;
String textLine;
String inputValue;
String fileValue;
String charValue;
String fileName = "";
String thirdValue;
Scanner inFS = null; //scanner
Scanner inSS = null;
Scanner inKS = null;
int NUM_CHARACTERS = 26; // Maximum number of letters
int MAX_WORDS = 10; // Maximum number of synonyms per starting letter
String[][] synonyms = new String[NUM_CHARACTERS][MAX_WORDS]; // Declare 2D array for all synonyms
String[] words = new String[MAX_WORDS]; // The words of each input line
inputValue = scnr.nextLine(); //accept file input
inSS = new Scanner(inputValue);
fileValue = inSS.next();
charValue = inSS.next();
if (fileValue.equals("educate")) {
fileName = fileValue + ".txt";
}
else if (fileValue.equals("goal")) {
fileName = fileValue + ".txt";
}
else if (fileValue.equals("happy")) {
fileName = fileValue + ".txt";
}
fileByteStream = new FileInputStream("src/" + fileName); //open file
inFS = new Scanner(fileByteStream); //assign to inFS
while (inFS.hasNextLine()) { //loop through data line by line
textLine = inFS.nextLine();//assign nextLine of file to variable
inKS = new Scanner(textLine);
for (int i = 0; i < synonyms.length; ++i) {
synonyms[i] = inKS.next();
}
}
}
}
我希望将文件中的值添加到数组中。
package com.tuling.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class New {
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
FileInputStream fileByteStream = null; //input stream
FileOutputStream fileStream = null;
char characterCheck;
String textLine;
String inputValue;
String fileValue;
String charValue;
String fileName = "";
String thirdValue;
Scanner inFS = null; //scanner
Scanner inSS = null;
Scanner inKS = null;
int NUM_CHARACTERS = 7; // Maximum number of letters
int MAX_WORDS = 7; // Maximum number of synonyms per starting letter
String[][] synonyms = new String[NUM_CHARACTERS][MAX_WORDS]; // Declare 2D array for all synonyms
String[] words = new String[MAX_WORDS]; // The words of each input line
for (int i = 0; i < synonyms.length; i++) {
for (int j = 0; j < synonyms[i].length; j++) {
synonyms[i][j] = "";
}
}
inputValue = scnr.nextLine(); //accept file input
inSS = new Scanner(inputValue);
fileValue = inSS.next();
if (fileValue.equals("educate")) {
fileName = fileValue + ".txt";
} else if (fileValue.equals("goal")) {
fileName = fileValue + ".txt";
} else if (fileValue.equals("happy")) {
fileName = fileValue + ".txt";
}
String path = New.class.getClassLoader().getResource(fileName).getFile();
fileByteStream = new FileInputStream(path); //open file
inFS = new Scanner(fileByteStream); //assign to inFS
int a = 0;
while (inFS.hasNextLine()) { //loop through data line by line
textLine = inFS.nextLine();//assign nextLine of file to variable
inKS = new Scanner(textLine);
int b = 0;
synonyms[a][b] = textLine;
while (inKS.hasNextLine()) {
synonyms[a][b++] = inKS.next();
}
a++;
}
System.out.println("=============");
for (String[] synonym : synonyms) {
System.out.println(Arrays.toString(synonym));
}
}
}