Java delimeter新行

时间:2016-01-20 21:22:44

标签: java

我有以下数据:

Archeage/2009/mmorpg
CS:GO/2014/fps
Runescape/2007/mmorpg

我尝试使用简单的Scanner阅读此内容,并将数据放入LinkedList,其中包含以下字段:

String pavadinimas;   //game name
int isleidimoMetai;  //game release year
String zanras;      //game genre

我正在使用以下分隔符:

public static final String delim = ("/|\\n");

似乎当我阅读String zanras; //game genre时,它还会读取一个换行符,而不是mmorpg我得到

mmorpg
(new line)

主要课程:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Scanner;


public class Main {
    public static LinkedList <Zaidimai> zaid = new LinkedList <Zaidimai>();
    String paketas = "src/lt/kvk/i7/engelaitis_justinas/";
    String duomenys = "failas.txt";
    String rezultatas = "rezultatas.txt";
    File file = new File(paketas + duomenys);
    File fileRez = new File(paketas + rezultatas);
    private Scanner scanner;
    private int size = 0;
    public static final String delim = ("/|\\r|\\n");

    public static void main(String[] args) throws IOException {
        Main main = new Main();

        Zaidimai prev = new Zaidimai();
        Zaidimai temp = prev;

        prev = main.sarasoSudarymas();

        main.meniu(prev, temp);
    }

    private void meniu(Zaidimai zaid, Zaidimai temp) throws IOException {
        System.out.println("--------------------------------------------------------");
        System.out.println("MENIU:");
        System.out.println("1. Saraso pildymas pagal reiksme");
        System.out.println("2. Saraso elemento paieska");
        System.out.println("3. Saraso elemento salinimas priekyje");
        System.out.println("4. Saraso spausdinimas");
        System.out.println("--------------------------------------------------------");
        System.out.println("Pasirinkite meniu numeri: ");
        scanner = new Scanner(System.in);
        int nr = scanner.nextInt();
        switch (nr) {
        case 1:
            sarasoPildymas(zaid, temp);
            break;
        case 2:
            sarasoPaieska(zaid, temp);
            break;
        case 3:
            salinimasPriekyje(zaid, temp);
            break;
        case 4:
            sarasoSpausdinimas(zaid, temp);
            break;
        default:
            System.out.println("Tokio meniu pasirinkimo nera!");
            meniu(zaid, temp);
        }
    }

    private Zaidimai sarasoSudarymas() throws FileNotFoundException {
        scanner = new Scanner(file);
        scanner.useDelimiter(delim);
        Zaidimai prev = new Zaidimai();
        prev.pavadinimas = scanner.next();
        prev.isleidimoMetai = scanner.nextInt();
        prev.zanras = scanner.next();
        System.out.println(prev.zanras);
        prev.prev = null;
        size = 1;
        Zaidimai temp = prev;

        // dvipusio sudarymas
        while (scanner.hasNext()) {
            temp.next = new Zaidimai();
            temp.next.pavadinimas = scanner.next();
            temp.next.isleidimoMetai = scanner.nextInt();
            temp.next.zanras = scanner.next();
            temp.next.prev = temp;
            temp.next.next = prev;
            prev.prev = temp.next;
            temp = temp.next;
            size++;
        }

        return temp;
    }

    private void sarasoPildymas(Zaidimai zaid, Zaidimai temp) throws IOException {
        System.out.println("Iveskite reikiamus laukus: ");
        scanner = new Scanner(System.in);
        Zaidimai naujas = new Zaidimai();
        System.out.print("pavadinimas: ");
        naujas.pavadinimas = scanner.next();
        System.out.print("isleidimoMetai: ");
        naujas.isleidimoMetai = scanner.nextInt();
        System.out.print("zanras: ");
        naujas.zanras = scanner.next();

        System.out
                .println("naujas Zaidimai: " + naujas.pavadinimas + " " + naujas.isleidimoMetai + " " + naujas.zanras);
        pildytiDuomenis(temp, naujas, true);

        temp = zaid;

        meniu(zaid, temp);
    }

    private void pildytiDuomenis(Zaidimai temp, Zaidimai naujas, boolean rewrite) {
        PrintWriter writer;
        if (rewrite) {
            try {
                writer = new PrintWriter(new FileWriter(file, true));
                writer.println(naujas.pavadinimas + " " + naujas.isleidimoMetai + " " + naujas.zanras);
                writer.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void sarasoPaieska(Zaidimai pr, Zaidimai temp) throws IOException {
        System.out.println("Paieska ---- Iveskite zanro pavadinima: ");
        scanner = new Scanner(System.in);
        String zPav = scanner.next();
        boolean rasta = false;
        temp = pr;
        for (int i = 0; i < size; i++) {
            if (zPav == temp.pavadinimas) {
                System.out.println("Zaidimas rastas: " + temp.pavadinimas + " " + temp.isleidimoMetai + " " + temp.zanras);
                rezultatuSpausdinimas(pr, temp, true);
                rasta = true;
            }
            temp = temp.next;
        }

        if (!rasta)
            System.out.println("Zaidimas nerastas!");

        meniu(pr, temp);
    }

    private void rezultatuSpausdinimas(Zaidimai pr, Zaidimai temp, boolean rewrite){
        PrintWriter writer;
        if (rewrite) {
            try {
                writer = new PrintWriter(new FileWriter(fileRez, true));
                writer.println("Gauti saraso duomenys po asmenu paieskos: ");
                writer.println("*******************************************************************");
                writer.println(temp.pavadinimas + " " + temp.isleidimoMetai + " " + temp.zanras);
                writer.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void sarasoSpausdinimas(Zaidimai zaid, Zaidimai temp) throws IOException {

        temp = zaid;
        for (int i = 0; i < size; i++) {
            System.out.println(temp.pavadinimas + " " + temp.isleidimoMetai + " " + temp.zanras);
            temp = temp.next;
        }
        meniu(zaid, temp);
    }

    public void salinimasPriekyje(Zaidimai zaid, Zaidimai temp) throws IOException {
        temp = zaid;
        Zaidimai istrintas = new Zaidimai();
        for (int i = 0; i < size; i++) {
            if (i == size - 2) {
                istrintas = temp.next;
                temp.next = zaid;
                temp.prev.next = temp;
                temp.next.prev = temp;
                size--;
            }
            temp = temp.next;
        }
        naikintiDuomenis(istrintas);
        meniu(zaid, temp);
    }

    private void naikintiDuomenis(Zaidimai temp) throws IOException {
        String[] lines = new String[200];
        String deleteLine = temp.pavadinimas + " " + temp.isleidimoMetai + " " + temp.zanras;
        int i = 0;
        scanner = new Scanner(file);
        while (scanner.hasNext()) {
            lines[i] = scanner.next() + " " + scanner.next() + " " + scanner.nextInt();
            i++;
        }

        PrintWriter writer = new PrintWriter(file);
        for (int j = 0; j < i; j++) {
            if (lines[j].compareTo(deleteLine) != 0)
                writer.println(lines[j]);
        }
        writer.close();
    }
}

Zaidimai班:

public class Zaidimai {
    String pavadinimas;
    int isleidimoMetai;
    String zanras;
    Zaidimai next;
    Zaidimai prev;

}

如何摆脱新线?

2 个答案:

答案 0 :(得分:3)

您的数据似乎使用\r\n分隔行,因此您的分隔符不会消耗\r部分。

将分隔符更改为"/|(\r?\n|\r)"或从Java 8开始,您可以\R使用"/|\\R"

答案 1 :(得分:1)

更简单的策略是使用默认的扫描仪分隔符,并使用scanner.nextLine()后跟String.split("/");