Java 将inOrderTraversal方法的内容放入文件时出现问题
我一直在尝试获取它,以便该方法返回一个字符串,但它会抛出一系列错误。我需要将inOrderTraversal方法的内容获取到一个文件中,或者以某种方式存储它,以便将其写入一个文件。如果有人能帮我,我会非常感激的!谢谢Java 将inOrderTraversal方法的内容放入文件时出现问题,java,linked-list,binary-search-tree,printwriter,Java,Linked List,Binary Search Tree,Printwriter,我一直在尝试获取它,以便该方法返回一个字符串,但它会抛出一系列错误。我需要将inOrderTraversal方法的内容获取到一个文件中,或者以某种方式存储它,以便将其写入一个文件。如果有人能帮我,我会非常感激的!谢谢 public class BinaryTree { // Tree: simplest possible binary search tree private Node root; // hidden root node // inorderTrave
public class BinaryTree {
// Tree: simplest possible binary search tree
private Node root; // hidden root node
// inorderTraversal: need because root is hidden
public void inorderTraversal() {
inorderT(root);
}
// inorderT: recursive function that does the work
private void inorderT(Node node) {
if (node != null) {
inorderT(node.left);
System.out.println(node.data+" ");
//node.data = node.data+" ";
inorderT(node.right);
}
}
========================================================================================================
public static void main(String ... args) throws IOException {
System.out.println("Starting");
File file = new File("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\unsorteddict.txt");
Scanner scan = new Scanner(file);
//Creation of linked list and line variable
String fileContents = "";
LinkedList<String> linkedList = new LinkedList<>();
BinaryTree tree = new BinaryTree();
int lineNum = 0;
//Writing contents into a file
PrintWriter writer = new PrintWriter("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\sorteddict.txt", "UTF-8");
while(scan.hasNextLine()){
fileContents = scan.nextLine();
lineNum++;
int x = 0;
if (linkedList.size() == 0) {
linkedList.add(0, fileContents);
}
else {
for (int i = 0; i < linkedList.size(); i++) {
tree.insert(fileContents);
}
}
}
tree.inorderTraversal(); //NEED TO PUT THIS LINE INTO A FILE
System.out.println("Ended");
}
公共类二叉树{
//树:最简单的二叉搜索树
私有节点根;//隐藏的根节点
//inorderTraversal:需要,因为根已隐藏
OrderTraversal()中的公共无效{
顺序(根);
}
//inorderT:执行该操作的递归函数
专用无效索引项(节点){
如果(节点!=null){
顺序(node.left);
System.out.println(node.data+“”);
//node.data=node.data+“”;
inorderT(node.right);
}
}
========================================================================================================
公共静态void main(字符串…参数)引发IOException{
System.out.println(“启动”);
File File=新文件(“C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\unsortedict.txt”);
扫描仪扫描=新扫描仪(文件);
//创建链表和行变量
字符串fileContents=“”;
LinkedList LinkedList=新建LinkedList();
BinaryTree=新的BinaryTree();
int lineNum=0;
//将内容写入文件
PrintWriter writer=new PrintWriter(“C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\sortedct.txt”、“UTF-8”);
while(scan.hasNextLine()){
fileContents=scan.nextLine();
lineNum++;
int x=0;
if(linkedList.size()==0){
添加(0,文件内容);
}
否则{
对于(int i=0;i
您可以将writer
对象传递给inorderTraversal()
方法以及inorderT()
方法,并将inorder遍历节点数据写入文件中
public class BinaryTree {
// Tree: simplest possible binary search tree
private Node root; // hidden root node
// inorderTraversal: need because root is hidden
public void inorderTraversal(PrintWriter writer) {
inorderT(root, writer);
}
// inorderT: recursive function that does the work
private void inorderT(Node node, PrintWriter writer) {
if (node != null) {
inorderT(node.left, writer);
System.out.println(node.data+" ");
//node.data = node.data+" ";
writer.println(node.data+" "); // Here, write to the file.
inorderT(node.right, writer);
}
}
public static void main(String ... args) throws IOException {
System.out.println("Starting");
File file = new File("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\unsorteddict.txt");
Scanner scan = new Scanner(file);
//Creation of linked list and line variable
String fileContents = "";
LinkedList<String> linkedList = new LinkedList<>();
BinaryTree tree = new BinaryTree();
int lineNum = 0;
//Writing contents into a file
PrintWriter writer = new PrintWriter("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\sorteddict.txt", "UTF-8");
while(scan.hasNextLine()){
fileContents = scan.nextLine();
lineNum++;
int x = 0;
if (linkedList.size() == 0) {
linkedList.add(0, fileContents);
}
else {
for (int i = 0; i < linkedList.size(); i++) {
tree.insert(fileContents);
}
}
}
tree.inorderTraversal(writer); //NEED TO PUT THIS LINE INTO A FILE
System.out.println("Ended");
}
公共类二进制树{
//树:最简单的二叉搜索树
私有节点根;//隐藏的根节点
//inorderTraversal:需要,因为根已隐藏
OrderTraversal中的公共无效(PrintWriter){
inorderT(root,writer);
}
//inorderT:执行该操作的递归函数
私有void索引(节点,PrintWriter){
如果(节点!=null){
顺序(node.left,writer);
System.out.println(node.data+“”);
//node.data=node.data+“”;
writer.println(node.data+“”);//在这里,写入文件。
顺序(node.right,writer);
}
}
公共静态void main(字符串…参数)引发IOException{
System.out.println(“启动”);
File File=新文件(“C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\unsortedict.txt”);
扫描仪扫描=新扫描仪(文件);
//创建链表和行变量
字符串fileContents=“”;
LinkedList LinkedList=新建LinkedList();
BinaryTree=新的BinaryTree();
int lineNum=0;
//将内容写入文件
PrintWriter writer=new PrintWriter(“C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\sortedct.txt”、“UTF-8”);
while(scan.hasNextLine()){
fileContents=scan.nextLine();
lineNum++;
int x=0;
if(linkedList.size()==0){
添加(0,文件内容);
}
否则{
对于(int i=0;i
您遇到了什么错误?NullPointerException,但我不明白为什么仍然是这样,如果我将方法更改为返回字符串,但现在该方法无效并且可以工作。唯一的问题是它会打印到控制台,而不是我想要的文件。我的意思是-作为问题的一部分,请显示您遇到的确切错误。要了解有关如何询问有关堆栈溢出的问题,请访问:我在void方法中没有遇到错误,只有当我尝试返回一个字符串时,我才能添加该字符串?非常感谢!这非常有效!我永远感谢!!