• Post Reply Bookmark Topic Watch Topic
  • New Topic

computer printing ? instead of the correct string value  RSS feed

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm revisiting an old assignment and adding a few additional features.  I'm working on a blackjack game and one part that worked in the previous version isn't working now. 

The specific section of code is:


It prints the card value correctly put prints a '?' instead of the suit.  the full source code for the version that prints correctly and the one that does not can be found in my github repository.  The original blackjack is the java object oriented concepts and the current version is under BJack.

Also, is there a standard way to order the methods used in a program?  Sometimes it gets a little difficult to find methods going through the code.  I know there is a glitch in the payout following each hand, and am working on that too.  I know I can track that one down, but could really use a fresh pair of eyes on this problem.

Thanks in advance.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:It prints the card value correctly put prints a '?' instead of the suit.  the full source code for the version that prints correctly and the one that does not can be found in my github repository.  The original blackjack is the java object oriented concepts and the current version is under BJack.


Sometimes that means that the character you're trying to print doesn't have a representation in the encoding (or charset) used by System.out when writing to the console. If this has changed since some earlier time, then maybe it's because your console is now different (e.g. Windows vs Unix or something vs Eclipse, for example). I don't really want to spend the time necessary to get all of your code from github, it would be far more preferable if you could post an SSCCE which exhibits the problem, but if it's an environment thing like that then looking at the code could well be unhelpful anyway. Would it be fair to say that the characters you're using for the suits are non-ASCII characters?

Also, is there a standard way to order the methods used in a program?  Sometimes it gets a little difficult to find methods going through the code.  I know there is a glitch in the payout following each hand, and am working on that too.  I know I can track that one down, but could really use a fresh pair of eyes on this problem.


No, there aren't any widely-used standards. Many people like to order the methods in a particular way and some companies like to set up a standard for their employees to use, but if it's just you working on the code I'd recommend just using an ordering which works for you.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try converting the suit value to a hex String and printing that to see what value it has.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively use an all‑Java® solution which avoids the command line for that instruction.You can read more about this in the Java™ Tutorials, and also there confirm I have spelt all the methods right.
Please confirm that get two methods in the card class don't return numbers. I think you wouldn't get ? printed if they did return numbers.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the code from your GitHub in a command prompt window on Windows 10 and I saw question marks instead of the suit symbols.

Question: would you find a code review of your GitHub BJack project helpful?
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I ran the code from your GitHub in a command prompt window on Windows 10 and I saw question marks instead of the suit symbols.

Question: would you find a code review of your GitHub BJack project helpful?


Yes, I would find it helpful.  There is one more part of this program that I plan to add.  Rather than printing hte card value and suit, I want to print a text based representation of the card.  If you look at the card class you can see that the foundation is set to print them.  The problem I have is with the kerning on the suit.  That characer is not the same width as the other characters.  It's just a little off on the first card, but it looks like about 2 - 3 characters off by the 5th card.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:... it would be far more preferable if you could post an SSCCE which exhibits the problem, but if it's an environment thing like that then looking at the code could well be unhelpful anyway. Would it be fair to say that the characters you're using for the suits are non-ASCII characters?...


Please forgive me for asking, what do you mean by SSCCE?

The suits are defined in the Card class as follows:
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Please confirm that get two methods in the card class don't return numbers. I think you wouldn't get ? printed if they did return numbers.


The getSuit method is returning a string:



One item I meant to add yesterday but omitted, when I run the program in the debugger and examine the card it shows the correct character for the suit variable.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a word has little dots underneath it, that means it's a link
 
Paul Peterson
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:When a word has little dots underneath it, that means it's a link


Thank you.

I think this comes close to meeting the SSCCE guidelines.  It also shows the first way I made the card and you can see that the spacing is off a little on the two lines that display the value and suit:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how is it off, exactly? Check line 10 and see if that's really how many times you want to repeat the character.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If what I suggested revealed your problem with the formatting, then you could have avoided the mistake by eliminating duplication.  The value 15 that you use repeatedly when printing the back of the card is a duplicated idea. It represents the width of a card. To eliminate duplication, you should have extracted 15 to a constant:


