Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Strings one and two are being outputted as "null" instead of the phrase  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning!

Overview

I'm trying to create a code that will compare two Strings to determine if the first word should be placed before or after the second word in the dictionary, but Strings one and two are being outputted as "null" instead of the phrase I want. I'm not sure how exactly to fix this error; I'm pretty sure its a formatting error, but I can't find the cause.

Original Code

Class 1:


Class 2:


Original Code Output


Trial and Error?

The above code is the only trial where I got an output including the "should be placed after" phrase, so I figured that I was on the right track. I've also tried adding (one, two) to Class 1 to make-
   
Which only produced the output-


and I tried using (String one, String two)-
   
which led to the output-


Question

How can I fix the error on the original code above? Thank you for your time.
 
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your WordsCompare class has a method called setWords which is never called, so wordOne and wordTwo do not get set was you want.
 
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new WordsCompare(String one, String two)

When you declare a constructor inside a class you are required to provide a type for each parameter. When you do a 'new' of the class you must not use the type names.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Ron! How can I call setWords?
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Carey! I'm not quite sure what you're suggesting. Even if I don't use the type names, I'm still receiving the "null" output?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Carey! Sorry if I was unclear- the changes I included in my original question were those I tried, but my final code that I was working with was the code labelled "Original Code" above.

class 1:



class 2:

 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:

On line 9 you are calling a constructor that sets wordOne and wordTwo to null. Nowhere are you calling setWords() to change it to something other than null. The print on line 10 calls the toString() method will print out "null should be placed after null"
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I call setWords()?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's step back a bit and look at your design. I can't envision any scenario in which you'd want either word to be null, in which case your design should lean towards preventing that. This starts with the constructor which should take two words as parameters. This prevents the user from using the default constructor which automatically sets them to null. Once you've done this then you may not need or want setWords(). If someone wants to set new words they just invoke the constructor again.

Your compare() method sets the "compare" field. So far, so good. Now, how do you prevent someone from calling toString() without having first called compare()? One option is to make compare() a private method and then have toString() call compare() up front. I think that should suffice.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! I'm working through your suggested changes, but I'm running into a slight issue with how to have toString() call private method compare()? How should I format that? Thank you for all your help- sorry for all the questions!
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to consider putting the logic in your compare() method directly into your toString() method and then do away with compare() and the member variable "compare". Seems like that would be cleaner.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Carey! I made your suggested changes, but now I'm receiving the error-



What's causing this? (Code as it stands right now is included below)-



Thank you so much for your help and patience!

Also, thank you for the cleaning up suggestion! I'll try that once I solve the remaining errors with that code as it stands currently, if for nothing else but the experience.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake. I missed that compare() requires that wordOne and wordTwo be passed in as arguments.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another mistake...
Your compare() method takes two parameters "one" and "two" but never uses them, instead it uses the member variables wordOne and wordTwo which is preferred. So, in compare() remove the parameters seeing as how they are not being used. Then call compare() as I showed earlier and there shouldn't be any compile errors.
 
Saloon Keeper
Posts: 1174
73
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You might want to consider putting the logic in your compare() method directly into your toString() method and then do away with compare() and the member variable "compare". Seems like that would be cleaner.

Just a doubt.

You mean put the code of compare method into toString method and remove compare method and instance variable compare?

If that's what you meant, wouldn't that be better to keep code of comparing string separate in a method compare rather than mixing that code in the method toString? which probably changes the meaning or what name of method i.e. toString implies? I mean when we see toString we ought to think it converts something to String.

What if we write method compare as following

which also removes compare instance variable? what would you suggest?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. You're probably correct that compare() should be left as is, however I still feel that toString() should call it otherwise there's nothing to guarantee that it is called before toString() is called.

With that in mind though, I'd change the compare() method to return an int that toString can use rather than keeping the "compare" member variable for no other purpose.
 
Ganesh Patekar
Saloon Keeper
Posts: 1174
73
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:however I still feel that toString() should call it otherwise there's nothing to guarantee that it is called before toString() is called.

I think the same.
 
Ganesh Patekar
Saloon Keeper
Posts: 1174
73
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:I'm trying to create a code that will compare two Strings to determine if the first word should be placed before or after the second word in the dictionary


If you user input String two = "Ape"; which gives resulted output abe should be placed after Ape which is incorrect as per dictionary word order so better use compareToIgnoreCase(...) method.

I guess you're initializing those two instance variables in a parameterised constructor of class WordsCompare then you can validate them by putting a if statement in it as following

By the way following code
imports all the static members of the System class which you seem not using anywhere, you can remove that or make use of it as follow in Main class's main method.

 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help!

So, in compare() remove the parameters seeing as how they are not being used. Then call compare() as I showed earlier and there shouldn't be any compile errors.



I made this change to my code, making compare() and toString()-

But I am now receiving the error-

What does this mean?

If you user input String two = "Ape"; which gives resulted output abe should be placed after Ape which is incorrect as per dictionary word order so better use compareToIgnoreCase(...) method.


