Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Help with toString message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with toString message" Watch "Help with toString message" New topic
Author

Help with toString message

lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I have a horrible project lol that I have been working on forever. I am so close lol. Here's my code:

Everything works the way it sould EXCEPT I've got something wrong in the last part. The public StringtoString. It's my IF statement I am sure, I know the other part is fine. What I am trying to do is get it to say your score is not acceptable if the persons score is 10 below the average or more. Any help would be appreciated. I'm really new at this. Thanks
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
The error on the if line at the bottom says unreachable code.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

You cannot unconditionally return from a method, and still have statements AFTER the "return":
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
What would be a better way of saying what I am trying to. I need it to return the string info first then say your grade is below but if I put the return message after the if statement it doesnt work either.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

At what point do you want to show the "not acceptable" message; each time after you call the toString() method I assume? You either move the "return" down so it's the last instruction in the toString() method, or you create a new method that just displays the "not acceptable" line.
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
If I change it like this:

public String toString() {
String message = lastName + "," + " " + firstName + ":\t" + score; {
if(score <= fail){
System.out.println("Your score is not acceptable!");
return message;}
}

}}

it tells me that I have multiple markers on the to String line; that it overrides the java toSring language and that this method must return the result of a string. I want it to display that special message only when the person's score is 10 marks or more lower than the class average.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Why can't you append the "not acceptable" string to the one you're returning; for instance:
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
ok I did that and here is my code again. The only problem is that it isn't displaying the message. I must be missing something?
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
This is my output and I think that Brenda and Ali should get the message:

Welcome to the Student Scores Application.

Students Before Sorting

Gator, Ali: 85.0
Vator, Ella: 75.0
Beam, James: 95.0
Class, Lastin: 55.0
Class, Brenda: 12.0

Average: 64.0

Students After Sorting

Beam, James: 95.0
Class, Brenda: 12.0
Class, Lastin: 55.0
Gator, Ali: 85.0
Vator, Ella: 75.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40


You don't have a setter for the fail variable in your Student class -- so a student can only fail if the score is less than zero.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

If you also include the value of "fail" in the message, you can see what its value is each time toString() is called. Perhaps it's not having the values you think it should have:
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I changed it like you said so I now have:

I had added the + fail on the string message and it messed up the output making the numbers 12.00.0 so I took that back out. The output still does not display the special message. here's my current output:

Welcome to the Student Scores Application.

Students Before Sorting

Gator, Ali: 85.0
Vator, Ella: 75.0
Beam, James: 95.0
Class, Lastin: 55.0
Class, Brenda: 12.0

Average: 64.0

Students After Sorting

Beam, James: 95.0
Class, Brenda: 12.0
Class, Lastin: 55.0
Gator, Ali: 85.0
Vator, Ella: 75.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

You don't actually use the setter for the fail variable in your Student class -- so a student can only fail if the score is less than zero.

Henry
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I'm sorry but I dont know what you mean and I don't know how to fix it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Lizz, can you please UseCodeTags next time? Thanks.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
ok I will try we have not been taught to do that. I still don't know what's missing in my code.
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I know that it's the math part that isnt working. I added the "fail" to my string message just to see what it would display and it displays zeros for all of them. But i'm not sure what I have wrong!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

lizz Palmer wrote:I know that it's the math part that isnt working. I added the "fail" to my string message just to see what it would display and it displays zeros for all of them. But i'm not sure what I have wrong!


Nope. It is not the math. That would imply that you are setting it incorrectly.... You are never setting the fail variable of the student class at all.

Henry
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
Isn't this setting the fail?:

public double getFail() {
return fail;

}

