【问题标题】:Taking inputs from a file in the command line in Java在 Java 的命令行中从文件中获取输入
【发布时间】:2018-07-08 17:57:30
【问题描述】:

对于以下问题,我无法通过命令行将文件解析为输入。

考虑两个关于学生的文件,分别包含他们的姓名和密码,以及姓名和电子邮件地址。有人可能希望将这些组合起来以得到一个包含姓名、电子邮件和密码的文件(按字母顺序)。

输入将通过 STDIN 输入,格式如下:

NUMBER OF RECORDS 
NAME1 FIELD1 
NAME2 FIELD1 
... 
NAMEN FIELD1 
NAME1 FIELD2 
NAME2 FIELD2 
... 
NAMEN FIELD2 

您的输出应采用以下形式:

NAME1' FIELD1 FIELD2 
NAME2' FIELD1 FIELD2 
... 
NAMEN' FIELD1 FIELD2 

输出的排序位置(因此是 ')。 因此,例如,给定以下输入:

3 
a 1 
c 2 
b 3 
b 4 
c 5 
a 6 

您的程序应提供以下输出:

a 1 6 
b 3 4 
c 2 5

我的代码如下:

import java.util.*;
import java.io.*;
public class Combiner 
{
     public static void main(String[] args) throws IOException
        {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        Scanner scanner = new Scanner(System.in);
        //first line number of students
        int numOfStudents = scanner.nextInt();
        scanner.nextLine();
//      ins1 is the array of inputs of name and UN
        String[] ins1 = new String[numOfStudents];
        //ins2 is the array of inputs of name and Password
        String[] ins2 = new String[numOfStudents];
        //collect all inputs of Student Name, UN
        for (int i = 0; i < numOfStudents; i++)
        {
            ins1[i] = stdin.readLine();
        }
        //collect all inputs of Student Name, Password
        for (int i = 0; i<numOfStudents; i++)
        {
             ins2[i] = stdin.readLine();
        }
        //sort both arrays
        Arrays.sort(ins1);
        Arrays.sort(ins2);

         for(int i =0; i<numOfStudents; i++)
        {
            //gets the last word from each element of ins2
            String toAdd = getLast(ins2[i]);
            //concats that to each element of ins 1
            ins1[i]= ins1[i] + " " + toAdd;
        }
        //print the result
        for(int i =0; i<numOfStudents; i++)
        {
            System.out.println(ins1[i]);
        }  
    }
      public static String getLast(String x)
    {
        //splits x into an array of words seperated by a space
        String[] split = x.split(" ");
        //gets the last element in that array
        String lastWord = split[split.length - 1];
        return lastWord;
    }
}

当我从命令行输入时,我得到了所需的输出。但是当我使用对诸如 C:\Users\Stephen\Documents\3 之类的文件的引用时,它只是一个包含

的文件
3 
a 1 
c 2
b 3
b 4 
c 5 
a 6 

抛出异常

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.ThrowFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Combiner.main(Combiner.java.10)

第 10 行是

int numOfStudents = scanner.nextInt();

我不知道它有什么问题,当通过我的 IDE 控制台或命令行单独添加每一行时它会起作用

【问题讨论】:

  • 你确定 3 是输入文件中的第一件事吗?它之前没有任何新行或空格吗?
  • 是的,我在尝试使用的每个文件上都遇到了同样的错误,包括讲师提供的文件
  • 然后使用 nextLine() 并打印出你得到的东西。我怀疑这是一个数字。看看它是什么,这应该可以帮助你弄清楚你从第一行的文件中得到了什么
  • 你曾经在课堂上使用过哈希集和/或数组列表吗?因为 HashSet> 的数据结构将是一个很好的数据结构来表示您正在解析和存储的输入
  • 为什么要使用扫描仪?如果您使用“stdin”读取第一行,它的表现是否更好(从文件中读取)?使用: int numOfStudents = Integer.valueOf(stdin.readLine()).intValue();

标签: java file parsing command-line command


【解决方案1】:

您能否分享显示您如何尝试提供文件名的代码?据我所知,你的第一段代码是:

scanner.nextInt();

它不会从文件中读取,它正在等待用户输入。另外,我看不到您从文件中读取的位置或方式,也许我错过了?

好的,所以下面的第 1 行正在创建一个 InputStream,然后使用 FileInputStream 从指定的文件中读取。

第 2 行使用 BufferedReaderInputStreamReader 将第 1 行中的 InputStream 分配给变量 stdin

第 3 行只是让您有一个空字符串用于保存读取的每一行。

第 4-6 行包含一个 while 循环,它使用 stdin.readLine() 从文件中读取每一行。在读取每一行时,将其分配给line 变量,如果内容不是null,则进入while 循环并尝试输出该行。在while 循环内部,您可以对文件中的每一行进行额外处理。我希望这会有所帮助,我会回来看看!

InputStream is = new FileInputStream("C:\\Users\\Stephen\\Documents\\3");
BufferedReader stdin = new BufferedReader(new InputStreamReader(is));
String line = "";
while((line = stdin.readLine()) != null) {
    System.out.println(line);
}

好的,你希望用户提供文件名吗?尝试在我上面提供的代码之上添加它。然后替换FileInputStream中的路径使用变量fileName

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the full path to the file containing student data, then press [Enter]: " );
String fileName = scanner.next();

【讨论】:

  • 用户应该输入信息,但为了更快的评估,讲师声称文件的解析方式与控制台行的解析方式相同。我不确定是否有办法同时做到这一点。我也可能试图错误地引用该文件。
  • 通过将 System.in 提供给 InputStreamReader,您仍在寻找用户输入。我在第一个响应中添加了一些代码以便于阅读,试图把它放在 cmets 中弄得一团糟。
  • 而且程序在用户输入的情况下工作得非常好,但是为了在更大的输入上进行测试,每次测试必须输入数百行是非常麻烦的
  • 这仅适用于特定文件,3是我自己测试目的的任意文件名,我不知道实际评估文件的名称是什么。
  • 我真的想知道是否有一种方法可以通过命令行调用文件,以便程序将其解释为就像用户逐行输入一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
  • 2011-07-20
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
相关资源
最近更新 更多