• Post Reply Bookmark Topic Watch Topic
  • New Topic

Counter giving random numbers  RSS feed

 
davey smithy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement a program which will get the students to count from 1 - 10.

The following is the code I have:

Main class:

   
Student class:

   
Computer student class:

  
Science Student:

   
Math student:

  

Student Thread:

   

The output of the current code gives:

   
MathStudent[12]-Smith:  - Count:
     MathStudent[28]-Smith:  - Count:2
   
   
   
   
     MathStudent[1]-Smith:  - Count:3
   
   
   
   
     MathStudent[20]-Smith:  - Count:4
   
     MathStudent[21]-Jack:  - Count:5
   
     MathStudent[16]-Smith:  - Count:6
   
     MathStudent[2]-Jack:  - Count:7
   
     MathStudent[36]-Smith:  - Count:8
   
     MathStudent[24]-Smith:  - Count:9
   
     MathStudent[25]-Jack:  - Count:10
   
     MathStudent[26]-Victor:  - Count:11
   
     MathStudent[27]-Mike:  - Count:12
   
     Science Student[52]-Dave:  - Count:13
     MathStudent[37]-Jack:  - Count:14
   
     MathStudent[38]-Victor:  - Count:15
   
     MathStudent[39]-Mike:  - Count: MathStudent[29]-Jack:  - Count:17
   
    1
   
     MathStudent[13]-Jack:  - Count:18
   
     MathStudent[14]-Victor:  - Count:19
   
     MathStudent[30]-Victor:  - Count:20
   
     MathStudent[32]-Smith:  - Count:21
   
     MathStudent[1]-Smith:  - Count:22
   
     MathStudent[33]-Jack:  - Count:23
   
     MathStudent[31]-Mike:  - Count:24
   
     MathStudent[15]-Mike:  - Count:25
   
     Science Student[64]-Dave:  - Count:26
   
     Science Student[65]-Oscar:  - Count:27
   
     Science Student[66]-Peter:  - Count:28
   
    16
     Computer Student[67] Philip:  - Count:29
   
   
     MathStudent[4]-Victor:  - Count:30
   
     MathStudent[17]-Jack:  - Count:31
   
     MathStudent[18]-Victor:  - Count:32
   
     MathStudent[19]-Mike:  - Count:33
   
     MathStudent[22]-Victor:  - Count:34
   
     MathStudent[8]-Smith:  - Count:35
   
     MathStudent[9]-Jack:  - Count:36
   
     MathStudent[10]-Victor:  - Count:37
   
     MathStudent[11]-Mike:  - Count:38
   
     MathStudent[23]-Mike:  - Count:39
   
     Science Student[61]-Dave:  - Count:40
   
     MathStudent[6]-Mike:  - Count:41
   
     Science Student[46]-Dave:  - Count:42
   
     Science Student[53]-Oscar:  - Count:43
   
     Computer Student[70] Shaun:  - Count:44
   
     Computer Student[73] Scott:  - Count:45
   
     Science Student[55]-Dave:  - Count:46
   
     Science Student[56]-Oscar:  - Count:47
   
     MathStudent[34]-Victor:  - Count:48
   
     MathStudent[35]-Mike:  - Count:49
   
     Science Student[41]-Dave:  - Count:50
   
     MathStudent[3]-Jack:  - Count:51
   
     Science Student[43]-Oscar:  - Count:52
   
     Science Student[44]-Peter:  - Count:53
   
     Science Student[57]-Peter:  - Count:54
   
     Science Student[49]-Dave:  - Count:55
   
     Science Student[50]-Oscar:  - Count:56
   
     Science Student[51]-Peter:  - Count:57
   
     Computer Student[97] Philip:  - Count:58
   
     Computer Student[98] Shaun:  - Count:59
   
     Computer Student[99] Scott:  - Count:60
     Science Student[54]-Peter:  - Count:61
   
     Computer Student[88] Philip:  - Count:62
   
     Computer Student[89] Shaun:  - Count:63
   
     Computer Student[90] Scott:  - Count:64
     Science Student[47]-Oscar:  - Count:65
   
     Science Student[48]-Peter:  - Count:66
   
     Computer Student[76] Philip:  - Count:67
   
     Computer Student[77] Shaun:  - Count:68
   
     Computer Student[78] Scott:  - Count:69
     Science Student[62]-Oscar:  - Count:70
   
     Science Student[63]-Peter:  - Count:71
   
     Computer Student[79] Philip:  - Count:72
   
     Computer Student[80] Shaun:  - Count:73
   
     Science Student[58]-Dave:  - Count:74
   
     Science Student[59]-Oscar:  - Count:75
   
     Science Student[60]-Peter:  - Count:76
   
     Science Student[68]-Dave:  - Count:77
   
     Computer Student[82] Philip:  - Count:78
   
     Computer Student[81] Scott:  - Count:79
     Computer Student[85] Philip:  - Count:80
   
     Computer Student[86] Shaun:  - Count:81
   
     Computer Student[94] Philip:  - Count:82
   
     MathStudent[5]-Victor:  - Count:83
   
     Computer Student[95] Shaun:  - Count:84
   
     Computer Student[87] Scott:  - Count:85
     Computer Student[83] Shaun:  - Count:86
   
     Science Student[69]-Oscar:  - Count:87
   
     Computer Student[84] Scott:  - Count:88
     Computer Student[96] Scott:  - Count:89
     MathStudent[7]-Mike:  - Count:90
   
     Science Student[71]-Peter:  - Count:91
   
     Science Student[40]-Dave:  - Count:92
   
     Science Student[42]-Oscar:  - Count:93
   
     Computer Student[72] Philip:  - Count:94
   
     Science Student[45]-Peter:  - Count:95
   
     Computer Student[91] Philip:  - Count:96
   
     Computer Student[74] Shaun:  - Count:97
   
     Computer Student[92] Shaun:  - Count:98
   
     Computer Student[93] Scott:  - Count:99
     Computer Student[75] Scott:  - Count:100