Then in your code, you can refer to CARD_WIDTH instead of hard-coding the value all over the place.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:And how is it off, exactly? Check line 10 and see if that's really how many times you want to repeat the character.

Line 10 repeats 12 times and the others 15 because the character width of the suit is wider than the standard characters.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:Line 10 repeats 12 times and the others 15 because the character width of the suit is wider than the standard characters.

So I guess you're using a variable width font to print this out. If you use a fixed-width font like what I"m doing in my IDE, then the card will print out fine. Your reasoning still does not justify hard-coding the value 15 all over the place. That's just plain poor coding style. The console also normally uses a fixed-width font.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:... Your reasoning still does not justify hard-coding the value 15 all over the place. That's just plain poor coding style. The console also normally uses a fixed-width font.


Thank you for your comment.  The code was just a proof of concept, not production.  If you looked ad the actual source code for the project you will notice that your suggestion is how I write most of my code.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:If you looked ad the actual source code for the project you will notice that your suggestion is how I write most of my code.

I did look at your code in GitHub and what I saw is pretty much what you posted that I commented on, with the exception of the for-loop to print the lines of 15 #s for the back of the card. You still hard-coded the values of 12 and 15 though.  Likewise, in your RPS game, you have hard-coded "win", "lose" all over the place and you have set yourself up to get used to a very bad and error-prone practice later on: You are using == to compare String objects in that RPS code. In most other circumstances, comparing Strings with == rather than equals() is a mistake that manifests itself through unexpected behavior. That your code works is simply beginner's luck because of the way Java handles String constants and the way you're using hard-coded "win" and "lose" literals all over your code.

Anyway, coming back to the problem you asked about, if you use variable-width fonts, then there's no reliable way to avoid those little misalignments in the formatting. Only fixed-width fonts will give you exact alignment.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the output of your sample code, adjusted to use 15 suit marks for the topBottom string instead of just 12 as you coded it:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will give you one thing though: I found this code kind of clever even though I wouldn't do it that way myself.

I think too many people will find the above code a little suspicious and doubt its correctness at first glance though. If I had to, I would write this instead:

Yes, it's a few lines longer and it's not as clever and concise but clear and understandable trumps clever but cryptic in my book.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing about the code you have in GitHub:

The variables declared on lines 21-23 are not appropriate as instance variables. The suits variable is more appropriate as an enum type. The cardValues and cardFaces variables are probably more appropriate as static constants of the Card class.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This keeps getting stranger.  When I run it in NetBeans, the output looks like this


but if I copy it into Notepad++, it looks like



and when I compile it and run it in the command window, the output looks like this:

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, the image didn't work, here is a link to the screen shot:  https://github.com/doghouse308/SoftwareGuild/blob/7f85750bd1c90968a3e33353adaed66f6a332981/Card.PNG
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, like I said as the very first response to this thread, it all depends on the encoding used by the System.out object (and also on the font used by the target of System.out). Different environments use different encoding/font choices and so you see different things.

