• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused as to why Set Method isnt working?

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Whenever I run my program, the get method does not seem to work, I enter an empty string, therefore expecting for the error message to be generated, to no avail,
 
tony narloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I meant the set method does not work.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tony narloch wrote:I enter an empty string, therefore expecting for the error message to be generated, to no avail,



Tell us exactly how you're entering an empty string.  In your setter, you can put a statement to print what the length of the string is, like so:


Try that with the same input you've been giving and tell us what you get. Copy-paste the output from your console.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pressing the Enter key by itself will not cause next() to return a string unless something precedes it. You can hit a whole bunch of Enters and nothing will happen because next() will block until you give it some text.
 
tony narloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


And all I get is:

Listening on javadebug
User program running
User program finished

BUILD SUCCESSFUL (total time: 4 seconds)


When I used the:



Same output:


Listening on javadebug
User program running
User program finished

BUILD SUCCESSFUL (total time: 4 seconds)

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do this
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You could do this


Never mind. That won't work. You'll have to use nextLine() if you want to detect Enter without any text.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tony narloch wrote:


That code doesn't make much sense. If the length of the string entered is 0, then that code will say "Valid entry. Thanks!" -- is that really what you wanted?

Also, use nextLine() instead of next() if you want the user to be able to just hit Enter to respond with an empty string.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have WAY too many blank lines. Also, your indents need to be 4 spaces for each level.
 
tony narloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ok, so the program is now running, but I am now a bit confused, because it would seem that I am not setting a value in the set method now? :|
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you want to set? Your Player has no member variables. Usually it is a member variable that you set. For instance
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the code carefully. Are you setting the value of e anywhere that can be accessed by any instance method? Looks to me like you don't. Do you know how to declare and use instance variables in Java?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your object should have its own members, for example.



However, These names should be more descriptive so that other programmers reading your code can follow along. For this example, I'll use m_Input.



m_Input is now a member, it is however private. So only methods that are inside of the Player Object can assign a value to m_Input or manipulate m_Input's value.

Other methods that are not inside of the Player Object can't access, or mutate, m_Input because they cannot see it- this is because m_Input is set to private.
We can allow them to access m_Input by creating public methods to set m_Input up with a value or to get the value that is stored in m_Input (which is what you were doing).




Take note of the "String e" inside of the arguments of the function. You can change the variable "e" to whatever you want, and its scope is only defined inside of the setInput function. "e" will lose its value once this function ends. So we store its value to the member's object m_Input so this way we can keep it.

Also, keep in mind that the value stored inside of the member m_Input can only be seen by methods that are inside of the Player Object, you will have to code a method to allow others to see the value inside of m_Input.



Please note the argument has nothing inside of the argument parenthesis. The getInput method only returns the value of the Input whenever it is called.

Since your program has a static method, you will have to create an instance of your Player object inside of it, which you did.



Whenever you call "new" on an object:
In memory, the computer allocates enough space to store all of the Object's members and their values for that instance of your object. In this class, our instance of the Player Object is called player.
This means, in memory, player, has its own member m_Input that we can set a value to and view its value using our get and set methods.

I was informed by staff to not give you a solution and have you work on it.
I hope this helps point you in the right direction.

Good luck
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Cacuza wrote:Your object should have its own members, for example.
However, These names should be more descriptive so that other programmers reading your code can follow along. For this example, I'll use m_Input.


Java conventions say that an underscore (_) should not be used in naming variables, methods, or classes. Use camel case instead. The one exception to this rule are constants whose names should be in all upper case with underscores separating name components. Additionally, the use of underscores or even something like "m_" as a prefix to indicate that it is a member variable is frowned upon.
 
Jon Cacuza
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Jon Cacuza wrote:Your object should have its own members, for example.
However, These names should be more descriptive so that other programmers reading your code can follow along. For this example, I'll use m_Input.


Java conventions say that an underscore (_) should not be used in naming variables, methods, or classes. Use camel case instead. The one exception to this rule are constants whose names should be in all upper case with underscores separating name components. Additionally, the use of underscores or even something like "m_" as a prefix to indicate that it is a member variable is frowned upon.



I apologize, at my job we use m_ to define members of the object- it helps in big projects. I will stick to java's naming convention here on the forums though. ;)
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Cacuza wrote:I apologize. ;)


Not a problem. And welcome to the Ranch.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Minor point. Why are you using == 0 when you could use this?
 
Ranch Hand
Posts: 606
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tony narloch wrote:


(off topic)
I confess  was really akin to write curse words when I started my first experiments.
but i guess was sublimated frustration more than real adverse feelings
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"the use of 'm_' prefix is frowned upon"

I'm trying to stay away from making seemingly subjective statements like this in isolation. I would offer some kind of rationale as well.

For me, prefixes like these are vestiges of Hungarian Notation, something that was started with good intentions but was quickly abused and misused to the point where it was more harmful than helpful.

The original intent of HN was to give a label that communicated an intent such as "f" for flag, not float as many later thought it was. Not all floating point variables were supposed to be labeled with "f" since not all of them were used as flags. Misunderstanding this led to every floating point variable being labeled with the "f" wart. Not good. Now the original intent was lost.

When Jon said that the m_ convention helped in big projects he worked on in the past, my spidey senses started tingling. It makes me think that the classes and methods were probably so big that it would actually be helpful to tag member fields so you know that's what you're looking at 1500 lines deep in a class in a method that is 100s of lines long.

Of course, the remedy to that problem and the key to eliminating the need for HN warts like "m_" is to follow rule #4 of Kent Beck's Rules for Simple Design. If you keep classes and methods small and the scope of variables as small as possible, it is much easier to tell if a variable is local to the method or if it's a member variable of some kind. Then you can just focus on finding a good intention-revealing name that you can actually pronounce.
 
tony narloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It is a trivial program, but I hope that the comments and annotations I've made on it will be sufficiently helpful for other newbies when it comes to grappling with these OOP concepts.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 69-75 should be in a toString() method, then you can do this:
 
tony narloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was just trying to give back....:\
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tony narloch wrote:Was just trying to give back....:\


That's cool. Don't take any additional comments the wrong way though. There are a number of things you can do to make the program better and what Carey said about a toString() method was true.
 
reply
    Bookmark Topic Watch Topic
  • New Topic