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.