This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Help With Java Output Command Line Text In CMD Please?  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
I am a total newbie here and to Java as well I guess.

I am using a really good book (or at least I think it is) called 'Java In Easy Steps'. It is really well written but having just created another simple Java program as part of a lesson, I have managed to compile the code correctly, but when I run the .class file the resulting lines of text are missing spaces in exactly the same place.
The actual result is correct but it looks a bit untidy. Am I doing something wrong or am I missing something in terms of the way Java is behaving in the CMD on Windows7?. I am using Java version 8 and the JDK version 8, becuase this is the program version being referenced in another book I am using which also includes 'netbeans'.

I am a little confused and would appreciate any help in terms of possibly tidying up the text in the Java output code in CMD. I have included a screen shot so you can see what I mean.
Thanks for reading.

cliff
CMD-Example.png
[Thumbnail for CMD-Example.png]
Screen Shot Of Java Text
 
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your full code here, and UseCodeTags <--link.
Also, post text whenever possible, screen snapshots are hard to read and impossible to cut and paste from. Cut and pasted text would also need Code tags.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that you have a line that look like this...
which is different than
note the space at the end of the String.
 
Cliff Black
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realised just now that I should have included the original code, so sorry about that guys, a rookie error I guess.

I am going to try to include it here with the Code Tag as you have advised. And I hope I don't mess this up.
Thank you.

 
Cliff Black
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carey Brown,
Thank you so much for your help. I did try to get that information out of the CMD window but it doesn't let you select, copy or cut, or at least I don't think it does, and so this is why I resorted to the screen shot option.
And maybe the image could have been larger as well.
If it comes to it I guess retyping the text from CMD into the Code Tag on the post is what I will have to do if that is what you require. I guess it's worth the time if I want to get the help :-)

Thank you once again,

cliff
 
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown was right: use need spaces at the end of your String constants.  For example:
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a command line window in Windows you can cut and paste text by
Right click on window header bar and select Edit > Mark.
Highlight your text an press Enter
Now highlighted text is in scratch pad memory which you can paste into a post with Ctrl+V.
 
Cliff Black
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute & Carey,

Amazing! you guys, thank you very much for that. it all totally worked and looks great (spacing is so important). And also I never knew that about copying from the CMD despite using windows for years so I really appreciate that also. Will use that in future posts.

Thanks again.

back later :-)

cliff
 
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Another way to do ti is to use the printf() method (or format() which is very similar). More details about it in the Java™ Tutorials. There are other Java™ Tutorials sections about the same topic.
 
Cliff Black
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie,
I did come across those alternatives when I was panicking to find a solution! I might give them a look a bit later when I am more familiar with the basics in the book.

Thanks for the pointer/tutorial. :-)

cheers,
cliff
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure

Don't try printf later; try it now. I think the sooner you use it the better. If you go through the Formatter documentation, you can find about the several hundred combinations of %‑tags . . . . . . . and if you forget 99% of them within 10″, who cares?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That's a pleasure

Don't try printf later; try it now. I think the sooner you use it the better. If you go through the Formatter documentation, you can find about the several hundred combinations of %‑tags . . . . . . . and if you forget 99% of them within 10″, who cares?

I'm a bit on the fence about pushing printf() at this point in the learning curve. On the other hand many beginners go through contortions to get output formatted properly (or not at all), that a smattering of printf() might be useful sooner rather than later. Initially look at %s and %n and if they are beyond you don't worry, you can come back to it later.
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . if they are beyond you don't worry, you can come back to it later.

That is what I meant about, “if you forget 99% of them”. As long as you know where the information is, you can look it up when you need it.

We tend to forget about %n, but that might be the most important tag to learn.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A side benefit of learning a bit of printf() is that the String class has a format() method that essentially works the same way.
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . the String class has a format() method that essentially works the same way.

. . . as well as Formatter#format(), which also works the same way. I suspect all those other methods create a Formatter object and use its format() method. Now you can write text in all sorts of places with similar methods
 
Cliff Black
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok guys,
Cheers,
I really appreciate this and I know exactly what you mean in regards to popular coding practices. So I will take a look at the tutorial today, I mean how confused can I be in one sitting at the end of the day right! :-) When it clicks into place I'll try to decide which permutation to adopt for now. Thank you so much for your input.

Cheers,
Cliff.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!