0

One of my objects is a randomly generated number. The other object is derived from using System.nanoTime(). I need to log the time the random number was generated and then store them in an array. I created an addTo method in my storage class. I am calling it from main after I call the methods to generate random number and the time, but I keep getting an error (Storage@1948cc8c). I'm not sure if the number and time I get are even being stored.

public class Driver 
{
static MyNum number = new MyNum();
static int numStore;
static Time time = new Time();
static long timeStore;
static Storage storage = new Storage(50);

public static void main (String [] args)
{
    for (int i = 0; i < 50; i++)
    {   
        System.out.println(number.myRand());
        System.out.println(time.tellTime());
        storage.addTo(number, time);
        System.out.println(storage);
    }
  }
}



public class Storage 
{
Node[] id;
private int count;
private MyNum m;
private Node n;

public Storage(int size)
{
    id = new Node [size];
    count = 0;
}

public void addTo(MyNum number, Time time)
{
    int size = 0;
    for(count=0;count<id.length;count++)

        id[size] = new Node(number, time);

   }
}
5
  • 3
    What's your question? Commented Jan 23, 2014 at 18:52
  • Am I actually storing the number and time into my array? If not, how do I do that? I wrote the method for it, but not sure if it's working. Commented Jan 23, 2014 at 20:18
  • Well, your id[size] = new Node(..) will always store into the index 0, since you don't actually modify size anywhere. So no, your code probably doesn't work. You're not really making an effort in finding out if it works either. It's your code, you can make all sorts of checks there and see what's stored where. Commented Jan 24, 2014 at 8:02
  • I did it that way before and it worked. I don't understand what it is I am doing diff now. I modified the size to 50 (id = new Node [50]); changed the for loop to "i", and put "i" where id[size] is. Now it's giving me an "error" like I didn't override my String method for my Node class, but I did write a toString method for it..... And I have been stuck on this for days. @Kayaman Commented Jan 27, 2014 at 1:47
  • Nevermind! I fixed it :D ! And it works! What I did in the previous comment made it work! Now I have to sort it.........@Kayaman Commented Jan 27, 2014 at 2:17

1 Answer 1

3

If you don't override toString() in your Storage class, System.out.println(storage); won't display anything helpful to you. You're not getting an error (or rather an exception) here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.