1

I need user input from cmd/terminal and I want it to be encoded in UTF-8. The problem is everything i tried does not seem to work including a lot of things i found on Stackoverflow.

I tried using Scanner class with System.in as input but it didn't work:

Scanner scanIn = new Scanner(System.in, "UTF-8");
String command= scanIn.nextLine();

I should be getting ČČČ, instead i'm getting ???.

I'm aware that System.in doesn't use UTF-8, and that problem might lie there, however i could use an alternative if there is any.

package utfexample;

import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;



public class UTFExample {


    public static void main(String[] args) throws UnsupportedEncodingException {

        System.out.println("Enter command: ");

        Scanner scanIn = new Scanner(new InputStreamReader(System.in, "UTF-8"));
        String command = scanIn.nextLine();


        System.out.println("command: " + command + "\n");

        if(command.equals("Č")){
            System.out.println("Test was successful!");

        }
    }


}
Pisoj61
  • 23
  • 5
  • Have you tried this answer? https://stackoverflow.com/a/40754385/530160 – Nick ODell Jan 09 '19 at 21:05
  • Unfortunately doesn't work, same result. One of the answers recommends altering the windows settings, but the code is meant to be ran on another machine, which i won't be able to alter. – Pisoj61 Jan 09 '19 at 21:17
  • 2
    Maybe a dumb question, but how do you know that the bytes you are sending to the program's standard input are, in fact, the UTF-8 representation of "ČČČ"? (I.e., I am asking, how do you know whether the problem is in your program, or external to your program?) – Solomon Slow Jan 09 '19 at 21:28
  • See https://stackoverflow.com/questions/88838/how-to-convert-strings-to-and-from-utf8-byte-arrays-in-java which describes conversion to and from UTF-8. Also see the answers to this posting https://stackoverflow.com/questions/11168276/how-do-i-convert-from-ascii-string-to-utf-8-string-in-java – Richard Chambers Jan 09 '19 at 21:32
  • Interestingly enough, I was using a debugger to, well, debug. The terminal however gives me CCC, instead of ???. Even though it still isn't what i need it to be. – Pisoj61 Jan 09 '19 at 21:40
  • How do you know that the problem is reading `Č` as `?`, and that the problem is not somewhere else like when you correctly read `Č` but then write it to a non-UTF-8 stream which turns them into `?`? – that other guy Jan 09 '19 at 21:40
  • I mean, the code uses a parser which i use on commands that are read from a file. It works while reading from a file, the same commands don't work while using user input, hence it should be an input problem. – Pisoj61 Jan 09 '19 at 21:43
  • @Pisoj61, Maybe it's time for an [sscce](http://sscce.org/). That is, a _complete program_ that you can post here, that other people can run. If it behaves badly when you run it, but it behaves well when other people run it, then that would suggest that the problem is in your environment, and not in the program itself. On the other hand, if it behaves badly when other people run it, then you will have given them enough information for them to help you figure out what is wrong with it. – Solomon Slow Jan 09 '19 at 21:52
  • I don't know if it'll do, but i edited in an example which doesn't work for me. – Pisoj61 Jan 09 '19 at 22:13

2 Answers2

0

Check your environment if it has UTF-8 Charset. If you are using some other charset then you need to configure it accordingly.

This code might help you tell what charaset configuration you are using.

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Locale;
import static java.lang.System.out;
/**
 * Demonstrate default Charset-related details.
 */
public class CharsetDemo
{
   /**
    * Supplies the default encoding without using Charset.defaultCharset()
    * and without accessing System.getProperty("file.encoding").
    *
    * @return Default encoding (default charset).
    */
   public static String getEncoding()
   {
      final byte [] bytes = {'D'};
      final InputStream inputStream = new ByteArrayInputStream(bytes);
      final InputStreamReader reader = new InputStreamReader(inputStream);
      final String encoding = reader.getEncoding();
      return encoding;
   }
   public static void main(final String[] arguments)
   {
      out.println("Default Locale:   " + Locale.getDefault());
      out.println("Default Charset:  " + Charset.defaultCharset());
      out.println("file.encoding;    " + System.getProperty("file.encoding"));
      out.println("sun.jnu.encoding: " + System.getProperty("sun.jnu.encoding"));
      out.println("Default Encoding: " + getEncoding());
   }
}
  • 4
    This answer is more of a comment than an actual answer. An actual answer would describe how to check the environment as well as what steps are needed to "configure it accordingly". – Richard Chambers Jan 09 '19 at 21:08
  • Will this work on another machine? Because the code won't be running on my machine and i won't be able to change its settings. – Pisoj61 Jan 09 '19 at 21:11
  • I added a sample code that you can run on destination machine and see if needs configuration. Additionally, set the environment variable JAVA_TOOLS_OPTIONS to -Dfile.encoding="UTF-8" to force a charset through JVM. – Chandra Sekhar Jan 09 '19 at 21:43
  • Default Locale: en_US Default Charset: UTF-8 file.encoding; UTF-8 sun.jnu.encoding: Cp1252 Default Encoding: UTF8 This is the output, which I guess is alright. – Pisoj61 Jan 09 '19 at 21:51
0

Try the following code:

package utfexample;

import java.io.Console;

public class UTFExample {

    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.err.println("No console");
            System.exit(1);
        }
        String command = console.readLine("Enter command: %n");
        System.out.format("command: %s%n", command);
        if (command.equals("Č")) {
            System.out.println("Test was successful!");
        }
    }

}

To run it in cmd execute the following command before your program:

chcp 1250

You can put it all in batch file with the following contents for convenience:

@echo off
chcp 1250 >nul
java <your_parameters_here>
user1257
  • 622
  • 1
  • 8
  • 13
  • Seems to be working, I'm getting a pass. I guess i should be using Console over Scanner then with a batch file included or something. Thanks for the answer. – Pisoj61 Jan 10 '19 at 15:44