有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中从命令行中的文件获取输入

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

请考虑两个学生的档案,包括他们的姓名和密码,姓名和电子邮件地址。人们可能希望将这些信息结合起来,得到一个包含姓名、电子邮件和密码的文件(按字母顺序)

输入将通过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的控制台或命令行分别添加每一行时,它就会工作


共 (1) 个答案

  1. # 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();