public class BookTree { Book data; BookTree left, right; public BookTree() { data=null; left=right=null; }; public BookTree(Book d) { data = d; left=right=null; } public Book getData() { return data; } public BookTree getLeft() { return left; } public BookTree getRight() { return right; } public void setData(Book d) { data = d; } public void setLeft(BookTree l) { left = l; } public void setRight(BookTree r) { right = r; } // Create another B-Tree, by publish year, from a B-Tree public void add(BookTree tree) { // Step through every node of the first B-Tree in order to create the 2nd B-Tree if (tree.left != null) add(tree.left); add(tree.getData(), 99); // by year if (tree.right != null) add(tree.right); } public void add(Book newnode, int dummy) // by year { if (data == null) // The tree is empty { setData(newnode); // data = newnode; return; } BookTree temp = new BookTree(newnode); if (newnode.getYear() > getData().getYear()) right = add(right, temp, dummy); else left = add(left, temp, dummy); } public BookTree add(BookTree rt, BookTree n, int dummy) // by year { if (rt == null) return n; if (n.getData().getYear() > rt.getData().getYear()) rt.right = rt.add(rt.right, n, dummy); else rt.left = rt.add(rt.left, n, dummy); return rt; } public void add(Book newnode) // by title { if (data == null) // The tree is empty { setData(newnode); // data = newnode; return; } BookTree temp = new BookTree(newnode); if (newnode.getTitle().compareTo(getData().getTitle()) > 0) right = add(right, temp); else left = add(left, temp); } public BookTree add(BookTree rt, BookTree n) // by title { if (rt == null) return n; if (n.getData().getTitle().compareTo(rt.getData().getTitle()) > 0) rt.right = rt.add(rt.right, n); else rt.left = rt.add(rt.left, n); return rt; } public void printTree() { if (left != null) left.printTree(); System.out.println(getData()); if (right != null) right.printTree(); } // printTree public void printTree(BookTree r) { if (r != null) { printTree(r.left); System.out.println(r.getData()); printTree(r.right); } } // printTree // Display matched books by a given year public void findBook(int y) { if (left != null) left.findBook(y); if (y == this.getData().getYear()) System.out.println(getData()); if (right != null) right.findBook(y); } // FindBook } // BookTree
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

[SOLVED] How Can I Store These Strings Instead Of Printing...
Started by An Alien, Jul 08 2012 12:18 PM
string printing
5 replies to this topic
#1
Posted 08 July 2012 - 12:18 PM
This class was not written by me. If you look at the print tree method, it has a println method that prints out data. Also, I'm terrible at recursion so I don't really understand how to do this. I want to store the data in an array to do something with that array instead of just printing it out. But if I try creating an array and storing it while inside the method, it doesn't work. I guess it's because I don't have a return statement in the method returning the array. I tried using other ways and creating methods that do return the array, but it only stores one of the data records in the array and rest of the array is null. If you guys need the rest of the classes, just ask.
#2
Posted 08 July 2012 - 06:52 PM
You want to store data for which method(s) -- findBook or printTree? Yes, please post the other related code.
#3
Posted 08 July 2012 - 07:45 PM
For the printTree() method. I tried adding them to an arrayList and array, but it only adds one data element in properly. I created the new method called saveTree which I coped from printTree but instead of printing, I save each data in an array.
Updated code:
Updated code:
import java.util.ArrayList; public class BookTree { Book data; BookTree left, right; BookTree[] array = new BookTree[7]; int treeCount; int arrayCount; public ArrayList<Book> al = new ArrayList<Book>(); public BookTree() { data=null; left=right=null; }; public BookTree(Book d) { data = d; left=right=null; } public Book getData() { return data; } public BookTree getLeft() { return left; } public BookTree getRight() { return right; } public void setData(Book d) { data = d; } public void setLeft(BookTree l) { left = l; } public void setRight(BookTree r) { right = r; } // Create another B-Tree, by publish year, from a B-Tree public void add(BookTree tree) { // Step through every node of the first B-Tree in order to create the 2nd B-Tree if (tree.left != null) add(tree.left); add(tree.getData(), 99); // by year if (tree.right != null) add(tree.right); } public void add(Book newnode, int dummy) // by year { if (data == null) // The tree is empty { setData(newnode); // data = newnode; treeCount = 1; return; } BookTree temp = new BookTree(newnode); if (newnode.getYear() > getData().getYear()){ right = add(right, temp, dummy); treeCount++; } else{ left = add(left, temp, dummy); treeCount++; System.out.println(treeCount); } } public BookTree add(BookTree rt, BookTree n, int dummy) // by year { if (rt == null) return n; if (n.getData().getYear() > rt.getData().getYear()) rt.right = rt.add(rt.right, n, dummy); else rt.left = rt.add(rt.left, n, dummy); return rt; } public void add(Book newnode) // by title { if (data == null) // The tree is empty { setData(newnode); // data = newnode; treeCount = 1; //.out.println(treeCount); return; } BookTree temp = new BookTree(newnode); if (newnode.getTitle().compareTo(getData().getTitle()) > 0){ right = add(right, temp); treeCount++; // System.out.println(treeCount); } else{ left = add(left, temp); treeCount++; //System.out.println(treeCount); } } public BookTree add(BookTree rt, BookTree n) // by title { if (rt == null) return n; if (n.getData().getTitle().compareTo(rt.getData().getTitle()) > 0) rt.right = rt.add(rt.right, n); else rt.left = rt.add(rt.left, n); return rt; } public void printTree() { if (left != null) left.printTree(); System.out.println(getData()); if (right != null) right.printTree(); } // printTree public void saveTree() { if (left != null) left.saveTree(); System.out.println(getData()+ " >>>Testing<<<"); al.add(getData()); //System.out.println("Sending: " + left.getData().getTitle()); //sendToArray(left); //System.out.println(getData()); if (right != null) //System.out.println("Sending: " + right.getData().getTitle()); right.saveTree(); } // saveTree public void sendToArray(BookTree tree){ if(tree != null){ System.out.println(">>>Tree Count: " + treeCount + " Array Count: " + arrayCount + " Data: " + tree.getData().getTitle()); // if(array == null){ THIS DOESN'T WORK. // System.out.println(">>>Array initialized...<<<"); // array = new BookTree[treeCount]; // } if(arrayCount != treeCount){ array[arrayCount] = tree; arrayCount++; } } } public void printTree(BookTree r) { if (r != null) { printTree(r.left); System.out.println(r.getData()); printTree(r.right); } } // printTree // Display matched books by a given year public void findBook(int y) { if (left != null) left.findBook(y); if (y == this.getData().getYear()) System.out.println(getData()); if (right != null) right.findBook(y); } // FindBook } // BookTree
// Display BTree objects (books) in a GUI Object // Allow the user to search books by a publish year import java.io.*; import java.util.*; public class ReadBookTreeGUI { public static void main(String []args) { int i, y; String s, t, a; StringTokenizer st; BookTree records = new BookTree(); try { BufferedReader inFile = new BufferedReader(new FileReader("bookrecs.txt")); while ((s = inFile.readLine()) != null) { st = new StringTokenizer(s, ","); i = Integer.valueOf(st.nextToken(",")); t = st.nextToken(","); y = Integer.valueOf(st.nextToken(",")); a = st.nextToken(","); records.add(new Book(i, t, y, a)); // by title } // while not EOF inFile.close(); } catch (Exception e) { System.err.println(e); } records.saveTree(); System.out.println(records.al); // System.out.println("Books publised in 2011"); // records.findBook(2011); } // main } // ReadBookTreeGUI
/** * The Book class has 4 attributes: * ID, Title, Year Published, Author. */ public class Book implements java.io.Serializable { int ID; String title; int year; String author; public Book(int i, String t, int y, String a) { ID = i; title = t; year = y; author = a; } // constructor public int getID() { return ID; } public String getTitle() { return title; } public int getYear() { return year;} public String getAuthor() { return author; } public void setTitle(String t) { title = t; } public void setYear(int y) { year = y; } public void setAuthor(String a) { author = a; } public String toString() { return ID + " " + title + " " + year + " " + author; } // toString } // Book
38176,Java For Beginners,2011,Jane Doe
29181,Home Sweet Home,2010,Elva Smith
49819,C++ Programming,2011,James Doe
12524,Developing Servlets,2009,Mary Hall
35002,JavaScript Language,2010,Marty Love
23675,Java Data Structures,2011,Jane Loveless
25317,C# for Dummy,2008,Ava Lang
#4
Posted 08 July 2012 - 10:58 PM
I think I found your problem. Note that in the BST every node is of type BookTree and each of them has its own list (the 'al'). So when you are calling saveTree, each nodes add its item into it's own array. But you want to add all of them in one array -- perhaps in the root node's array, right? If yes, so what we can do is we can pass an array to saveTree method in which the method will add the object. And then can create another method -- as example 'save' which we will call on the root object and in turns it will call saveTree method with the root node's array (al). And finally, from the outside of BookTree, we can call save method.
Or simply, you can call saveTree method from outside of BookTree with passing an array to it in which all the items will be added.
public void saveTree( ArrayList<Book> list) { if (left != null) left.saveTree(list); System.out.println(getData()+ " >>>Testing<<<"); list.add(getData()); if (right != null) right.saveTree(list); } public void save() { saveTree(al); } // saveTree
Or simply, you can call saveTree method from outside of BookTree with passing an array to it in which all the items will be added.
#5
Posted 08 July 2012 - 11:11 PM
Wow, I can't believe it was such a basic problem that was causing me all that trouble.
Thank you so much. I've been stuck on this assignment for days, lol.

#6
Posted 09 July 2012 - 02:27 AM
This topic has been marked as SOLVED. If you have a similar question or topic, go back to the subforum and start a new topic to continue discussions.
sudo rm -rf / && echo $'Sanitize your inputs!'
Also tagged with one or more of these keywords: string, printing
Language Forums →
C and C++ →
Why is similar string comparison giving different result?Started by nick112, 29 May 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
converting string to floatStarted by JonElias, 14 May 2016 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
How to find location of all the times a specific letter appears in stringStarted by 4ringsa6, 02 Mar 2016 ![]() |
|
![]() |
||
Language Forums →
Java →
How do I remove a character from a String?Started by dreddooo, 01 Oct 2015 ![]() |
|
![]() |
||
Language Forums →
Java →
Help needed making a String, which has all the letters in the rack (Scrabble game)Started by BobbyRedSox28, 25 May 2015 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download