0

I've been working on a program which handles companies reading and writing a .xls file. Since I haven't studied yet how to make an interface for my program, the entire program is compiled and executed in cmd.

The thing I'm struggling with is that I want my program to read and write with characters from the ISO-8859-1 charset such as "ñ" or "ó", and I managed to do it in Eclipse changing the scanner format with:

public static Scanner keyboard = new Scanner(System.in, StandardCharsets.ISO_8859_1.name());

However, when I export and compile the program with Launcher4j and try to enter for example:

Córdoba

It reads and writes onto the .xls file:

C¢rdoba

Like I said, this only happens once I've exported and compiled the program; doing a test run in Eclipse will work as desired. This is because Eclipse happens to be in ISO-8859-1 format, but CMD isn't. This is what is called mojibake , and what I'm trying to do is to prevent that from happening in CMD.

Workaround solution

I've been working on it for a while and I've found a workaround solution to this problem. Basically it's a method which replaces all the characters from a String variable based on its integer value; leaving the "normal" ones intact.

Here's the code:

String original = keyboard.nextLine();
    char[] modified = new char[original.length()];
    int letter;
    for(int i = 0; i < original.length(); i++) {
        letter= original.charAt(i);
        switch(letter) {
        case 160:
            modified[i] = 'á';
            break;
        case 8218:
            modified[i] = 'é';
            break;
        case 161:
            modified[i] = 'í';
            break;
        case 162:
            modified[i] = 'ó';
            break;
        case 163:
            modified[i] = 'ú';
            break;
        case 164:
            modified[i] = 'ñ';
            break;
        default:
            modified[i] = (char)letter;
            break;
        }
    }
    original = new String(modified);
    return original;

All of this happens because the CMD does not know how to deal with these special characters and writes it down wrong. What I mean is that the CMD is able to print those characters well as long as they are stored as a String; the problem comes when it has to write those characters. So what this method basically does is to help the CMD writing those characters.

However, this method works in my program because I don't want any of the "substitute" characters. Since my program deals with companies' name, it doesn't make sense to store '?' in those names. If your program has to deal with those characters, this method won't be useful in your situation. Like I said, this is a workaround solution; not a real solution.

7
  • When you say "work with ISO-8859-1" what do you mean? Because what you're doing is using the wrong encoding to read input. It works in Eclipse because the encoding for that terminal happens to be ISO-8859-1. If the encoding is different, you have various chances at getting mojibake depending on the actual encoding and the encoding you think you're getting (in your case you think you're always getting ISO-8859-1 which isn't true).
    – Kayaman
    Sep 9, 2020 at 10:02
  • I meant that my program needs to read and write strings with characters from the ISO-8859-1 charset, such as "ñ" or "ó". I didn't know anything about this mojibake, but you're right. What I want to do is to avoid this "mojibake" in cmd. I'll edit the question to talk about the mojibake, now that I know what it's actually hapenning.
    – topocart
    Sep 9, 2020 at 10:51
  • Print out System.getProperty("file.encoding"), and show how you write to the excel file. Sep 9, 2020 at 11:07
  • Characters such as ñ and ó exist in multiple charsets and encodings. It's a difficult issue that seems simple, making it a tripping point for beginners and non-beginners alike.
    – Kayaman
    Sep 9, 2020 at 11:51
  • @MarkRotteveel it doesn't print anything at all, it just skips it or something.
    – topocart
    Sep 10, 2020 at 7:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.