• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DOS Vs. Bash

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've gotten a few nitpicks related to use of additional carriage returns in my formatted ouput. For example, in Java 4a, I put in the following line of code:

...and got the following comment:

I'm thinking this println()isn't really necessary.


However, when I took that link out, my output looked like this:
[tom@localhost 4a]$ java Say 20
tweny[tom@localhost 4a]$
If I run a test program that runs Say for every postive number that less than 100, my ouput looks something like this:
...
sixteen
seventeen
eighteen
nineteen
twentytwenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirtythirty-one
thirty-two
...etc.
I'm assuming that my ouput wouldn't look this way if I was running my program in a DOS prompt. However, I'm using a bash shell in Linux. Having said all of this, here's my questions:
1. Is there a way to code around this so that no matter what shell I'm using, my printed ouput would look the same?
2. Should every program be tested using the DOS shell? I'm able to do so (using Wine I think); I'm just curious.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was your println() statement in both the if and the else blocks?

What if you do your if statement where all you do is print( number ) and then after that (outside of the if/else blocks) just println().
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the gods, I would HOPE we didn't require everyone to run in a DOS shell! What sort of backwards thinking would that be??
More than likely, the nitpicker was being vague about what the comment was referring to, wanting you to think about it and figure a better way to do things. They have a bad habit of doing that
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't actually shell-related. It's related to which Java VM you're running under. System.out.println() will terminate lines with \n in a *n*x VM and with \r\n in a Microsoft environment.
The VM is, however obligated to read both as equivalent when using readln().
If you don't include the \r in a file that will be read by a stupid Windows program (e.g. Notepad) you get one long ugly line in the editor window. Conversely, if you *do* have \r in files read by many *n*x utilities, you see it displayed as crud at the end of each line.
The rule is, if you're emitting output designed as text for the same OS, use println(). However, if you are emitting text for a different OS, explicitly print() your eol charactersnad *don't" use println(). The simple fact is, a "text" file's format differs depending on OS (note that I didn't even *mention* the Mac, where eol = '\r'!).
You may be able to override the default line-ending being output. I'm too lazy to RTFM. But since that would be global to everything your app's doing, I'd do so carefully.
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Tom said:
1. Is there a way to code around this so that no matter what shell I'm using, my printed ouput would look the same?


I think the issue is not so much the shell as the type of number being printed out, that is, whether it's composite or not. In your output above, you can see which numbers are followed by a line break and which ones aren't. (In other words, which ones were printed out with print() which ones with println().)
In your program, you use print() or println() in different places, depending on the kind of number you have to spell out.
In some cases, therefore, your output is as expected:
[tom@localhost 4a]$ java Say 20
tweny[tom@localhost 4a]$

Why that nitpick? I assumed that you added in that println() to make up for that. This seemed more like an extra formatting kind of thing, one that's not necessarily required for the assignment, hence that nitpick.
If you want to make sure you always end up with a line break, Marilyn's advice above is the way to go.

Pauline
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pauline McNamara:

If you want to make sure you always end up with a line break, Marilyn's advice above is the way to go.

Pauline


Regardless of what shell you're using or what OS you're running, the generally aceppted practice is that if you're outputting lines of text, that *each* line of text end with an eol. That is, end-of-line is a delimiter and not a seperator.
It wasn't always so, but a lot of systems ended up with problems because they had to handle the final "line" of text differently than the others, and we're hardly so tight for storage these days that the extra wartiness it makes for apps isn't worth saving a byte or 2 in a file.
 
Tom Purl
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of the help on this one everybody! I can't believe it took me so long to get my head out of my butt this time. Java 4a looks a lot better.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic