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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JM11
    New Member
    • Jul 2015
    • 14

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

    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!
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    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.

    Comment

    • JM11
      New Member
      • Jul 2015
      • 14

      #3
      Code:
      PrintWriter writer = new PrintWriter("myFile.txt", "UTF-8");
      w = Workbook.getWorkbook(inputWorkbook);
      Sheet sheet = w.getSheet(0);
      for (int j = 0; j < sheet.getColumns(); j++) {
      				ArrayList current = new ArrayList(); 
      
      				for (int i = 0; i < sheet.getRows(); i++) {
      					Cell cell = sheet.getCell(j, i);
      					if(cell.getContents()!=null && cell.getContents().length()!=0){
      						if(cell.getContents().indexOf('Â')!=-1)
      						    	cell.getContents().replace('Â', ' ');
      							current.add(cell.getContents());
      						writer.print(cell.getContents()+":; ");
      					}
      					}
      
      				writer.println("");
      				listOfLists.add(current);
      			}
      
      			writer.close();
      Last edited by Rabbit; Jul 20 '15, 06:06 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Can you show us what "Workbook.getWo rkbook(inputWor kbook)" is doing (code) and what class "inputWorkb ook" 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.

        Comment

        • JM11
          New Member
          • Jul 2015
          • 14

          #5
          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:

          Code:
          public void setInputFile(String inputFile) {
          		this.inputFile = inputFile;
          	}
          
          
          //This is in a different class
          ReadExcel r = new ReadExcel();
          		  r.setInputFile("C:/Trial/TrialRead.xls");
          		  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
          Last edited by Rabbit; Jul 20 '15, 06:06 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

          Comment

          • JM11
            New Member
            • Jul 2015
            • 14

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

            Code:
            PrintWriter writer = new PrintWriter("myFile.txt");
            Instead of:
            Code:
            PrintWriter writer = new PrintWriter("myFile.txt","UTF-8");
            Thank you,
            Regards

            Comment

            • chaarmann
              Recognized Expert Contributor
              • Nov 2007
              • 785

              #7
              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.

              Comment

              Working...