aspose file tools*
The moose likes Beginning Java and the fly likes Need help with displaying code to console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Need help with displaying code to console

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54


I'm trying to display a few project and quiz scores but I can't get anything to appear on the console. The console is completely empty when I try to run the code.

I'm not sure where the problem is in the code so I'd really appreciate it if someone could look over my code and see what's wrong.


Student class:




Main class:

Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Isn't this the same question you asked here ?
It's better to continue the discussion there where you've already been given an answer.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Your getProjectScoreForID method always returns empty string.
projects array is filled so that all the values are -1 (in your constructor), and in main method you didn't put any other values in it. If you check the the getProjectScoreForID method you'll see that -1 doesn't satisfy any of those if-else if conditions you have there. That's why you get no output. If you replace the last else if with just else (e.g. all scores less than 50 are graded with F, no matter if it's 0 or 49 or other value), it should probably work.

You have the same situation in your getQuizScoreForID method.

By the way, didn't you already post something similar to this? I think I've seen this class yesterday...


The quieter you are, the more you are able to hear.
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

Yes but I wanted a new thread because I've changed and added some code to the student class.

Sorry about that
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

I've now been able to fix my code so no further help will be needed.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

kenny gill wrote:I'm not sure where the problem is in the code so I'd really appreciate it if someone could look over my code and see what's wrong.

Well first, could you please correct the indenting. As it stands it makes your code very hard to read.

However, a few things off the top of my head; none of which have anything to do with your problem:

1. You do like 'while' loops, don't you? Why not just use 'for' loops? They're generally much more compact and complete. And for the ones in your Student constructor, you could use Arrays.fill().
You might also want to think about providing a descriptive constant for your "doesn't have a score" value, eg:
public static final double NO_SCORE = -1.0;

2. The code at 111-125 is repeated verbatim at line 137.

3. Your checks at lines 87 and 98 basically do exactly the same thing, just with different arrays. How do you think you might make them more generic?
It's also worth noting that another alternative is not to do them at all and just let the method throw an ArrayIndexOutOfBoundsException. It's sometimes best not to mollycoddle your users too much.

5. You don't have to use 'array.length - 1' all the time. Have a think how you could modify your conditions to just use 'array.length'.

6. getProjectScoreForID(int ID) seems very low level for a public method. How is a client likely to know that Josh Bloch's Biology project is @ ID number 7?

7. getProjectsLength() and getQuizzesLength() are poor choices for method names, since they describe internal details (the length of the array) that a client really doesn't need to know about. How about projectsCompleted() and quizzesTaken()?

8. Again, getNextProjectsIndex() seems very "bits and bytes". Why not just have an addProjectScore() method, and hide all that index manipulation away from your clients?
Indeed, if those arrays were Lists instead, you could just use its add() method, and you wouldn't have to worry about any limits.

When you've corrected the indenting I'll have a look at your actual problem.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Stuart A. Burkett wrote:Isn't this the same question you asked here ?
It's better to continue the discussion there where you've already been given an answer.
Agree. Closing this discussion.

And please, when you correct the indentation, get rid of some of the unnecessary blank lines, which also make code harder to read.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with displaying code to console