Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleting white spaces in java code  RSS feed

 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
my method below works fine to print a matrix but when it prints every row,
it is printing extra 4 white spaces which is not required. How can I delete those
extra spaces at the end? when I use System.out.print((matrix[i][j] + " ").replaceAll("^\\s+", ""));
it trims every thing including the spaces I needed for my matrix. So where should I put replaceAll("^\\s+", "") ?
Thanks!


 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than replacing it or deleting it]]the spaces after you print them, couldn't you write an if statement to only print the spaces when you need them? Are there particular values of i or j for which you do not need the spaces printed?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think OP means four spaces at the end of every line.
This can be done using if statement as Mike suggested:
 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Thanks the spaces looks removed but can you see any difference between my out put
and program's output? it is telling me they are not identical? I wanted to post a screen shot
of my pc, is it possible here in this forum? But my option for now is to put the output inside
Code as follows:


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:This can be done using if statement as Mike suggested:

Or indeed without one (at least inside the loop):which might save a few checks - not that efficiency should be high on your priority list; but in this case I don't think it hurts readability.

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A slightly naughty suggestion:_
System.out.print("\b\b\b\b");
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
daniel kidanee wrote:Hi, Thanks the spaces looks removed but can you see any difference between my out put
and program's output? it is telling me they are not identical? I wanted to post a screen shot
of my pc, is it possible here in this forum? But my option for now is to put the output inside.


Those outputs look identical to me. And indeed, thay are identical (checked with KDiff3).
Maybe test software checks line ending characters and sees a difference?

Maybe you should use "\n" after all...
In this topic Campbell Ritchie said
Campbell Ritchie wrote:Don't use \n (despite what it says in many books) unless somebody has told you they want an LF character.

This might be the case.
Note that I am just guessing here. If there is no visible differences in output and yet the software sees the difference. So it might be what I said.

If test software runs on Windows and was written to expect "\n" you might have a problem because your program would generate "\r\n".
You should ask your teacher about it because, in my opinion, the problem is solved correctly.

If you want to post pictures on this forum click Img button and paste url of your picture in dialog.
Note that you generally should post text, not images.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For formatted printing I suggest you find out about the %d tag and similar.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!