23

I'd like to apologize upfront for my incredible newb-ness with regard to Java and programming in general. But I've searched everywhere for an answer to this and I just can't seem to find one.

So I'm simply trying to run the following:

public class WriteSquares2
{
  public static void main(String[] args)
  {
    for (int i=1; i<=10; i++)
    {
      System.out.println(i + " \u2261 " + (i % 7) + " modulo 7");
    }
  }
}

The Unicode character \u2261 is the congruence sign (≡). DrJava shows the following as output:

1 ? 1 modulo 7
2 ? 2 modulo 7
3 ? 3 modulo 7
4 ? 4 modulo 7
5 ? 5 modulo 7
6 ? 6 modulo 7
7 ? 0 modulo 7
8 ? 1 modulo 7
9 ? 2 modulo 7
10 ? 3 modulo 7

What's weirdest about this is that when I simply type

'\u2261'

into the Interactions box, I get the equivalence character in single quotes:

'≡'

I've tried simply putting '\u2261' in the code instead of " \u2261 ", but then I get stuff like this:

8803 modulo 7
8805 modulo 7
8807 modulo 7

I also tried just simply inserting the unicode character into the code, but that just gave an error message. Can anyone figure out what's going on or what I'm doing wrong? Your help is much appreciated.

[I should add that this isn't a homework assignment or anything, as our book barely even mentions Unicode - just trying to figure this out ... and when I started, I didn't think it would be this hard!]

EDIT: I'm using Mac OS 10.7.5 and Dr Java says its build is "drjava-20120818-r5686".

EDIT #2: Here's a screenshot using Monospaced 12 as my main font. It still shows up as question marks, even though I can get the console to return me the symbol if I type it in directly, but with single quotes.

enter image description here

Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
  • It works for me without a problem when I checked in eclipse. Don't have drjava.... – Thihara May 22 '13 at 03:51
  • 2
    It **doesn't** work for me when I checked in eclipse. Also don't have drjava; you're not alone :S – Craig May 22 '13 at 03:56
  • 3
    Perhaps the font used on your system does not have a glyph for that character? – William Price May 22 '13 at 04:11
  • 1
    I think it is more related to the settings for the "terminal". For example, under Windows command prompt, if you are using a code page that do not contains the character you want, then character conversion will fail and give you a `?`. Check your code page by `chcp`. Under Unix, similarly, it is affected by the locale settings. Check the LANG (do I remember it right?) env variable to make sure that the encoding contains the char you need – Adrian Shum May 22 '13 at 04:28
  • @William I can type all the characters fine on my computer - for some reason they just won't show up in DrJava. I'd say that it's not allocating the fonts correctly or something? I'm not really a computer "geek" (in the positive sense) so I'm not very knowledgeable about much beyond the basic GUI. @ Adrian I should have added that I'm using a Mac... not quite sure if I understand your directions. Perhaps it's a Windows thing? – Chris Middleton May 22 '13 at 05:21

3 Answers3

8

The issue has to do with the font you're using, it is not a problem with your code. See DrJava's settings under Edit > Preferences > Display Options > Fonts > Main Font.

DrJava using default font (Monospaced 12) on Linux: DrJava on Linux with "Main Font" set to default (Monospaced 12)

DrJava using a different font (PT Sans 12) on Linux: DrJava on Linux with "Main Font" set to "PT Sans 12"

Both screenshots are from the same instance of DrJava; I didn't even have to shut it down. Note that in the Interactions pane, the glyph is correct for my Monospaced font and is a generic placeholder for PT Sans. The same occurs in the Console tab (not shown).

William Price
  • 4,033
  • 1
  • 35
  • 54
  • 1
    The specific monospaced font is DejaVu Sans Mono. – Mechanical snail May 22 '13 at 09:43
  • Thank you for the answer. It made sense to me and I thought it would work, but unfortunately, when I changed the font to Monospaced 12, I still got question marks in place of congruence symbols. Now I'm really not sure what's going on... – Chris Middleton May 22 '13 at 22:37
  • (I added a screenshot to my original post.) – Chris Middleton May 22 '13 at 22:47
  • My example was to show that it is likely a font glyph issue. "Monospaced 12" may map to different fonts on different platforms. Since my example was from a Linux machine and you're on OS X, you may need to try other fonts that are installed on your system. – William Price Jun 06 '13 at 17:43
2

I did a bit research, and I found a solution that was found before from another question here

So in this case, it might be something similar. Not really your code, but the way DrJava handles unicode depending on your system.

" Character encoding depends on the system. Depending on your box, it may print the 16 bits of the UTF-16 encoding (which is 9794), the high 8 bits (which would be 38) or, as in your case, the low 8 bits (which is 66).

9794 / 256 = 38
9794 % 256 = 66

"

Community
  • 1
  • 1
0

Have you changed your prompt to output in unicode.(note Java default in the english speaking world is IS0-8859-1).

  • I'm not sure what you mean. By prompt, do you mean in the Dr Java console window? I've looked in all the menus and found no mention of Unicode. It seems like such a simple thing... – Chris Middleton May 22 '13 at 05:24