简体   繁体   English

如何使用Scanner类打印纯文本文件?

[英]How do I use a Scanner class to Print a plain text file?

I'm attempting to create a Scanner Class which is supposed to output a plain text file which contains the following information; 我正在尝试创建一个Scanner类,该类应该输出包含以下信息的纯文本文件;

PersonName, Address, City, Phone_Number, PersonName, Address, City, Phone_Number, PersonName, Address, City, Phone_Number, PersonName, Address, City, Phone_Number, PersonName,地址,城市,电话号码,PersonName,地址,城市,电话号码,PersonName,地址,城市,电话号码,PersonName,地址,城市,电话号码,

My Delimiter is the commas. 我的定界符是逗号。

Each set of data has to be output in the form of a Binary Tree. 每组数据必须以二叉树的形式输出。 With the Name as the rootnode firstly, then the address as the rootnode, then the city as rootnode and so on. 首先以名称为根节点,然后以地址为根节点,然后以城市为根节点,依此类推。

This is my Java file that i've created and it simply outputs "usage: java Scanner_Two telephone.txt " + "file location" everytime with no tree underneath. 这是我创建的Java文件,每次都输出“用法:java Scanner_Two Telephone.txt” +“文件位置”,而下面没有树。 Can anyone show me where i've gone wrong. 谁能告诉我我哪里出问题了。 Ive created an Entry class, BinaryTreeNode class and Binary Tree class aswell. 我已经创建了Entry类,BinaryTreeNode类和Binary Tree类。

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Scanner_Two
{

     private static void readFile(String TreeName)
     {

       try {
      // Scanner input = new Scanner (new File("telephone.txt")).useDelimiter("\\s*,\\s*");
         Scanner scanned = new Scanner(new File(TreeName));

         scanned.useDelimiter
            (System.getProperty("line.separator"));

         while (scanned.hasNext())
         {
           parseLine(scanned.next());
         }

         scanned.close();
       } catch (FileNotFoundException e) {
         e.printStackTrace();
       }
     }

private static void parseLine(String line)
{
       Scanner lineScanner = new Scanner(line);
       lineScanner.useDelimiter("\\s*,\\s*");

       String rootnode = lineScanner.next();
       String bone = lineScanner.next();
       String btwo = lineScanner.next();
       String bthree = lineScanner.next();

       System.out.println("Name: " + rootnode + " Address: " + bone + ", City: " + btwo + ", Telephone Number: " + bthree);
}

public static void main(String[] args)
{
       if(args.length != 1)
       {

         System.out.println("usage: java Scanner_Two Person.txt " + "file location");
         System.exit(0);

       }

       readFile(args[0]);
}

}

If you are using 如果您正在使用

if(args.length != 1)
 {           
     System.out.println("usage: java Scanner_Two Person.txt " + "file location");    
     System.exit(0);    
 }    

and you get this message actually printed out, it means that you either have less than 1 parameter or more than one parameter. 并且您实际上得到了打印此消息,这意味着您具有少于1个参数或多个参数。 You should check this. 您应该检查一下。

The issue that you are describing is not an issue with the Scanner class, as you had indicated in your title, but in fact in your handling of arguments. 正如标题中所指出的那样,您所描述的问题与Scanner类无关,实际上与您处理参数有关。 In the following code, (located in your main method) we can see that your program fails before it even hits a Scanner declaration: 在下面的代码中(位于您的main方法中),我们可以看到您的程序在未达到Scanner声明之前就已失败:

if(args.length != 1) {
System.out.println("usage: java Scanner_Two Person.txt " + "file location");
System.exit(0);
}

Since this code is executing, we know that args comes into the main method with either 0 or more than 1 element. 由于该代码正在执行,因此我们知道args使用0或1个以上的元素进入main方法。 I'm going to take a wild swing and guess that you're a newbie, in which case you should really be looking into how to use command line arguments . 我将大步向前,猜测您是新手,在这种情况下,您应该真正研究如何使用命令行参数 Typically command line arguments are only used if you're executing through some command line environment (such as terminal or command prompt). 通常,只有在通过某些命令行环境(例如终端或命令提示符)执行时,才使用命令行参数。 If you're using an IDE, entering such parameters can be done although it is IDE specific. 如果您使用的是IDE,则尽管输入特定于IDE,也可以输入此类参数。

If you tell us specifically how you are executing the code, and, if you do it through the command-line, the specific command you use, I could provide a more detailed answer. 如果您具体告诉我们如何执行代码,并且如果通过命令行(即使用的特定命令)来执行代码,我将提供更详细的答案。

Even easier than learning about command line environments, and more convenient for repeated testing, would be using a hardcoded main method: 使用硬编码的main方法甚至比了解命令行环境更容易,并且对于重复测试更加方便,该方法是:

public static void main(String[] args) {
readFile("C:\\[filepath]\\Person.txt");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM