Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Letters are displaying wrong  RSS feed

 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i have bought a book "Head First Java" 2nd edition, and I liked it very much.
I written my first program as the book said, but I got this strange thing.

I think that's because of russian letters, but when i'm using english letters, I don't have this problem.


Thanks for helping!
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I edited your program to remove the long lines which make the code hard to read. I didn't correct your indentation; line 21 sh‍ould be two lines, for example. We always use English. I got a translation of your code from Google translate and it looks like the PhraseOMatic I remember from the book. Also copy‑and‑paste output because it is easier to read than a screenshot.
I tried your code and got:-
java PhraseOMatic
Всё, что вам нужно, - это 30000-футовый распределённый выход из положения
I am using Linux. I think you have a problem with the Windows terminal, which is notorious for only displaying a small range of characters. Maybe it is not set to accept Cyrillic writing. You are probably not using a computer with Russian as its default language. Try this instead of the print call in line 20:-
javax.swing.JOptionPane.showMessageDialog(null,
      "Всё, что вам нужно, - это " + phrase);

See what appears on the option pane. I think that will correct your problem
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying!
That's right, it's PhraseOMatic from the book.
I already had Russian as default language.
I tried to use your new argument for printing phrase, but it didn't resolve the problem.
I'm using J2SE Development Kit 5.0 Update 15, which is old version. If I update it to newest version, can it resolve the problem?
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to mention that program on your computer worked properly.
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rich McStone wrote:Thanks for replying!
That's a pleasure
. . . I tried to use your new argument for printing phrase, but it didn't resolve the problem.
Please show us exactly what code you used with option pane and explain what went wrong.
I'm using J2SE Development Kit 5.0 Update 15, which is old version. If I update it to newest version, can it resolve the problem?
You oughtn't to have that problem with Java5, nor with Windows® if you have Russian locale set. You may need to dig deeper to find the problem. Try the following code unchanged:-This will tell you where the numbers come from. You shou‍ld get this sort of output:-
javac RussianLetters.java
java RussianLetters
0x0430 = 'а'    0x0431 = 'б'    0x0432 = 'в'    0x0433 = 'г'    0x0434 = 'д'   
0x0435 = 'е'    0x0436 = 'ж'    0x0437 = 'з'    0x0438 = 'и'    0x0439 = 'й'   
0x043a = 'к'    0x043b = 'л'    0x043c = 'м'    0x043d = 'н'    0x043e = 'о'   
0x043f = 'п'    0x0440 = 'р'    0x0441 = 'с'    0x0442 = 'т'    0x0443 = 'у'   
0x0444 = 'ф'    0x0445 = 'х'    0x0446 = 'ц'    0x0447 = 'ч'    0x0448 = 'ш'   
0x0449 = 'щ'    0x044a = 'ъ'    0x044b = 'ы'    0x044c = 'ь'    0x044d = 'э'   
0x044e = 'ю'    0x044f = 'я'   
Now change the code to read this:-You sho‍uld get the same output. If you get anything different with either version please tell us.
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us exactly what you used...


Here's the code



That's the code, and output was exactly the same as first time with the same problem.

...explain what went wrong.


Nothing changed, same problem

You sho‍uld get the same output. If you get anything different with either version please tell us.


First program worked as it has to, without any errors

Second program didn't even compile:

B:\Users\Desktop>javac RussianLetters2.java
RussianLetters2.java:5: unclosed character literal
        for (char c = '╨░'; c <= '╤П'; c++)
                      ^
RussianLetters2.java:5: illegal character: \176
        for (char c = '╨░'; c <= '╤П'; c++)
                        ^
RussianLetters2.java:5: unclosed character literal
        for (char c = '╨░'; c <= '╤П'; c++)
                         ^
RussianLetters2.java:5: unclosed character literal
        for (char c = '╨░'; c <= '╤П'; c++)
                                 ^
RussianLetters2.java:5: unclosed character literal
        for (char c = '╨░'; c <= '╤П'; c++)
                                    ^
RussianLetters2.java:10: illegal start of expression
    }
    ^
6 errors

 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier I tried to post exactly the same text, and forum software didn't allow me, but now it's fixed.
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might be an encoding problem; please supply details of your editor and its settings including the encoding it is using.
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I must have done something wrong, I tried second program RussianLetters once again and it worked, I got same output as first RussianLetters program
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are your letters coming out correctly for the PhraseOMatic now?
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still outputing wrong letters:

B:\Users\Desktop>java PhraseOMatic
Р?С?С?, С?С?Р? Р?Р°Р? Р?С?Р?Р?Р?, - С?С?Р? Р?Р° Р?С?Р?Р?Р?Р? Р?Р?Р?-С?Р?С?Р?Р?Р?
Р?Р?РёР№ С?Р°С?РїС?Р?Р?Р?Р?С?Р?Р?С?Р№ С?РёРї С?С?С?С?РєС?С?С?С?