As you can see the current output gives a randon count values and random student values which I don't understand why.

The output that I require is:

    
MathStudent[1]-Smith:  - Count:1
     MathStudent[2]-Jack:  - Count:1
     MathStudent[3]-Victor:  - Count:1
     MathStudent[4]-Mike:  - Count:1
     Science Student[5]-Dave:  - Count:1
     Science Student[6]-Oscar:  - Count:1
     Science Student[7]-Peter:  - Count:1
     Computer Student[8] Philip:  - Count:1
     Computer Student[9] Shaun:  - Count:1
     Computer Student[10] Scott:  - Count:1
     MathStudent[1]-Smith:  - Count:2
     MathStudent[2]-Jack:  - Count:2
     MathStudent[3]-Victor:  - Count:2
     MathStudent[4]-Mike:  - Count:2
     Science Student[5]-Dave:  - Count:2
     Science Student[6]-Oscar:  - Count:2
     Science Student[7]-Peter:  - Count:2
     Computer Student[8] Philip:  - Count:2
     Computer Student[9] Shaun:  - Count:2
     Computer Student[10] Scott:  - Count:2
     MathStudent[1]-Smith:  - Count:3
     MathStudent[2]-Jack:  - Count:3
     MathStudent[3]-Victor:  - Count:3
     MathStudent[4]-Mike:  - Count:3
     Science Student[5]-Dave:  - Count:3
     Science Student[6]-Oscar:  - Count:3
     Science Student[7]-Peter:  - Count:3
     Computer Student[8] Philip:  - Count:3
     Computer Student[9] Shaun:  - Count:3
     Computer Student[10] Scott:  - Count:3
     MathStudent[1]-Smith:  - Count:4
     MathStudent[2]-Jack:  - Count:4
     MathStudent[3]-Victor:  - Count:4
     MathStudent[4]-Mike:  - Count:4
     Science Student[5]-Dave:  - Count:4
     Science Student[6]-Oscar:  - Count:4
     Science Student[7]-Peter:  - Count:4
     Computer Student[8] Philip:  - Count:4
     Computer Student[9] Shaun:  - Count:4
     Computer Student[10] Scott:  - Count:4
     MathStudent[1]-Smith:  - Count:5
     MathStudent[2]-Jack:  - Count:5
     MathStudent[3]-Victor:  - Count:5
     MathStudent[4]-Mike:  - Count:5
     Science Student[5]-Dave:  - Count:5
     Science Student[6]-Oscar:  - Count:5
     Science Student[7]-Peter:  - Count:5
     Computer Student[8] Philip:  - Count:5
     Computer Student[9] Shaun:  - Count:5
     Computer Student[10] Scott:  - Count:5
     MathStudent[1]-Smith:  - Count:6
     MathStudent[2]-Jack:  - Count:6
     MathStudent[3]-Victor:  - Count:6
     MathStudent[4]-Mike:  - Count:6
     Science Student[5]-Dave:  - Count:6
     Science Student[6]-Oscar:  - Count:6
     Science Student[7]-Peter:  - Count:6
     Computer Student[8] Philip:  - Count:6
     Computer Student[9] Shaun:  - Count:6
     Computer Student[10] Scott:  - Count:6
     MathStudent[1]-Smith:  - Count:7
     MathStudent[2]-Jack:  - Count:7
     MathStudent[3]-Victor:  - Count:7
     MathStudent[4]-Mike:  - Count:7
     Science Student[5]-Dave:  - Count:7
     Science Student[6]-Oscar:  - Count:7
     Science Student[7]-Peter:  - Count:7
     Computer Student[8] Philip:  - Count:7
     Computer Student[9] Shaun:  - Count:7
     Computer Student[10] Scott:  - Count:7
     MathStudent[1]-Smith:  - Count:8
     MathStudent[2]-Jack:  - Count:8
     MathStudent[3]-Victor:  - Count:8
     MathStudent[4]-Mike:  - Count:8
     Science Student[5]-Dave:  - Count:8
     Science Student[6]-Oscar:  - Count:8
     Science Student[7]-Peter:  - Count:8
     Computer Student[8] Philip:  - Count:8
     Computer Student[9] Shaun:  - Count:8
     Computer Student[10] Scott:  - Count:8
     MathStudent[1]-Smith:  - Count:9
     MathStudent[2]-Jack:  - Count:9
     MathStudent[3]-Victor:  - Count:9
     MathStudent[4]-Mike:  - Count:9
     Science Student[5]-Dave:  - Count:9
     Science Student[6]-Oscar:  - Count:9
     Science Student[7]-Peter:  - Count:9
     Computer Student[8] Philip:  - Count:9
     Computer Student[9] Shaun:  - Count:9
     Computer Student[10] Scott:  - Count:9
     MathStudent[1]-Smith:  - Count:10
     MathStudent[2]-Jack:  - Count:10
     MathStudent[3]-Victor:  - Count:10
     MathStudent[4]-Mike:  - Count:10
     Science Student[5]-Dave:  - Count:10
     Science Student[6]-Oscar:  - Count:10
     Science Student[7]-Peter:  - Count:10
     Computer Student[8] Philip:  - Count:10
     Computer Student[9] Shaun:  - Count:10
     Computer Student[10] Scott:  - Count:10


