1

I am using 2 classes and a bunch of methods to store something in an array then writing it to a file. After I write something to a file, instead of being of the var double, this is my code:

public void storeArray(Quest1 a, Quest2 b, String filename) throws   FileNotFoundException{

PrintWriter k = new PrintWriter(filename);
for(int i = 0; i < a.getDays(); i++)
{
    k.println(b.storeArr(a));
}

k.close();
System.out.println("Written.");
}

Quest 1 is a class, Quest 2 is a class and String filename is just getting passed through. After doing all that and putting the Quest3 object in my main menu. I run the program, input all my values etc which get put into an array in the class Quest 2 and then I write them to a file.

I open that file to check if it has worked and i get this:

     [D@264532ba

How do I fix that to get my double variables in the file?

4 Answers 4

3

Print out Arrays.toString instead of just print out the array (which invokes its toString inherited from Object):

k.println(Arrays.toString(b.storeArr(a)));

Or if you want some custom format, you can use StringUtils.join from Apache Commons. Or, perhaps just write a loop if you cannot use any dependencies.

The thing you output is the toString of the array, which is its type ([D) + @ + its hash code (264532ba).

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

4 Comments

i did that, not its printing in 2 dimensions, i dont have 2 dimensions
It returns the value of the array joined with , and enclosed in [], but what do you mean by "2 dimensions"?
i thought that was 2 dimensions, sorry. but i understand now. why exactly does it print it enclosed [] with that?
That's how it works, I can't say why: docs.oracle.com/javase/6/docs/api/java/util/… All the others are working in the same way.
1

Arrays use the default implementation of toString() and that's why the output of the array is:

[D@264532ba

You have two options to print an array's content:

  • Iterate over each element.
  • Use Arrays.toString(array).

Explanation of the weird output

Let me explain the following code:

double[] array = new double[10];
System.out.println(array);

array is an object, hence you are calling println(Object) of PrintStream (System.out), which calls toString() on the passed object internally. The array's toString() is similar to Object's toString():

getClass().getName() + "@" + Integer.toHexString(hashCode());

So the output would be something like:

[D@756a7c99

where [ represnts the depth of the array, and D refers to double. 756a7c99 is the value returned from hashCode() as a hex number.

Read also Class.getName() JavaDoc.

Comments

0

if storeArr returns an Array, you could use Arrays.toString(b.storeArr(a))

Comments

0

This here:

k.println(b.storeArr(a));

You are printing b.storeArr(a) which is an array object.

Just do this:

PrintWriter k = new PrintWriter(filename);
for(int i = 0; i < a.getDays(); i++)
{
    b.storeArr(a);
    k.println(Arrays.toString(b));
}

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.