By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

Printing the character Instead of a Space in a String (When Printing into a Text F

P: 14
I am trying to print into a text file a String value. However this String contains empty spaces that are being replaced by the character ''. Any suggestions on how to fix this issue?
Thank you for your help!
Jul 20 '15 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 785
Please show us the code you have written that prints into a text file. I suspect line-end or characterset (UTF8) problems. But that is all guessing without seeing your code.
Jul 20 '15 #2

P: 14
Expand|Select|Wrap|Line Numbers
  1. PrintWriter writer = new PrintWriter("myFile.txt", "UTF-8");
  2. w = Workbook.getWorkbook(inputWorkbook);
  3. Sheet sheet = w.getSheet(0);
  4. for (int j = 0; j < sheet.getColumns(); j++) {
  5.                 ArrayList current = new ArrayList(); 
  7.                 for (int i = 0; i < sheet.getRows(); i++) {
  8.                     Cell cell = sheet.getCell(j, i);
  9.                     if(cell.getContents()!=null && cell.getContents().length()!=0){
  10.                         if(cell.getContents().indexOf('')!=-1)
  11.                                 cell.getContents().replace('', ' ');
  12.                             current.add(cell.getContents());
  13.                         writer.print(cell.getContents()+":; ");
  14.                     }
  15.                     }
  17.                 writer.println("");
  18.                 listOfLists.add(current);
  19.             }
  21.             writer.close();
Jul 20 '15 #3

Expert 100+
P: 785
Can you show us what "Workbook.getWorkbook(inputWorkbook)" is doing (code) and what class "inputWorkbook" is? I suspect it's an Apache-POI method to read data from an Excel-sheet.
So we must also see what data the excel-sheet contains. Maybe a formula/macro excel field is read in as text field or so. Or a field that has additional info attached. So first check the type of the cell before calling getContents() and make sure it's only text. Else you try to convert binary data to text which will lead to the confusing character ''.

For debugging the process, first create an excel-sheet that only contains test text (with no internal line breaks) in its fields and no formatting, formula etc. and then see if you also get the confusing character.
Jul 20 '15 #4

P: 14
Thank you chaarmann for your reply,

I am reading from an excel sheet, manipulating the data and then writing on text files so that I use them in another class.
Below is the related code:

Expand|Select|Wrap|Line Numbers
  1. public void setInputFile(String inputFile) {
  2.         this.inputFile = inputFile;
  3.     }
  6. //This is in a different class
  7. ReadExcel r = new ReadExcel();
  8.           r.setInputFile("C:/Trial/TrialRead.xls");
  9. ;

Going back to the previous code that I posted:
In fact, when I am printing the values before adding them to the arrayList "current" and printing them to the text file "myFile", I am getting correct results (the numbers without the character '').
I tried also printing the contents of the arrayList; it is also correct. So it seems that my problem is when printing to the file.
I did the code initially on a PC which is based on an English system and I got correct results even on the file. When I imported the project into a French based PC, I got errors in the syntax of the numbers (the comma and the point differ between the two systems); I managed to fix them but I am stuck with this weird character being printed on the file and thus everywhere in the project!

Thank you,
Jul 20 '15 #5

P: 14
The problem was solved! When I removed UTF-8 from the instantiation of the printWriters, everything was working perfectly!

Expand|Select|Wrap|Line Numbers
  1. PrintWriter writer = new PrintWriter("myFile.txt");
Instead of:
Expand|Select|Wrap|Line Numbers
  1. PrintWriter writer = new PrintWriter("myFile.txt","UTF-8");
Thank you,
Jul 21 '15 #6

Expert 100+
P: 785
Thanks for telling us how the problem was solved.

So my first speculation was right: character set problem. If your text does not contain any special european characters, then writing as ASCII or UTF-8 is the same, but when it contains some, then each of these special european characters are saved as 2 bytes instead of one. And then you are reading them in again afterwards as 2 single characters.

I assumed these charaters were there BEFORE you write the first time. If I would have known that they are there only AFTER writing when you read in the output again, then I would have noticed the error immediately.
Jul 21 '15 #7

Post your reply

Sign in to post your reply or Sign up for a free account.