0

I'm making an encryption software, and so far everything is great. You enter your message and my software selects 26 unicode characters in order from a random spot in the unicode table and switches each letter with its cooresponding unicode character. The problem is, however, that when I paste the encoded message (unicode characters) into the console, Java spits back different characters each time.

The following code yields this as a result:

Scanner sc = new Scanner(System.in);
String msg = sc.next();
System.out.println(msg);

ⶫⶭ (entered to equal msg)

â¶«â¶­ (given back by Java in the print statement)

2
  • 2
    You are supposed to tell the Scanner what character set the characters are in. Commented Jan 3, 2016 at 23:04
  • 2
    You need to learn the difference between Unicode (the abstract concept) and its multibyte encodings, including UTF-16 and UTF-8, and other single-byte character encodings such as Windows-1252 and Latin-1. This is much too large a topic to be re-described here on SO. Commented Jan 3, 2016 at 23:04

1 Answer 1

1

You can set the encoding type that the Scanner should be reading in with

String encoding = "the encoding"; // e.g. UTF-8 etc.
Scanner sc = new Scanner(System.in, encoding);

This should fix your issue. Also, please note that when I tested your code as it was in the question on my machine, it worked properly and displayed ⶫⶭ from the print statement.

Sign up to request clarification or add additional context in comments.

1 Comment

For encoding see: StandardCharsets

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.