Why is the output printing out the random count values and random student numbers and how I can I fix this?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a suggestion for testing:  Reduce the number of lines in the output to make debugging easier.  Have 3 students instead of 10.

Is this the same problem: https://coderanch.com/t/677269/java/objects-array-count

Also posted here:
https://www.java-forums.org/new-java/96996-getting-objects-array-count-10-a.html
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to be clearer on the requirements.

  • Do you want a count of MathStudent("Mike") instances with a separate counter for ComputerStudent("Mike")?
  • Do you want a count of all the "Mike" instances regardless of his student type?

  • Either way the best way to implement this is with a Map<String,Integer> where String is the student's name and Integer is the counter. Depending on your clarified requirements this Map would be a static in each student type class or a single static in the parent Student class.

    In the future please be more forthright about your cross postings.
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    davey smithy wrote:. . . . . .
    What does that loop do? How many times does it run? Don't use number as a variable in the loop: use st.length. Actually, st isn't a good name for a variable. In three months you can come back to this code and not know what st means.
    Avoid the finalize method. It will not reliably count instances because it won't run until garbage collection occurs. There is no straightforward way to count extant instances.
     
    davey smithy
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    davey smithy wrote:. . . . . .
    What does that loop do? How many times does it run? Don't use number as a variable in the loop: use st.length. Actually, st isn't a good name for a variable. In three months you can come back to this code and not know what st means.
    Avoid the finalize method. It will not reliably count instances because it won't run until garbage collection occurs. There is no straightforward way to count extant instances.

    I have corrected the loop to increment after each loop. But still I am getting the same problem.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try debugging the code to see what is wrong.  Stop the program at the very FIRST place where it is generating invalid output and look at the variables used at that point to see why they give the wrong results.
    There is no sense running the code past the first bad value that is output.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!