【问题标题】:How to find and separate string values within text files using java?java - 如何使用java在文本文件中查找和分隔字符串值?
【发布时间】:2014-03-17 02:47:06
【问题描述】:

对于这个 java 程序,我必须读入三个字符串值;来自文本文件的 SSN、姓氏和名字,以创建 1000 个节点。然后,稍后创建一个跳过列表。但是,我很难(或忘记)如何读取 SSN、姓氏和名字字符串变量的这些值。文本文件是这样的,我意识到字符中断将是';'然后按到下一行。

510421600;雪莱;摩根

790701850;霍尔顿;何塞

932371897;海因斯;娜奥米

714797789;昆克尔;迪伦

878566780;格里沙姆;艾莉

到目前为止,这是我的 Node 类的内容。

package list;

import java.io.File;

/** * Represents the nodes and list of SSNs * @author Adam Taylor * @version 1.1 */

public class Node
{
    /**
     * The last name of the person
     */
    public String lastName;

    /**
     * The first name of the person
     */
    public String firstName;

    /**
     * The SSN of the person
     */
    public String SSN;

    /**
     * The head of the node
     */
    public Node head;

    /**
     * The tail of the node
     */
    public Node tail;

    /**
     * The node pointers
     */
    public Node Link1, Link2, Link3, Link4;

    /**
     * The NodeObject
     */
    public Node next;

    public Node(int i, int j, int k)
    {
        File file = new File("Database.txt");

    }

    public String getSSN()
    {
        return "";
    }

    public String toString()
    {
        return "SSN:" + SSN + " Last Name: " + lastName + " First Name: " + firstName;
    }
}

【问题讨论】:

    标签: java string file linked-list


    【解决方案1】:

    您可以使用 String 类的 .split() 方法。它根据作为参数传递的正则表达式创建一个字符串数组。

    在“;”处拆分以及您可以使用的新行:

    stringVar.split(";|\n");
    

    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html?is-external=true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多