public void setFail(double fail) {
this.fail = fail;
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

lizz Palmer wrote:Isn't this setting the fail?:

public double getFail() {
return fail;

}

public void setFail(double fail) {
this.fail = fail;


From my previous post....

Henry Wong wrote:You don't actually use the setter for the fail variable in your Student class -- so a student can only fail if the score is less than zero.


For the fail variable to be set, you actually have to call the setter (the setFail() method that you provided) -- and you never call the setter anywhere in your program.

Henry
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
Henry, this is my very very first java class and now my last because its so unbelievably had to comprehend. I don't know what you mean when you tell me I'm not using it. It's right there?!? it would be wonderful if you could give me a hint of some sort that I might understand. I have this project, one more and then my rediculous final. They told me I didn't need to know anything to take this course and obviously that's not true lol. The instructor doesn't answer my questions and it's an online class. I have just this forum to squeek by. It's destroying my perfect GPA hence why I am changing back to my MBA degree lol. Any hints you can give me would be very very appreciated. Lizz
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

lizz Palmer wrote:Henry, this is my very very first java class and now my last because its so unbelievably had to comprehend. I don't know what you mean when you tell me I'm not using it. It's right there?!? it would be wonderful if you could give me a hint of some sort that I might understand. I have this project, one more and then my rediculous final. They told me I didn't need to know anything to take this course and obviously that's not true lol. The instructor doesn't answer my questions and it's an online class. I have just this forum to squeek by. It's destroying my perfect GPA hence why I am changing back to my MBA degree lol. Any hints you can give me would be very very appreciated. Lizz



Quit frankly. I am at a loss -- out of ideas on how to explain it..... hmmmm.... how about like this? Let's try to do a comparison to something that is working....


Here is the Student class toString() method that uses the fail variable....



But this time, let's not just look at fail, let's look at the score variable too. Notice that it uses the score variable -- to print the score, and to compare it to the fail variable.

Now, how is this score variable set? .... to see that let's look at the setters (and getters) for the score variable....



Pretty straight forward at this point. There is a setter (in the student class) to set the score variable. And up to this point, I think you are okay with it.... (note: the setters for the fail variable has also been included for comparison purposes).


So how are this setters used? To understand that let's look at the main code -- the code that does the work....




If you take a look at line 18, you will see that this is where each Student instance's score setter is called -- which of course, sets the score for that student. There is also a place where the getter is called later, which is used to calculate the total and average. Pretty straightforward usage of the Student instances.

And here is where the difference occurs...

Now, if you look at this code, you will also notice that ... nowhere in the code is the student class setter for the fail variable ever called. So, the student objects never has the fail variable ever set. Unlike the score, the fail information never gets to the student object (via its setter), which means that it is not available to the toString() method.

As an aside, if you look at line 32, you will see that a fail variable is being set -- but this is not the fail variable of the student class. This is a local fail variable of the main class, which has been declared at line 8.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
Should the fail field be part of the student class at all? If it should, surely it is the same for all students and ought to be static. Anyway, it ought to be initialised in a static initialiser (or in the constructor). And then the get and set methods should be static, too.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Campbell Ritchie wrote:Should the fail field be part of the student class at all? If it should, surely it is the same for all students and ought to be static. Anyway, it ought to be initialised in a static initialiser (or in the constructor). And then the get and set methods should be static, too.

I think it should be public static final without a getter or setter. Edit: except doing that prevents grading on a curve if you ever want to, so it's probably better to keep that option available.
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
Henry - Ok I think I get it a little better but when i put it in the while loop, it doesn't make a difference in the output.

while (aScanner.hasNext()){
Student student = new Student();
student.setLastName(aScanner.next());
student.setFirstName(aScanner.next());
student.setScore(aScanner.nextDouble());
student.setFail(fail);
total += student.getScore();
studentsArray[count] = student;
count++;

I'm assuming I would do it like that because I'm not calling it from the scanner document, it's just from the student class. ? Any suggestions what I am doing wrong?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

lizz Palmer wrote:Henry - Ok I think I get it a little better but when i put it in the while loop, it doesn't make a difference in the output.

while (aScanner.hasNext()){
Student student = new Student();
student.setLastName(aScanner.next());
student.setFirstName(aScanner.next());
student.setScore(aScanner.nextDouble());
student.setFail(fail);
total += student.getScore();
studentsArray[count] = student;
count++;

I'm assuming I would do it like that because I'm not calling it from the scanner document, it's just from the student class. ? Any suggestions what I am doing wrong?


When this change, let's go back to one of your previous posts....

lizz Palmer wrote:I know that it's the math part that isnt working. I added the "fail" to my string message just to see what it would display and it displays zeros for all of them. But i'm not sure what I have wrong!


Yup. It's the math. At the point that the variable is being set, the fail value hasn't been calculated yet.

Henry
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I tried moving the calculation for the fail class above the scanner but it just doesn't run that way.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

lizz Palmer wrote:I tried moving the calculation for the fail class above the scanner but it just doesn't run that way.


Yup, that's correct. This is not something that you can fix by moving code around, nor can it be fixed by adding a line somewhere (actually, you can ... ) ..... Regardless, you really need to stop what you are doing, go back a few steps, and work this out with pen and paper first.

Henry
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
It's my value of fail, correct? It ends up with a value of zero for every student. That's not right. I can't seem to see what's wrong with it but I'm assuming it's in there somewhere.
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I really dont know what line I would be adding where lol. I looked at the values of fail and I think they say what I want so im lost.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

lizz Palmer wrote:I really dont know what line I would be adding where lol. I looked at the values of fail and I think they say what I want so im lost.


As mentioned, I recommend that you go back to pen and paper. You really need to understand (or figure out) what your program is doing. Fixing something by trail and error is really not an efficient technique.

Henry
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I've been trying what you said and doing it out on paper all day and I'm not further ahead.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

lizz Palmer wrote:I've been trying what you said and doing it out on paper all day and I'm not further ahead.



Some possible things to work out on paper. The flow of the program -- what it does, in what order, and when it does it. And when you do this, try to get as much detail as possible. The data structures -- how they are accessed, how they are processed, and even how they are connected.

Now... having said that... When I ask at what point is the fail variable completely calculated? you should know -- based on what you work out on paper. If you don't know, go back and add details to your flow diagrams.... When I ask at what point is the student object printed to the screen? You should know -- same reason. And of course, given this information, you should be able to conclude that the fail variable should be set *after* the calculation has completed, but *before* the student instances are printed. You now have the possible locations where you can make the change.

As for the best location, take a look at how to change it, depending on how the data is organized -- based on what you worked out on paper. If you don't, go back and add details about your data structures diagrams. From the diagrams, particularly how they are accessed, you should be able to find the best point. Heck, you may even conclude (as some other ranchers already hinted) that putting the fail variable in the student class is not ideal -- and be able to find a better way to print the output.

Good luck,
Henry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with toString message