gpt4 book ai didi

Java分隔符换行符

转载 作者:行者123 更新时间:2023-12-01 18:07:52 25 4
gpt4 key购买 nike

我有以下数据:

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

我尝试使用简单的扫描仪读取此内容,并将数据放入具有以下字段的LinkedList中:

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

我正在使用以下分隔符:

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

似乎当我读到String zanras时;//游戏类型它还会读取一个新行字符,而不是获取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();
}
}

扎迪麦类:

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

}

如何删除新行?

最佳答案

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

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

关于Java分隔符换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34910818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com