Did I miss something? Do I have to put something in PhraseOMatic code?
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Perhaps the Windows console is set to the wrong country? ... when you run the "chcp" command, does it give you the code for Russia?

Henry
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says:

The current code page:866

I translated it from Russian of course.
 
Knute Snortum
Sheriff
Posts: 3603
86
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like the correct code page -- or one of them.
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and what about your editor? What are you using and can you find its encoding?
 
Dave Tolls
Ranch Hand
Posts: 2554
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Java is outputting in UTF8 then you'll get this issue, as Cp866 doesn't really agree with it.
The numbers will be a complete mismatch.

Not sure how you find out what Sysyout is using (locale possibly?).
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and what about your editor? What are you using and can you find its encoding?


I'm using Notepad++ and it uses ANSI for encoding.

Sorry for not replying first time
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this SO thread about ANSI encoding, and a Wikipedia entry. It would appear that Windows 1252 may be incompatible with the code page you are using. The links also say that ANSI is an inaccurate name for the encoding. This link makes some suggestions of encodings suitable for Russian script. Also please tell us what the output from chcp is.
 
Knute Snortum
Sheriff
Posts: 3603
86
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell wrote:Also please tell us what the output from chcp is.

He did.
Rich wrote:The current code page:866
 
Dave Tolls
Ranch Hand
Posts: 2554
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, I suspect it's Java that is outputting in something other than Cp866.

Wrapping the Sys.out in another PrintStream that supplies a Cp866 encoding might work.
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java® defaults to UTF16 for String literals, but I am not sure that helps. But why is OP getting the compiler errors in this post? Why did the first lot of code not show correctly on an option pane dialogue (same post).
I knew the name Cp686 looked familiar; I didn't notice that it was part of the output from chcp. Sorry.

It doesn't say anything about Locales in System#out, but there is the possibility

RM: what happens if you use printf instead of println? Not
System.out.println("Всё, что вам нужно, - это " + phrase);
but
System.out.printf("Всё, что вам нужно, - это %s%n", phrase);
That is because printf automatically uses your default Locale. If that doesn't work, try
System.out.printf(new java.util.Locale("RU", "ru"), "Всё, что вам нужно, - это %s%n", phrase);
Have I got the right syntax for the country abbreviation?
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("Всё, что вам нужно, - это %s%n", phrase);
That is because printf automatically uses your default Locale. If that doesn't work, try
System.out.printf(new java.util.Locale("RU", "ru"), "Всё, что вам нужно, - это %s%n", phrase);


Both of them worked, now there are no problems anymore.
That's funny, solution was really simple it's just one letter "f"
But I didn't use %s%n because it caused error:
B:\Users\Desktop>java PhraseOMatic
Всё, что вам нужно, - это Exception in thread "main" java.util.MissingFormatArgu
mentException: Format specifier '%s'
        at java.util.Formatter.format(Unknown Source)
        at java.io.PrintStream.format(Unknown Source)
        at java.io.PrintStream.printf(Unknown Source)
        at PhraseOMatic.main(PhraseOMatic.java:13)


But why is OP getting the compiler errors in this post? 


First time I just put RussianLetters program into txt file, and then saved it as .java file through notepad++ program and it worked.
Second RussianLetters program I copied and pasted instead first program directly into .java file and it didn't work.
When I saved second RussianLetters program into txt file, and then saved it as .java file program worked.

Problem solved,  are we done for sure?

 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rich McStone wrote:. . . But I didn't use %s%n because it caused error:
B:\Users\Desktop>java PhraseOMatic
Всё, что вам нужно, - это Exception in thread "main" java.util.MissingFormatArgu
mentException: Format specifier '%s'
. . .
. . .
You shouldn't get such an error if you wrote the printf call correctly. There shou‍ld be one object after the ", and what I wrote shouldn't cause that Exception.

Well done sorting out the printing
 
Dave Tolls
Ranch Hand
Posts: 2554
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely that's some kind of bug?
I wouldn't expect println and printf to function quite so differently.
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried all three versions, with println, option pane, and printf and all three worked nicely. But I am using Linux, where the terminal supports more characters than a Windows® command line.
 
Dave Tolls
Ranch Hand
Posts: 2554
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't try it here as I'm on a Mac.
And I'll have forgotten by the time I get home!
 
Rich McStone
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thank you everybody very much for helping!
I really like this friendly community.

P.S. somehow, I don't have this problem with println anymore after using printf.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!