• 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

Java PrintWriter class

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I have a simple question regarding the PrintWriter class. Let's say i want to print 100 integers to a txt file. The following code does the trick:



This works fine, i get a number with 100 digits. Now i want to add a simple space in between each number. I change line 9 to:



And i only get gibberish added to the text file. I notice that if i add some text instead of line spacing, i get the results i want - but for some reason it does not work when adding just a blank space. Why is that? What is going on and how do i deal with it?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gibberish? Could you be more specific? Like, show us what you're seeing in the file?
 
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, @Tim,

I just tried plugging your code into an IDE.

It gave me this output.

3 0 3 0 6 6 7 8 1 4 3 9 1 8 5 0 6 3 3 8 3 9 5 8 5 9 4 0 8 0 8 2 2 6 7 5 0 0 5 3 9 7 2 2 8 9 8 4 1 3 5 1 1 5 1 9 8 2 8 8 0 4 6 9 6 4 0 5 9 8 2 0 8 7 1 3 3 1 9 4 3 2 8 2 8 1 9 9 8 7 7 7 7 7 6 9 3 8 2 9


I think that is just what you wanted, given the change of adding the string with a space in it.

However, had you instead used code like this,



you would see more like gibberish. Could that have been the problem? The difference between double quote (a string), and single quote (a char)? Single quotes are treated as 0x20 int (or 32 decimal).

35323532383839403336354133403732383535403541374037413632403240343438393732323735413934344041403633353733333733414034404032363841383632374140343240393335353341-
363534403440334141403939393939384135403441

 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the late response. Let me give an example of what i mean. If I, in code line 8 use syntax output.print(number), i get this return: 3030667814391850633839585940808226750053972289841351151982880469640598208713319432828199877777693829

Fair enough, 100 random digits as expected. Now i change it to output.print(number + " "), and the return is: ″‰″‰‶‶‷‸‱‴″‹‱‸‵‰‶″″‸″‹‵‸‵‹‴‰‸‰‸′′‶‷‵‰‰‵″‹‷′′‸‹‸‴‱″‵‱‱‵‱‹‸′‸‸‰‴‶‹‶‴‰‵‹‸′‰‸‷‱″″‱‹‴″′‸′‸‱‹‹‸‷‷‷‷‷‶‹″‸′‹

I also tried changing double quotes to single quotes using syntax output.print(number + ' '), and got return 35323532383839403336354133403732383535403541374037413632403240343438393732323735413934344041403633353733333733414034404032363841383632374140343240393335353341363534403440334141403939393939384135403441

That is 200 digits, what is going on here? For IDE I use Eclipse if it matters...


 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weirdly enough, if I add two spaces instead of one, i get a proper output. But not with one, that returns the weird symbols in the post above. I can't understand what is going on. I tried the same code on my laptop (also Eclipse), and it produces the same results.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only thing I can think of is that your text editor interprets your text file using the a different character encoding than the one your application uses to write the file. Try setting the encoding on the print writer explicitly, and tell your text editor to interpret the text file using the same encoding. In this case, ASCII should be fine.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Gibbins wrote:Weirdly enough, if I add two spaces instead of one, i get a proper output. But not with one, that returns the weird symbols in the post above. I can't understand what is going on. I tried the same code on my laptop (also Eclipse), and it produces the same results.


Have you tried printing out the the space without the number? That might point you in the right direction...

Winston
 
L Foster
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That is 200 digits, what is going on here?



@Tim, for this specific question, please refer to my earlier post. A single-quoted space is interpreted as char, rather than String. In that case, it is simply adding your int constant to the value 0x20, and giving you larger numbers.

For the other part, I like the suggestion given, about the encoding (by @Stephan van Hulst). Another thing you can do, is to use "octal dump", or "od", to look at the contents of your Java source file, to see exactly what is being placed into those double quotes.
 
L Foster
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,
I also was just reminded of something that could easily pass under one's radar. I ran @Tim's code, which is calling "Random", and the string of digits I got looked identical to what he did, at a different time of day, on different hardware, possibly a different time zone... I even re-ran it again, and got the same results.

This is reinforcing that we mustn't take Random for granted. We need to pay attention to the seeds. Same seed of '10' gives predictable results, which is not what you want when using Random for anything other than making a lot of digits.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for the “beginning” forum. Moving.
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Tom Gibbins wrote:Weirdly enough, if I add two spaces instead of one, i get a proper output. But not with one, that returns the weird symbols in the post above. I can't understand what is going on. I tried the same code on my laptop (also Eclipse), and it produces the same results.


Have you tried printing out the the space without the number? That might point you in the right direction...

Winston



Hi, i tried that now. The return is an empty text file, with added blank spaces (can click the cursor the first 100 spaces on line 1)
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The only thing I can think of is that your text editor interprets your text file using the a different character encoding than the one your application uses to write the file. Try setting the encoding on the print writer explicitly, and tell your text editor to interpret the text file using the same encoding. In this case, ASCII should be fine.



Not sure if i did this correctly, but i changed line 3 to



I still get the same results. Cryptic language with one space, output as expected using everything else.
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Foster wrote:All,
I also was just reminded of something that could easily pass under one's radar. I ran @Tim's code, which is calling "Random", and the string of digits I got looked identical to what he did, at a different time of day, on different hardware, possibly a different time zone... I even re-ran it again, and got the same results.

This is reinforcing that we mustn't take Random for granted. We need to pay attention to the seeds. Same seed of '10' gives predictable results, which is not what you want when using Random for anything other than making a lot of digits.



You are absolutely right. Thanks for pointing that out
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Foster wrote:

That is 200 digits, what is going on here?



@Tim, for this specific question, please refer to my earlier post. A single-quoted space is interpreted as char, rather than String. In that case, it is simply adding your int constant to the value 0x20, and giving you larger numbers.

For the other part, I like the suggestion given, about the encoding (by @Stephan van Hulst). Another thing you can do, is to use "octal dump", or "od", to look at the contents of your Java source file, to see exactly what is being placed into those double quotes.



Ah yes, i see. Thanks for the clarification. I'm not sure how this "octal dumping" you mentioned work though
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried something else, instead of using a loop, i made a single line code like this:


This works fine. I also tried something else, which was very interesting and could possibly be a big hint - i changed line 7 to

putting the string before the number. Success! This works. I get complete, proper data. Maybe someone understand what this means?

This setup also works as intended:



 
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic