• Post Reply Bookmark Topic Watch Topic
  • New Topic

why cant i return a value from this method?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why cant i return userText to the class that called the function to have the user make a selection?
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i also tried putting return after the if block
i get an error
return required and cannot return with void keyword...dont understand this
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 11, where did you declare the return type of the method?
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't it be
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . .. . .
But System.in always has more input, potentially, so hasNext() will return true, unless some wally has gone and closed System.in.
Your enterNumber method is incorrect; I can see two errors. Once you have sorted out the enterText method, you can probably work out what is wrong with the number method.

Oh, I can see a third error in it.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:


Just because you can make '} else System.out.println("...");' a single line doesn't mean you should.  In fact, do yourself, future maintenance programmers and the Java community as whole a favor and get in the habit of (in order of importance, IMHO)...
A) Start the conditionally executed code (the System.out.println() in this case) on a new line, even if you don't always use braces.
B) Always use braces even though you don't have to.
C) Put the else on a new line.

C) is just a question of style an some folks here may disagree.  Fine... put your else on the same line as the closing brace for the if().  All the time and disk space you spend putting in unnecessary braces will be more than paid for the first time you DON'T have a hard-to-find bug like...

Initial code


Modified code added during debugging:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!