• 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

Store value returned by method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey I'm having a problem storing the value return by the admit() method
It should print "admitted" the first time through.
Then the next time through the loop, if you enter the same patNumber should print "Already admitted"
I've been told I'm not storing the return value correctly (if at all).
Is there not a simple way to store it that will be checked the second time through and go to the else statement?


 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi benjamin pritchard,

First of all - Welcome to JavaRanch.

Now about your program:
Its having a lot more problems, than you explained you're facing.

1. Code is very difficult to read, because there are no methods, and all logic laying down in one single lets by assuming in a "main" method. You should decompose your program into small methods, so you and your readers could follow your code, and understand what it suppose to do.
2. You're missing curly braces around "if-else" statements - it is error prone, don't take this habit as it is not a good one.
3. Such a lines "System.out.print("Enter selection: ");" doesn't specify what kind of selection user suppose to enter. You should give an options at least.
4. This code below is not doing what you expect from it to do. It should be in a loop, and iterate, while "length != 1".

5. Probably you should work first on the mentioned points above, then look forward what else you have.
6. Probably it would help, if you would write down for yourself all steps program suppose to do in simple English or other native language, and likely you'd see, that current code is not doing what it suppose to do.

Hope it helps.
 
benjamin pritchard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liutauras
Thanks for the quick reply.
I've left some code out (the menu that gives the options to select from) because I thought it wouldn't be relevant.
Unfortunately this is all part of the main method as well and probably adds to your advice that one method is difficult to read.
The menu seems to work ok when I execute it.
I understand it may not be the cleanest way to do things but I'm new to this and haven't really learnt how to make multiple methods.
Do you have any ideas how I could store the return value of the admit() methods so the next time round the loop it will produce "already admitted"?
Thanks for your help
Ben
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, the code which laying down in a "main" method is not correct, more specific: there is too much code in your "do-while" loop and it is not correct.
What you suppose to do within "do-while" loop, you should get a wanted character only, that's it. And other code part, which is laying down in "else" part, suppose to be outside "do-while".
Once again, do-while should ensure, that you'll get wanted character, which in your case are 'x' and 'X'. And after that, you should do what are you doing with "else" statement part.

edit: I see now what are you trying to achieve. Well if you say it works, it means works, you tried.

Think about case what happens if user enters character, which is not on option list, as well not a 'x' or 'X'. And keep entering this. What would happen?
 
Ranch Hand
Posts: 57
3
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

benjamin pritchard wrote:
Do you have any ideas how I could store the return value of the admit() methods so the next time round the loop it will produce "already admitted"?


There's probably a few ways to do this. A simple way to do this would be to create a List of values that are already admitted, then when a new one is entered, go through the List and check to see if one of the values match the input value. You can track this by using a boolean flag. It might look something like this:


Also, please take care when leaving out code - sometimes it's hard to tell what's an actual issue, and what's just been omitted when that happens. As it stands in your original posting, your code has several issues that could simply be because you left some stuff out.

Hope this helps!
 
benjamin pritchard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the main method, hopefully it makes more sense now!
The way it is now, if I enter 'A' at the menu and then the 'patNumber' it results in Admitted.
I want to enter 'A' the second time round the loop and the output to be "Already admitted"

 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other way achieve this, is by using HashSet. Try to google it for more information.
 
reply
    Bookmark Topic Watch Topic
  • New Topic