【问题标题】:Help with line.split CSV帮助 line.split CSV
【发布时间】:2010-02-16 01:53:34
【问题描述】:

我是一个初学者,所以请不要把我的工作推到这么远:) 我正在尝试读取 CSV 文件,然后查看它是否与某些命令匹配。 CSV 中的某些数据有一个句点,我认为当我尝试拆分它时它会搞砸。当我尝试转储我的数组以查看其中的内容时,它总是在这段时间之后被切断。这是一个数据样本。任何帮助,将不胜感激。同样,我是初学者,因此将不胜感激。

样本数据

创建,先生。琼斯,呜呜呜
创造,夫人。史密斯,废话,废话
public class TestHarness {
        public static void main(String[] args) throws IOException, FileNotFoundException {

    Scanner input = new Scanner(new File("C:\\Users\\Ree\\Desktop\\SPR\\commands.txt"));

    String[] validCommands = { "create", "move", "useWeapon", "search", "heal" };

    boolean proceed = false;

    while (!proceed)
    {
       for (int i = 0; i < validCommands.length; i++)
       {
          String line = input.next();
          String[] nline = line.split (",");

          if (nline[0].equals("create")) 
          {
            String soldierName = nline[1];
            String soldierType = nline[2];
            String weapon = nline[3];
            Soldier aSoldier = new Soldier(soldierName,weapon);

            System.out.println("Command: "+ nline[0] +","+ soldierName +","+ soldierType+","+ weapon);

            if (soldierType.equals("enlisted"))
            {
                Soldier s = new Enlisted(soldierName,weapon);
                System.out.println(s.toString());
            }
            else if (soldierType.equals("officer"))
            {
                Soldier s = new Officer(soldierName,weapon);
                System.out.println(s.toString());
            }
          }

          else if (nline[0].equals("useWeapon")) {
              System.out.print("weapon " + nline[0] + "\n");
          }
          else if (nline[0].equals("move")) {
              System.out.print("move " + nline[0] + "\n");
          }
          else if (nline[0].equals("search")) {
              System.out.print("search " + nline[0] + "\n");
          }
          else if (nline[0].equals("heal")) {
              System.out.print("heal " + nline[0] + "\n");
          }
        }
    }
}

}

【问题讨论】:

    标签: java csv split


    【解决方案1】:

    调用Scanner.next 只会返回下一个单词(由空格分隔)。

    您需要调用nextLine 一次读取整行。

    【讨论】:

      【解决方案2】:

      有几个可用于 Java 的开源 CSV 解析器:

      【讨论】:

        【解决方案3】:

        这是一个相当快的错误,不是吗?

        这不是您的问题的答案,而是使用哈希的建议。 先定义一个接口

        public interface InputDance
        {
          public void dance(String[] input);
        }
        

        我建议你的主要例程应该是

        public static void main(String[] args)
          throws IOException, FileNotFoundException{
        
          Scanner input = new Scanner(
          new File("C:\\Users\\Ree\\Desktop\\SPR\\commands.txt"));
        
          String line = input.nextLine();
          String[] nline = line.split (",");
          InputDance inputxn = inputActions.get(nline[0]);
          if (inputxn!=null)
            inputxn.dance(nline);
        }
        

        您将使用散列来存储接口 InputDance 列出的所有操作。

        这样您的输入阅读程序将被简化为

        • 从动作哈希中检索动作 使用输入的 word0 作为键。
        • 执行该操作

        如果你只有五种士兵,你可以把所有的逻辑放在一个例程中。 但是,对于超过 10 种类型的人员,将动作放在常规之外会更干净。

        如果您正在编写电脑游戏,或将人员记录保存在军事数据库中,您会经常遇到增强请求以包括新的人员类型或规则的例外情况。然后你的 if-then-else-if 链会变得越来越长和混乱。尤其是当士兵对不同的曲调有特殊要求时。或者当您的游戏画布或人员数据库需要包含非战斗单位时。但是,当然,每次有新的人员类型时,您仍然需要更新主类中的哈希。

        请注意,在我的建议中,您的所有例程都会执行 dance(String[]) 方法。任何复杂情况都将由实现舞蹈的各个班级处理。

        接下来定义一个实现类

        public class SoldierDance
          implements InputDance
        {
          public void dance(String[] nline){
            String soldierName = nline[1];
            String soldierType = nline[2];
            String weapon = nline[3];
        
            System.out.println(
              "Command: "+ nline[0] +","+ soldierName +","+ soldierType+","+ weapon);
        
            Soldier s;
            if (soldierType.equals("enlisted")){
              s = new Enlisted(soldierName,weapon);
            }
            else if (soldierType.equals("officer")){
              s = new Officer(soldierName,weapon);
            }
            else{
              s = new Soldier(soldierName,weapon);
            }
        
            System.out.println(s.toString());
          }
        }
        

        然后定义你的主类。请注意,哈希是一个静态实例。

        此外,还有一个占位符舞蹈,所以当你有一个新的人员类型,但你还不知道如何处理它时,你只需将新的人员类型散列到这个占位符舞蹈中。

        注意,例如在“useWeapon”哈希键中,接口也可以匿名实现

        public class TestHarness
        {
          static public class PlaceHolderDance
            implements InputDance
          {
            public void dance(String[] nline){
              System.out.print("Action=" + nline[0] + "\n");
            }
          }
        
          static public Hashtable<String, InputDance> inputActions;
        
          // A static enclosure is to execute all the class definition once.
          static {
            inputActions = new Hashtable<String, InputDance>();
        
            InputDance placeHolderAction = new PlaceHolderDance();
        
            inputActions.put("create", new SoldierDance());
            inputActions.put("move", placeHolderAction);
            inputActions.put("search", placeHolderAction);
            inputActions.put("heal", placeHolderAction);
        
            // Can also anonymously implement an interface
            inputActions.put("useWeapon",
              new InputDance(){
                public void dance(String[] nline){
                  System.out.print("weapon " + nline[0] + "\n");
                }
              }
            );
        
          }
        
          // The static main method
          public static void main(String[] args)
            throws IOException, FileNotFoundException{
          Scanner input = new Scanner(
            new File("C:\\Users\\Ree\\Desktop\\SPR\\commands.txt"));
        
              String line = input.nextLine();
              String[] nline = line.split (",");
              InputDance inputxn = inputActions.get(nline[0]);
              if (inputxn!=null)
                inputxn.dance(nline);
            }
        }
        

        而且,如果士兵类与其 inputdance 之间存在一一对应关系,您甚至可以在士兵类中实现 InputDance 并提供 dance 方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-06
          • 1970-01-01
          • 2011-04-14
          • 2011-02-20
          • 2016-11-13
          • 2021-11-04
          • 1970-01-01
          相关资源
          最近更新 更多