473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

14
Hello,
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
6 5656
chaarmann
785 Expert 512MB
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
JM11
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(); 
  6.  
  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.                     }
  16.  
  17.                 writer.println("");
  18.                 listOfLists.add(current);
  19.             }
  20.  
  21.             writer.close();
Jul 20 '15 #3
chaarmann
785 Expert 512MB
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
JM11
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.     }
  4.  
  5.  
  6. //This is in a different class
  7. ReadExcel r = new ReadExcel();
  8.           r.setInputFile("C:/Trial/TrialRead.xls");
  9.           r.read();



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,
Regards
Jul 20 '15 #5
JM11
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,
Regards
Jul 21 '15 #6
chaarmann
785 Expert 512MB
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

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

Similar topics

0
by: Asif Abbas | last post by:
Hello to all, I've oracle developer 6. My all reports are in character mode and its also print well in Windows 98. But when I print it from Windows NT, its print in bitmap mode instead of...
0
by: DotNetDummy | last post by:
Hi all, I am trying to set the printing setting e.g duplex mode etc. on a default printer when I.E object started printing a particular html doc. Here's the partial code, any help would be...
0
by: dynek account | last post by:
Hi! I created a form with a graph on it. I can see the graph on the screen but when I try to print it, I get a white square instead of the graph. Any idea? Thank you
4
by: Jack Russell | last post by:
Any bright ideas on how I can calculate how wide a piece of text will be when printing. When writing text to the screen in graphics mode I put it in a invisible text box to get its height and...
4
by: Water Cooler v2 | last post by:
Besides embedding <BR/>, like general purpose programming languages, I thought you could embed escape sequences such as \n or \r\n into string arguments to cause line breaks in JavaScript. To...
2
by: Rick | last post by:
Let me start by saying that I know very little about JavaScript. The software that I use to convert my FrameMaker files to HTML uses a JavaScript to hide certain text. The user has to click the...
5
by: Xah Lee | last post by:
If i have a nested list, where the atoms are unicode strings, e.g. # -*- coding: utf-8 -*- ttt=, ,...] print ttt how can i print it without getting the u'\u1234' notation? i.e. i want it...
11
by: S N | last post by:
how to print apostrophe character ' and double quote " in asp using vbscript. my code using response.write replaces " character with inverted question mark. please help
2
by: Joseph J. Kesselman | last post by:
Gabe wrote: You can do exactly that, exactly as you've shown it: <nameJohn Smith</name> But... Now you aren't talking about XML -- you're talking about the behavior of the tool you're...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.