My current test data does not have words that with different capitalization, so thank you for pointing that out! I will look into that after I've fixed the errors with my current code!

Again, thank you both for your help and patience!
 
Ganesh Patekar
Saloon Keeper
Posts: 1174
73
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears you haven't initialized those two String variables in the constructor. Will you please post the complete new updated code?
 
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:. . . But I am now receiving the error- . . .

That isn't an error, but an exception and it is a good thing to see. I am really pleased to see it because it tells me that you are actually calling compareTo() on one of your Strings.
Ganesh told a long time ago to create a constructor for your object. The only way to be certain that those Strings will be initialised is to do so via a constructor. Otherwise people will see your object and not even know they are supposed to call setWords() or compare(). Write a constructor taking those two Strings, and also calling the compare() method. That is also the only way to be certain that the compare field is not 0 for the whole life of the object.

By the way: what are you going to do if the two words are exactly the same?
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It appears you haven't initialized those two String variables in the constructor. Will you please post the complete new updated code?


Class 1-

Class 2-


Thank you for the help!

The only way to be certain that those Strings will be initialised is to do so via a constructor. Otherwise people will see your object and not even know they are supposed to call setWords() or compare(). Write a constructor taking those two Strings, and also calling the compare() method. That is also the only way to be certain that the compare field is not 0 for the whole life of the object.


Thank you for the help! Just a clarification note- should I be making these changes to an existing constructor or writing an entirely new one?
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

. . . should I be making these changes to an existing constructor or writing an entirely new one?

How many constructors have you got? Count them.
 
Bartender
Posts: 1973
57
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest to read "Passing Information to a Method or a Constructor" too in the link Ganesh Patekar posted.

Let's read your code line by line:

 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help, and sorry for the (very) late response!

1) Should I be using something more similar to this?

(Running my code with only the above change to toString() gives me the output "WordsCompare@4aa298b7," which I don't understand in the slightest if anyone can help.)

2) I've been reading the resources you've directed me to (thank you!) and I couldn't figure out if creating the constructor was referring to the changes made above or if you were suggesting something more similar to what Salvin seems to be advising (which I interpreted as needing to change my Main class to pass "one" and "two" to "test," which I thought maybe I could do by making a change similar to-

or

both of which gave me the error-

(I tried the above changes (those listed under "2") with and without the change discussed under "1," but I received the same error during all trials.)

Thank you so much!
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please re-post all of your code again so we can get back in sync. I suspect that you haven't implemented a constructor properly. It also seems that you've introduced a new issue with toString() which should not be taking any arguments.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey! Sorry for the confusion!

Class 1-

Class2-
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, you haven't implemented a proper constructor yet so all you have is the default constructor that takes no arguments. Seeing as how you won't be needing setWords() any more and it is so close to what you need for a constructor you just need to make a few tweaks. A constructor's name must match the name of the class. A constructor returns no arguments, not even void. Make those two changes and you'll have your constructor.
Also, your toString() method should not have any parameters, it will get access to wordOne and wordTwo because they're member variables and toString() is a member method.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! I'm changed the constructor name; how should I make sure that the "constructor returns no arguments, no even void" ?
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the dumb question! I was receiving an error that I thought was being caused by deleting "void," but I found the actual issue.

Thank you all for your time and patience! I'll post my finished code in a second to help anyone who may be having a similar issue!
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:Worth reading The Java™ Tutorials: Providing Constructors for Your Classes or What is constructor in Java?

If you haven't read any of these articles yet I would highly recommend it. Ultimately, the goal is to know it, not just guess at it.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works correctly:

Class 1


Class 2


Again, thank you to everyone in this thread for your help!

A quick question: If I wanted to add more test values, is there a way to run the code (and test values "ape" and "abe" that exist in it) before clearing String one and String two of those values to run two new values with the same code?
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the direction! I will reread up on constructors (I didn't realize how little I knew about them until working with this code) so hopefully I can avoid this issues in the future!
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class is pretty much designed to compare two given words. If you want more flexibility you need to think hard about the requirements first. Then examine your current design to see if you can modify it or if you need to start from scratch. At the end of the day all your class does is put a wrapper around the String#compareTo() method, which in most cases, should just be used directly and not put in a wrapper class, but for your exercise that is fine. If you need to compare multiple words, now you're in the realm of arrays and lists.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! For both your help and your patience!
 
salvin francis
Bartender
Posts: 1973
57
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:Thank you so much! For both your help and your patience!


I am glad you learnt from this experience. I have a few more comments from my end...
First of all "WordsCompare" does not sound like a right name for a class,  Since "Compare" is a verb. I would suggest to call it "WordsComparer" or similar to make it a noun.

Next, this code does not smell nice :
It seems like we're relying on WordsCompare's "toString()" method to do the actual work of comparison. toString() is a simple method that most programmers use for debugging code. Here's the documentation for that:
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--

javadocs wrote:Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.


Can you use your method and constructor knowledge to write this code in a better way ? Don't worry about right and wrong, we all learn at some point of time.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!