代码工作正常,我只是想知道如果可能的话,如何防止 char 0D 在我的文本文件末尾打印。谢谢你。如果有帮助,这是代码。对不起,如果它是平庸的,还在学习java。
import java.util.*;
import java.io.*;
public class Driver{
String inputFile, input;
Scanner kb = new Scanner(System.in);
PrintWriter output = new PrintWriter(System.out);
File file;
BST tree = new BST();
Trie trie = new Trie();
public static void main(String[] args){
Driver driver = new Driver();
boolean done = false;
driver.initialDataFromFile();
while (!done){
done = driver.menu(true);
}
}
public void initialDataFromFile() {
System.out.println("Welcome to Project0");
System.out.println("What is the name of the input file?");
System.out.print("> ");
inputFile = kb.nextLine();
File file = new File(inputFile);
try{
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine()){
String in = scanner.nextLine();
tree.insert(in);
trie.insert(in);
}
System.out.println("Finished reading the file");
}
catch(FileNotFoundException e){
System.out.println("File does not exist");
System.exit(1);
}
}
public boolean menu(boolean done){
System.out.print("Please enter your command\r>");
input = kb.next();
File file = new File(inputFile);
PrintWriter output = null;// = new PrintWriter(inputFile);
if(input.toLowerCase().equals("quit")){
System.out.println("Quitting");
System.out.println("Writing updated info to " + inputFile);
try{
output = new PrintWriter(file);
// String str = "";
// str += tree.toString();
output.println(tree.toString());
output.close();
}
catch(IOException e){
System.out.print("error");}
System.out.println("Goodbye");
return true;
}
else if(input.toLowerCase().equals("insert")){
input = kb.next();
tree.insert(input);
trie.insert(input);
System.out.println();
return false;
}
else if(input.toLowerCase().equals("delete")){
input = kb.next();
tree.delete(input);
trie.delete(input);
System.out.println();
return false;
}
else if(input.toLowerCase().equals("find")){
input = kb.next();
tree.find(input);
trie.find(input);
System.out.println();
return false;
}
else if(input.toLowerCase().equals("print")){
tree.print();
trie.print();
System.out.println();
return false;
}
else{
System.out.println("Not A Valid Choice");
System.out.println();
return false;
}
}
}