• 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

why cant i return a value from this method?

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 11, where did you declare the return type of the method?
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't it be
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1205
22
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
reply
    Bookmark Topic Watch Topic
  • New Topic