我整天都在这个程序上遇到压力问题,要读取整数的文本文件并将整数存储到数组中。我以为我终于得到了以下代码的解决方案。
但遗憾的是......我必须使用hasNextLine()方法遍历文件。 然后使用nextInt()从文件中读取整数并将它们存储到数组中。 所以使用扫描构造函数,hasNextLine(),next()和nextInt()方法。
然后使用 try并catch 来确定哪些单词是整数,哪些单词不是使用InputMismatchException。文件中的空行也是例外吗? 问题是我没有使用try和catch和exception,因为我只是跳过了无内容。 另外,我正在使用一个int数组,所以我想在没有列表的情况下这样做。
public static void main(String[] commandlineArgument) {
Integer[] array = ReadFile4.readFileReturnIntegers(commandlineArgument[0]);
ReadFile4.printArrayAndIntegerCount(array, commandlineArgument[0]);
}
public static Integer[] readFileReturnIntegers(String filename) {
Integer[] array = new Integer[1000];
int i = 0;
//connect to the file
File file = new File(filename);
Scanner inputFile = null;
try {
inputFile = new Scanner(file);
}
//If file not found-error message
catch (FileNotFoundException Exception) {
System.out.println("File not found!");
}
//if connected, read file
if (inputFile != null) {
// loop through file for integers and store in array
try {
while (inputFile.hasNext()) {
if (inputFile.hasNextInt()) {
array[i] = inputFile.nextInt();
i++;
}
else {
inputFile.next();
}
}
}
finally {
inputFile.close();
}
System.out.println(i);
for (int v = 0; v < i; v++) {
System.out.println(array[v]);
}
}
return array;
}
public static void printArrayAndIntegerCount(Integer[] array, String filename) {
//print number of integers
//print all integers that are stored in array
}
}
然后我将在第二种方法中打印所有内容,但我以后可以担心。 :○
文本文件的示例内容:
Name, Number
natto, 3
eggs, 12
shiitake, 1
negi, 1
garlic, 5
umeboshi, 1
示例输出目标:
number of integers in file "groceries.csv" = 6
index = 0, element = 3
index = 1, element = 12
index = 2, element = 1
index = 3, element = 1
index = 4, element = 5
index = 5, element = 1
对不起类似的问题。我非常紧张,甚至更多的是我做错了......我完全陷入了这一点:(
答案 0 :(得分:1)
您可以通过这种方式阅读您的文件。
/* using Scanner */
public static Integer[] getIntsFromFileUsingScanner(String file) throws IOException {
List<Integer> l = new ArrayList<Integer>();
InputStream in = new FileInputStream(file);
Scanner s = new Scanner(in);
while(s.hasNext()) {
try {
Integer i = s.nextInt();
l.add(i);
} catch (InputMismatchException e) {
s.next();
}
}
in.close();
return l.toArray(new Integer[l.size()]);
}
/* using BufferedReader */
public static Integer[] getIntsFromFile(String file) throws IOException {
List<Integer> l = new ArrayList<Integer>();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
try {
l.add(Integer.parseInt(line.split(",")[1]));
} catch (NumberFormatException e) {
}
}
return l.toArray(new Integer[l.size()]);
}
使用您的代码:
public static void main(String[] commandlineArgument) {
Integer[] array = getIntsFromFileUsingScanner(commandlineArgument[0]);
ReadFile4.printArrayAndIntegerCount(array, commandlineArgument[0]);
}
答案 1 :(得分:0)
以下是满足新要求的一种方法,
public static Integer[] readFileReturnIntegers(
String filename) {
Integer[] temp = new Integer[1000];
int i = 0;
// connect to the file
File file = new File(filename);
Scanner inputFile = null;
try {
inputFile = new Scanner(file);
}
// If file not found-error message
catch (FileNotFoundException Exception) {
System.out.println("File not found!");
}
// if connected, read file
if (inputFile != null) {
// loop through file for integers and store in array
try {
while (inputFile.hasNext()) {
try {
temp[i] = inputFile.nextInt();
i++;
} catch (InputMismatchException e) {
inputFile.next();
}
}
} finally {
inputFile.close();
}
Integer[] array = new Integer[i];
System.arraycopy(temp, 0, array, 0, i);
return array;
}
return new Integer[] {};
}
public static void printArrayAndIntegerCount(
Integer[] array, String filename) {
System.out.printf(
"number of integers in file \"%s\" = %d\n",
filename, array.length);
for (int i = 0; i < array.length; i++) {
System.out.printf(
"\tindex = %d, element = %d\n", i, array[i]);
}
}
输出
number of integers in file "/home/efrisch/groceries.csv" = 6
index = 0, element = 3
index = 1, element = 12
index = 2, element = 1
index = 3, element = 1
index = 4, element = 5
index = 5, element = 1
答案 2 :(得分:0)
也许您可以通过我在下面发布的简短代码来解决此问题。
public static List<Integer> readInteger(String path) throws IOException {
List<Integer> result = new ArrayList<Integer>();
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = null;
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = null;
line = reader.readLine();
String input = null;
while(line != null) {
input = line.split(",")[1].trim();
matcher = pattern.matcher(input);
if(matcher.matches()) {
result.add(Integer.valueOf(input));
}
line = reader.readLine();
}
reader.close();
return result;
}
答案 3 :(得分:0)
将try catch包含在以下代码中:
try
{
if (inputFile.hasNextInt()) {
array[i] = inputFile.nextInt();
i++;
}
else {
inputFile.next();
}
}catch(Exception e)
{
// handle the exception
}