Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] How Can I Store These Strings Instead Of Printing...

string printing

  • This topic is locked This topic is locked
5 replies to this topic

#1 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

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.

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

  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

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.
  • 0

#3 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

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:
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


  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

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.


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.
  • 1

#5 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

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. :blushing: Thank you so much. I've been stuck on this assignment for days, lol.
  • 0

#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

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.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download