Somebody earlier suggested you should use a Swing component to display your card. This takes away the need to understand the debacle which ensued when Unicode encountered all of the many previous failed and/or proprietary ways of extending ASCII.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gold stars for:

  • BlackJackApp.java only launches the program
  • A test suite
  • A Console class that takes care of all the screen writing
  •  
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Peterson wrote:This keeps getting stranger.  When I run it in NetBeans, the output looks like this

    Check your configuration in NetBeans -- dollars to donuts, it's configured to use a variable-width font rather than a fixed-width font. In NetBeans, I think you can just right click on your output window and select "Choose font..." from the context menu.
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:Gold stars for:

  • BlackJackApp.java only launches the program
  • A test suite
  • A Console class that takes care of all the screen writing


  • Thank you.  I appreciate the feedback.  What areas need improvement?  I know there is a bug handling the payout and am working on that.  What other areas need work?
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Paul and Junilu,  I changed the font in the output window and that resolved the formatting issue.

    Any ideas why it is printing the ? rather than the suit ?
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Short answer: Because your console is unable to render those Unicode characters.

    Long answer: First of all, I'm going to assume your command line is a Windows command line. So go to the command line and execute the "chcp" command. (When I do that it tells me that my code page is 850.) Now google for that code page and you'll find the Wikipedia article about it. That article will tell you which 256 characters are supported, and you won't find your suit characters among them.
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:Short answer: Because your console is unable to render those Unicode characters.

    Long answer: First of all, I'm going to assume your command line is a Windows command line. So go to the command line and execute the "chcp" command. (When I do that it tells me that my code page is 850.) Now google for that code page and you'll find the Wikipedia article about it. That article will tell you which 256 characters are supported, and you won't find your suit characters among them.


    If that was the case, it would always print a ?, but it actually works in one version, and the coe is identical.  It's just in a different project.
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, if you're really interested in what's going on then we can continue. But it doesn't seem like you are. I've indirectly asked you twice now about what environment you're running the code in but I'm not getting any answers. Perhaps indirect questions aren't a good approach? Let's try the direct question: Where does the console come from for these projects?
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:Well, if you're really interested in what's going on then we can continue. But it doesn't seem like you are. I've indirectly asked you twice now about what environment you're running the code in but I'm not getting any answers. Perhaps indirect questions aren't a good approach? Let's try the direct question: Where does the console come from for these projects?


    I would like to continue this discussion and find out why this isn't working, but not tonight.  I'll return to this in the morning
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm curious too, so I'll check back tomorrow.
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, we're getting closer to a what, but not sure about a why.  I had a thought as I was drifting off to sleep last night.  First, a little background info about the environment.  I think this summarizes things pretty well:

    This came from the About menu in NetBeans

    Product Version: NetBeans IDE 8.1 (Build 201510222201)
    Updates: NetBeans IDE is updated to version NetBeans 8.1 Patch 1
    Java: 1.8.0_91; Java HotSpot(TM) 64-Bit Server VM 25.91-b14
    Runtime: Java(TM) SE Runtime Environment 1.8.0_91-b14
    System: Windows 10 version 10.0 running on amd64; Cp1252; en_US (nb)
    User directory: C:\Users\dogho\AppData\Roaming\NetBeans\8.1
    Cache directory: C:\Users\dogho\AppData\Local\NetBeans\Cache\8.1

    Active code page: 437

    When I googled the active code page, the Wikipedia article showed

    2665
    3

    2666
    4

    2663
    5

    2660
    6

    I'm assuming that the 4 digit number is the Unicode reference. Those are the same ones used in the Java code .

    Now, back to my thought last night.  I was trying to think of any differences between the code that worked and the code that did not.  Most of the code was copied from the working project into a new project.  Then I realized there may have been one difference, and I was right.  The first project was a Java Application and the second (one that displayed the ?) was created as a Maven Java Application.  To test my idea, I created a new Java Application and used the refactor command in NetBeans to copy all of the classes to the new project.  Drum roll please....yes, it worked.  but why?
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't use Google to find Unicode numbers: use the Unicode website, where you will find this PDF with suit symbols both black and white.
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I found the Unicode information in a textbook I have at home.  The Wikipedia showed that I was using the same ones associated with that page code.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lots of places have copies of such Unicode numbers.
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When you say "it worked,"  do you mean it displayed correctly in a Windows command window?
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Peterson wrote: What areas need improvement?  I know there is a bug handling the payout and am working on that.  What other areas need work?

    You have a tendency to write things like this:

    There are two things of note about this line: first, the right-hand side shouldn't be a raw type.  Add the generic type:

    Second, you should program to the interface as such as possible.

    This way you have the flexibility to change the Map implementation later.  Likewise, use the interface in your method signatures:
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:When you say "it worked,"  do you mean it displayed correctly in a Windows command window?


    It displayed correctly in the NetBeans output window, still displaying ? in the command window.
     
    Paul Peterson
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum,

    Thank you!  I really appreciate your feedback!
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Peterson wrote:. . . It displayed correctly in the NetBeans output window, still displaying ? in the command window.
    That is because NetBeans runs in Java® and doesn't have the restrictions which the Windows® command line has. I am using Linux at present and that would have no such restrictions.
    My Computer, using Bash, wrote:[critchie@campbellsComputer ~]$ printf "\\u2666"
    ♦[critchie@campbellsComputer ~]$ printf "\\u2660"
    ♠[critchie@campbellsComputer ~]$ printf "\\u2661"
    It seems to make no difference whether I write \u2661 or \\u2661.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!