code is working fine, I’m just curious as to how I can prevent the char 0D from printing at the end of my text file if at all possible.
Thank you.
here’s the code if it helps. sorry if it’s mediocre, still learning 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;
}
}
}
will use OS dependant
line.separatorproperty to determine line separator sequence:It happens to be
\r(?) on your computer (OD) (from JavaDoc ofPrintWriter.println()):Just use plain
print()and attach whichever character you want:…or override the
line.separatorproperty.