Ich habe versucht, eine Datei.dat zu lesen, die viele Befehle enthält, die das Zeichen am Anfang jeder Zeile sind. Aber irgendwie scheint der Code, den ich geschrieben habe, einen Teil des Zeichens am Anfang einer Zeile zu überspringen oder nicht zu erkennen.
Dies ist die Datei (Original):
p 10 1 100
p 23 1 50
p 12 2 275
d 1
s 1
p 14 2 1050
d 3
x 4
p 37 2 25
p 41 1 500
d 2
s 2
q
Das ist das Ergebnis:
got p!
x, y, z of p command are: 10, 1, 100
got p!
x, y, z of p command are: 23, 1, 50
got p!
x, y, z of p command are: 12, 2, 275
command not found
got p!
x, y, z of p command are: 14, 2, 1050
command not found
got p!
x, y, z of p command are: 37, 2, 25
got p!
x, y, z of p command are: 41, 1, 500
got s
x of s command is: 2
java.util.NoSuchElementException
Process finished with exit code 0
Die Ergebnisse würden jedoch einen weiteren Fehler verursachen, wenn ich versuchte, die Datei ein wenig zu bearbeiten (tauschen Sie einfach 2 Zeilen aus).
Hier ist die bearbeitete Datei:
d 3
p 10 1 100
p 23 1 50
p 12 2 275
s 1
d 1
p 14 2 1050
x 4
p 37 2 25
p 41 1 500
d 2
s 2
q
Das Ergebnis der bearbeiteten Datei:
command not found
command not found
command not found
got s
x of s command is: 1
command not found
command not found
got p!
x, y, z of p command are: 37, 2, 25
got p!
x, y, z of p command are: 41, 1, 500
got s
x of s command is: 2
java.util.NoSuchElementException
Process finished with exit code 0
Und das ist der Code, den ich geschrieben habe:
import java.io.File;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class test3 {
public static void main(String[] args) throws Exception {
File file = new File("file.dat");
try {
Scanner input = new Scanner(file);
while (input.hasNext()) {
if ("p".equals(input.next())) {
System.out.println("got p!");
String x = input.next();
String y = input.next();
String z = input.next();
//I put this print and variables x,y,z here for debug but also to implemented it later on
System.out.println("x, y, z of p command are: " + x + ", " + y + ", " + z);
} else if ("d".equals(input.next())) {
System.out.println("got d!");
String x = input.next();
System.out.println("x of d command: " + x);
} else if ("s".equals(input.next())) { //This is the show status operation from 's' command
System.out.println("got s");
String x = input.next();
System.out.println("x of s command is: " + x);
} else if ("q".equals(input.next())) {
System.out.println("got q");
} else {
System.out.println("command not found");
}
}
} catch (NoSuchElementException e) {
System.out.print(e);
}
}
}
Ich bin dankbar für alle Lösungen, die von Ihnen allen kommen werden
Sie überspringen Teile der Datei, indem Sie aufrufen input.next()
. Speichern Sie das Ergebnis von input.next()
in einer Variablen und verwenden Sie diese Variable dann in Ihrem if elses. Sobald Sie wissen, mit welchem Befehl Sie es zu tun haben, können Sie so input.next()
viele Eingaben aufrufen, wie Sie für den angegebenen Befehl erwarten.
Diese Lösung hat immer noch ein Problem, wenn die Datei nicht richtig formatiert ist. Wenn der Befehl nicht die erwartete Anzahl von Zeichen enthält, wird die Datei falsch analysiert. Sie sollten die Zeilen zu Beginn Ihrer Bedingungen in einer Variablen speichern .nextLine()
und die Zeilenvariable von dort aus analysieren, um Ihre Befehle abzurufen und dieses Problem zu vermeiden.
Hier habe ich den Code für den ersten Befehl festgelegt:
import java.io.File;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class test3 {
public static void main(String[] args) throws Exception {
File file = new File("file.dat");
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
string currentLine = input.nextLine();
string[] currentLineArray = currentLine.split(" ");
if ("p".equals(currentLineArray[0])) {
System.out.println("got p!");
if(currentLineArray.length == 4){
String x = currentLineArray[1];
String y = currentLineArray[2];
String z = currentLineArray[3];
System.out.println("x, y, z of p command are: " + x + ", " + y + ", " + z);
} else {
System.out.println("Incorrect number of arugments for command p!")
}
}
}
}
}
}
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen