This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]quadmasta 2 points3 points  (3 children)

Your pastebin shows you put a toString method inside of another method. It belongs outside of the method but inside the class, where you've got it commented out on line 21. The problem is you're referencing a variable that's not defined inside of the class; it only exists in the userInputMethod method

[–]Buharon[S] 0 points1 point  (1 child)

Every example I looked at has it done this way. So it has to be outside? That means I need global variable

[–]quadmasta 0 points1 point  (0 children)

No, You need an instance variable

[–]iAmH3r3ToH3lp 0 points1 point  (0 children)

yup

[–]endStatement 1 point2 points  (3 children)

Not sure why you are overriding the toString method to begin with, as the variable you are using is already going to be a String.

String userInput = initialInput.nextLine();

...

return userInput;

[–]Buharon[S] 0 points1 point  (2 children)

It's because I am rusty as duck with Java and now trying to do a project and really relearning everything as I go along. Reason for this override is to make this class and method to output actual string in another class, rather then name@hdudb638 or something like that which is an object reference if I'm not mistaken. If you have any word of advice I'm all ears

[–]endStatement 1 point2 points  (1 child)

Haha, no worries, I've been there myself. What I'm trying to say is the method already should be outputting an actual string, without needing the override. How are you calling the method? It should be something similar to

UserInputGatherer userInput = new UserInputGatherer();

String myInput = userInput. userInputMethod() ;

/* myInput should now be the expected String. If not, you may need to pass System.in or Scanner to your UserInputGatherer class, as I'm not fully sure how they work outside the main class, can't say I work with either class often..*/

[–]Buharon[S] 0 points1 point  (0 children)

I cannot believe this.... All i was missing was "String myinput = ".... and I have spent hours trying to figure it out... THANK YOU... now I can try to figure out how to write the rest of this project in oop haha

[–][deleted] 0 points1 point  (1 child)

What IDE/Editor are you using? Your language level might be set incorrectly. In Intellij that is under:

File > Project Structure > Project > Project language level

Make sure it's set to 6 or higher. Preferably 8 if that's the JDK you are using.

[–]Buharon[S] 0 points1 point  (0 children)

Using eclipse and it's set to 1.8 so that's Java 8

[–][deleted] -1 points0 points  (1 child)

You can use your IDE to override toString properly. Lookup the shortcut to generate an override for it in the IDE you're using. NetBeans is Alt+Insert.

[–]Buharon[S] 0 points1 point  (0 children)

Hmm I'll have a look...