the code is just under 200 lines so I'll print the stack trace here and also limit it down to where I think the problem is
this is the method I believe is causing the problems
Exception in thread "main" enter the childs full name
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
obviously I'm doing something wrong with the scanner but can't figure out what,
I'm having hard times to read your code. Main problem is that you have classes, you have methods, but there is little to no logic why some methods are in some classes. I'm failing to find relations. Hence this all your OO techniques are pretty out of sync with normal logic people are tend to follow. i.e.:
You have class Camp, this class has method payNow(), pay what? and what is that Camp in the first place? Hang on, same class have method printList(), that's probably even worse, what list and of what?
Why getDetails() method adding child?
Do you see how everything is messy and non sensical?
I'm sorry for pointing those things out (they may sound as critics), but at some point i've been criticized too so it helped me to do research and find better ways.
1. Camp. I'd change maybe that name to ChildrenSummerCamp or ChildrenCamp? What do you think, it might would be more obvious what that camp is?
2. Method addChild() is quite misleading to me. Maybe registerChild or enrollChild? Which could finally addChild if all payments have been made, etc...
3. Method printList() isn't good, unless we'd talk about some kind of collections List class's list method implementation. Now you have some exact context what is your project about, so there could a better name for sure. Apart from that, this method does two things, it prints children names who paid for summer camping services and who don't. Those two would need to be distincted. Remember, method supposed to do one and only one thing. Maybe think similarly along the lines:
Such style is still procedural, but slightly better than what you have now in my opinion.
4. I would not expect to see method start() in a Camp class. I don't think it is a Camp's class responsibility to control program's interaction.
5. You have fields in a Camp class public. That's a bad thing and shouldn't be like that, but about that we could continue later... maybe tomorrow.
If you are up to start over, I'd be happy to participate and help a little.
and yes I agree 100% I think I could have made the project more OOP orientated,and the naming is bad it's quite confusing.
Yes I would love to start again with your help if you don't mind =) that would be great,should I start from scratch again or should I work with the existing code or as you mentioned maybe start again and just use that code as a reference?
I wanted to make a program for a summer camp,the program should be able to store the number of children in the camp,the children's ages and names plus a unique ID number,the children's information should be stored in permanent storage (could use a database but in this case will use a text file),a price must be calculated and then displayed for the user to pay or choose the option pay later.when the program finishes as mentioned all the information should be stored in permanent storage so when the next time the program is run all the children's information is loaded back into memory (maybe stored in an List or could even be a Map),there should be two files with information one with the children who have paid and one with the children who still have to pay,The program when finished should also print out the list of children including displaying if the children have paid or not.
the reason I'm using a file is because I want to improve on working with I/O.
I spent my morning working on it,anyway I came up with a better solution,it's still quite sloppy but it's an improvement on the former,note I'm still not done,
anyway I want to overwrite the numberOfChildren.txt file with the number of children each time that variable is updated,I followed a few threads online but can't figure out why it's appended and not overwriting,I set the option to both true and false and still appends also declared the objects outside the constructor and still no look,
any idea how I can overwrite the file each time the number is updated?
I have a major hiccup in my program,I write to the file with the number of children that works great but I have a problem with trying to read from that file every time the program executes the file get's overwritten with nothing essentially deleting all of it's contents,when I then try to read from the file(trying to set num = to numreader.readLine() ) I get null,
anybody know a way around this and what I am doing wrong?
here is the part of the code which the problem is occuring
and where I write to it
Does it create an output class instance to write to a disk file before trying to read from that file? Creating the instance of the output class can create an empty file.
very good point I was thinking this may be the problem myself for a little bit but brushed it off,so when each time the program executes and I set BufferedWriter to equal null,the file gets overwritten?
if so any way I can fix this?
file seems to get overwritten(or everything wiped) once the program executes
Yes, it will overwrite everything when the class object is created. Some output classes have an append option in the constructor. Can you use that?
Try doing some debugging by adding print statements next to the statements that create, write to, read from and close the file(s) in question. The print out will show you what order the program is accessing the file(s).
After reading your specified requirements, I'd think of having at least such next classes:
After starting implement this, it might would turn out of a need of some other classes/interfaces.
For instane your current method enterChildDetails() does so much, that it is hard to follow what it actually does. Also it does things at wrong abstraction levels. i.e.
enterChildDetails() and addChildToCamp() in my opinion supposed to be at the same level, rather than former contain latter inside. paymen() method also shouldn't be inside of enterChildDetails() - what payment has to do with entering child's details? printChoices() method's name isn't obvious what it supposed to do without looking for its implementation - that suggests, that method name isn't descriptive.
It seems to me, that you start very big, I'd start building program in slightly smaller pieces, with some actual testing rather than run and see what happens.
If it is Registry class, its responsibilities are to collect child's personal details and enrol him to a summer camp. It is the only one class which supposed to know how many children are enrolled as of now; their names; contact details; how many children enrolments are in an interim stage for one or another reason, so you might could contact them and ask for more information if needed in order to finalise enrolment.
If it is Financier or Accountant (whatever you choose name), it supposed to be responsible for defining prices to get onto summer camp, to provide prices when it is asked, it supposed to hold information which children paid in full, which children's payment's are awaiting; their preferred payment type. Financier shouldn't be asking child's details, maybe registry office should provide that as they got them during child's enrolment (21st century, everything is computerised).
I hope these thoughts might be useful for you in thinking and trying to re-design your application in case you'll have time.
I agree I think it can still be more object orientated so I will re-write it but this time use more classes as you mentioned such as registry,launcher,payment etc it would make things easier and tidier =)
before I start re implementing the code just wondering how I would go about fixing the problem I have about the file which holds the number of children been wiped clean each time the program executes?
Norm has given me very good advice but I still can't find a solution,I'm going to keep trying though using his advice maybe I'm just doing something small wrong
thanks Norm and Liutauras =)
this is the order in which the events happen print from console
WELCOME TO THE SUMMER CAMP
how many children?
$35 : 6-8
$25 : 8-10
$20 : 10-12
enter the full name of your child
enter the age of your child
the total price is :25
do you want to pay now,1 for yes,any other key for no
do you want to exit? press 1 for yes any other key for no
I'll just put the updated code here and comment which I have changed,would be great if someone knew how to solve this problem instead of having messy code to solve the problem,thanks
Dave Tolls wrote:Well, your numberWriter is opened to append, so that's what it's going to do.
Knute Snortum wrote:It is often the case that refactoring your code leads to problems going away -- at least they become easier to spot.
Mock refactored version:
As you see, there is no better time to refactor as "now" - almost always. But I know how hard can it be to convince yourself.
sorry I have not been online in almost a week I dropped my laptop and damaged the nic card and hard drive,luckily I recovered all my project files before it failed anyway it set me back a couple hundred,but I'm back online now and will get right back into finishing this project in the morning
anyway it seems that when I enter a full name such as Mike Smith the first part of the string in this case Mark is cut off and only Smith is saved to my file,
I'm guessing there is something wrong with this block of code,I've spent over an hour looking for the solution but can't seem to find out where I have gone wrong here is the block of code in question
helper method to check if string is valid
can anyone see where the problem lies? it was working fine until I added the helper method,
Mark my words: don't create Scanner objects in your class. Create a utility class which has one Scanner field and use that scanner exclusively.
Adam Chalkley wrote:. . . Mike Smith the first part of the string in this case Mark is cut off and only Smith . . .
Features of utility classes:-
Are you using next() or nextLine()? If you use next() with the default delimiter, you will get the first name, Malc, but it stops there. The second half Smith waits to be read later. If you use nextLine() on its own without anything preceding, you would get Martin Smith as one String object. Note that you only ever get null from a Scanner if you use one of the two methods whose name starts find... All other methods return something, and if they have any difficulty finding what you are looking for, throw some sort of Exception.
Tell us what your book says nextLine() does, then compare that to what the documentation does. That means that after nextSomethingElse() [enter key], you may be right at the end of the line, so nextLine() returns the empty String "", which is not equal to null. If on the other hand, you are part‑way through a line, you will only get the second part of the line; it is clearly stated in the API documentation.
This is how I sorted that problem out:-That will still only give you the right half of a line if you call it when half‑way along a line.
Once you get such a utility class working, you can enhance it and keep using it for ever.
Campbell great explanation I am still quite confused and I'm going to research scanner more just a follow up question to that information you gave I will try that code now,
when I get rid of the scanner.next() and replace it with scanner.nextLine() it seems to skip a line so I can't enter a second child in
here is the output
enter the full name of your child
enter the age of your child
Here I can't enter the name of the child
if I put the scanner.nextLine();
after this block of code
it will appear with two lines so essentially I have to enter something in twice which I don't want to happen
sorry for plugging stackoverflow I'm loyal to coderanch =)it's just the only source I could find
you explained a solution in the last post but I'm not too sure how the solution works,
thanks for the help =)
if anyone could explain I would really really appreciate it I'm still quite lost