1

I wrote a program to generate a group of objects using a for-loop, and then send output to console. I have managed to make output readable. However the console output looks not so good.

Code for class "Player":

public class Player
{
    static String Name; 
    static int HP; 
    static int ATK; 
    static int playerNum; 

    public Player()
    {
        System.out.println("Empty constructor called. "); 
    }

    public Player(String Name, int HP, int ATK)
    {
        Player.Name = Name; 
        Player.HP = HP; 
        Player.ATK = ATK; 

    }

    public StringBuilder setName()
    {
        StringBuilder conc = new StringBuilder();       
        StringBuilder result = conc.append("Player ").append(Name); 
        return result; 
    }

}

Code for class "Multiplayers"

public class Multiplayers
{
    public static void createPlayer()
    {
        int playerCount; 
        int players [] = new int [2]; 
        for (playerCount = 0; playerCount<players.length; playerCount++)
        {
            Player p1 = new Player("なのは", 100, 100); 
            StringBuilder p1_n = p1.setName();
            System.out.print(p1_n+" ");
            System.out.println(playerCount+1); 
            System.err.println("ATK "+Player.ATK);
            System.out.println(Player.HP);
        }
    }
}

The main method only calls "createPlayer()" method. What I want console to display is this, which I can see it while debugging:

Player なのは 1  
ATK 100  
100  
Player なのは 2  
ATK 100  
100

What I see after running is this:

Player なのは 1  
ATK 100  
100  
ATK 100  
Player なのは 2  
100  

Since the program can run nicely and I don't see abnormal result during debugging, I have no idea why output shows in different order. .

  • Does the console display garbled characters? – Mena Feb 01 '16 at 16:53
  • 2
    Is it that one message goes to `System.err`, whereas the others go to `System.out`? – Andy Turner Feb 01 '16 at 16:54
  • 1
    @AndyTurner good eye. Although it would display in the console from an IDE, although not likely in the desired order. – Mena Feb 01 '16 at 16:55
  • 2
    Also: any variable you set in a constructor should not be `static`, e.g. `Name`, `HP`. – Andy Turner Feb 01 '16 at 16:55
  • @Mena really? I can't see a difference in the output, other than ordering. And `static` variables wouldn't make a difference between debug/non-debug. – Andy Turner Feb 01 '16 at 16:58
  • @AndyTurner nah you're right. I need me some coffee here... – Mena Feb 01 '16 at 17:02
  • Just change `.err` to `.out`. I just ran it and got `Player test 1 ATK 100 100 Player test 2 ATK 100 100` – brso05 Feb 01 '16 at 17:08
  • But as @AndyTurner pointed out you probably don't want `static` variables since you have 2 separate players... – brso05 Feb 01 '16 at 17:09
  • I change "out" to "err" simply want to distinguish both variables using color. @brso05 Thanks a lot for your advise. I will change as you mention to see what the output is, although I guess this is not the main reason. – 白崎つぐみ Feb 01 '16 at 17:22
  • @白崎つぐみ well it is the main reason for the wrong order in the console but you may face other issues down the road if you don't change the way you are creating players (using static variables)... – brso05 Feb 01 '16 at 17:28
  • And the reason I put those variables as `static` is because when I was coding, I encountered an error saying "non-static variables cannot be referenced in a static context". I may not need that `static` anymore. @brso05 @AndyTurner Thanks for your advise. – 白崎つぐみ Feb 01 '16 at 17:29
  • @brso05 Yeah, I need to change the way creating new players, otherwise I will face difficulties when adding new features. I am considering alternative ways to do so. – 白崎つぐみ Feb 01 '16 at 17:37
  • (1) Non-final reference names start in lowercase (`Name` --> `name` etc.). (2) `setName` is functioning more like `getName`. (3) The `Player` properties are `static`, this is almost surely not what you want. – user1803551 Feb 01 '16 at 22:19
  • As for the order, see [here](http://stackoverflow.com/a/4959696/1803551) and [here](http://stackoverflow.com/a/4216932/1803551) and... – user1803551 Feb 01 '16 at 22:32
  • @user1803551 Thank you for advise. I accept (1), but for (2), well, it is more like formatting the output since default System.out prints hashcode with class name and method name. For (3), I have modified the code so that those variables are not static. And thank you for your link. This is my fault to not searching site deeper. – 白崎つぐみ Feb 02 '16 at 17:36
  • (2) Then override `Player`'s `toString` method and return a readable name. Then you can just print an instance of `Player` and it will automatically print the `toString` value. (3) If you now know of a question whose answer is also the answer to your question then please mark your question as a duplicate of that one. – user1803551 Feb 02 '16 at 18:05
  • (2) I have been told that overriding toString() method is for debugging purpose only, and it is recommended to write own method to do the same. [link](http://www.javapractices.com/topic/TopicAction.do?Id=55) So I am trying to write an alternative method. (3) Well, the answer in that question is helpful, although I think mine is slightly different from that one. And, it seems that I can only flag post for moderator intervention, not "duplicate" yet. – 白崎つぐみ Feb 02 '16 at 18:51
  • "*I have been told that overriding toString() method is for debugging purpose only*" No no no and I have never seen such a recommendation. It is recommended that all classes override `toString` to give a meaningful representation (see the [Javadoc itself](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) and *Effective Java* by J. Bloch). – user1803551 Feb 02 '16 at 22:54
  • But what if other classes (not subclasses) need to override toString() again? I mean, a class want to send output to console like "name is" followed by an integer while another class want to send output to console like "value is" followed by a floating number. Should I override toString() accordingly, and they are not affected by each other? – 白崎つぐみ Feb 03 '16 at 09:44
  • Please use @username to reply to comments, otherwise the user doesn't get notified. As for `toString`, I don't see why you would think that overriding it, or any other method, in different classes has any relation between them. The only relation is that a subclass will inherit its parent's method if it does not implements one itself. – user1803551 Feb 04 '16 at 06:27
  • @user1803551 I am sorry for that missing "@". And yeah, when overriding methods, programmer in fact has created a new method inheriting parent method. Override and Inheritance are closely related. – 白崎つぐみ Feb 04 '16 at 15:27

1 Answers1

1

System.out and System.err cannot guarantee the order in which they appear on the console since they are different streams. I would suggest using only System.out for output.

aglassman
  • 2,643
  • 1
  • 17
  • 30