• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing a 2d Array to String in specific format  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I wrote the following method to extrac the values of an array and put them in a String:


Now the assignment requires the String to be output in a very specific manner: {{a,b,c},{d,e,f},{g,h,i}}
I cant seem to get it exactly like that.
Any hints?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call the String array; that is very confusing. Don't call the parameter m. That is a dreadful name.
Why are you using System.out. when your method says it returns a String? Make the method return the String and do nothing else, least of all printing. You can always test it with
System.out.println(MyArrayClass.toString(myArray));
Why are you using for loops when you can use for‑each loops?
Don't use the + operator on Strings repeatedly. You won't notice the difference with a small app like yours, but when you get to more than about 10000 iterations you most certainly will. At 100000 iterations you start your program, go and make a cup of coffee and come back and see whether it has finished. Use a StringBuilder instead. Use its append method. Remember you can call append several times.
myStringBuilder.append(x).append(",");
Don't mess around with checking whether you need to add the comma. You know you have to add the comma after each number except the last. You can add the comma after every number and remove it by simply setting the “length” of the StringBuilder 1 less than it was before.

Please read the documentation for StringBuilder.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I will explore your suggestions. I will change the name of the String as well, but the parameter name is given by the assignment and cannot be changed.
I will get back when I have somehting new.
Thanks
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I got the following:


the first Junit test that tests for an empty array goes through.
The second test that test for just one element (The number 1) in the array sometimes fails and sometimes goes through. Why would it do that?
The third test fails because the String does not contain the right amount of commas.
(The format has to be exactly the following: {{a,b,c},{d,e,f},{g,h,i}})

How can I solve the comma issue?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when trying to test the method by just printing it to console I get the weird gibberish of: "{{[D@15db9742, }{[D@6d06d69c, }}" how can i display it correctly to exactly see what changes I make when correcting the code?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already told you how to solve the comma issue.
Forget about the JUnit tests for the time being. Start by going through the method you have and working out what you are trying to add to the String at which point. If that doiesn't sort things out, simplify your method so you can write this:-
System.out.println(MyArrayUtils.toString(new double[]{}1.23, 2.34, -3.45, 4.56e78, 5.67e-89, 6789.0, 789d}));
… because it is taking double[] array not double[][] array as its parameter. You can reuse that method later.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote: . . . weird gibberish of: "{{[D@15db9742, }{[D@6d06d69c, }}" . . .
That is not weird gibberish. That is because the toString method is not overridden in arrays.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I override the method in arrays?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't override it in an array. There is a toString method in the Arrays class but it uses [] rather than {}. You are going to have to write your own with a loop and the classes I have already told you about.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I used the following to fix the comma issue:
result.setLength(result.length() - 1);

seems to work ok.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got the following:


It prints out the array to string as is required.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done But I suspect there is a shorter neater way to do it, without all those if statements. And use for‑each loop rather than for loops.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have one more method to write.
The method is the opposite of the toString method I just did.
This method receives a String as input in the same format as the last method outputs, and creates a 2d Array from the String.
Do you have any tips where I could start?
I was thinking iterating through the string, is that possible?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote: . . . creates a 2d Array from the String. . . .
No, it doesn't because there is no such thing as a 2D array. There are only arrays of arrays. That definition permits “jagged” arrays, even with empty elements, which you would need to allow for when printing an array.

Hint for how to write the method: There is a piece of hardware which is getting in your way. It is called a computer. Turn it off. Write down on paper what you would do, then you can consider turning the computer back on.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello after some wrangling I got this:


It passes the test with string "{{1}}".
Yet it fails with string "{}" and it fails on string "{{ 1.0, 2.0}, {3.0, 4.0 } }".

Did I do something wrong with the replacing at the beginning of the method?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok just fixed the test with String "{{1.0, 2.0}, {3.0, 4.0}}".
Notice the string has empty spaces so I just used .replace(" ", "") to get rid of all empty spaces and it now works.

Now as for the "{}" String, Im assuming that the error could be solved by not even going into the whole replace rampage but instead use a if loop to check the empty condition and if it is "{}" just return the empty array.
Am I having the right thoughts?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok just solved the "{}" issue as well by inserting the following if condition:


(Since "{}" is supposed to represent an empty array as a string in this problem, it should always have a length of 2)
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something about replace() which I am not happy with. That method looks like something you have put together and it just about works, rather than planning it in which case it would have worked nicely.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that method works perfectly.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its the toString method that doesnt work perfectly.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have the problem with the following code:



The Method is supposed to take a double [] [] array and convert it into a String representation of the array.
My problem is that I cannot check my work because the String prints out as below.

For example for the Array {{1.0, 2.0}, {3.0, 4.0}} I get the output: {{[D@15db9742,[D@15db9742},{[D@6d06d69c,[D@6d06d69c}}

How can I get it to print out the true numerical representation of the array as follows: {{1.0, 2.0}, {3.0, 4.0}} ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "[D@15db9742" is what you get when you call toString() on a double[] (an array of double).

You're not calling toString() explicitly on a double[] in your code, but it's behind called behind the scenes in line 12 of your code, where you do this:

Note that m[i] is a double[], not a single double value.

Instead of the above, you probably meant this:

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes thank you.

Thanks Jesper that helped as well!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!