• 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

no such element exception

 
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys I was just practicing,and ran into an exception,the problem is I don't understand why the exception is being thrown I have done some research and many people say it's because I may have closed another scanner object(stream) but in my my case this isn't true I've been trying to figure out why this exception is being thrown,anyone see anything wrong?

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)
at Camp.getDetails(Main.java:133)
at Camp.start(Main.java:70)
at Main.main(Main.java:17)


obviously I'm doing something wrong with the scanner but can't figure out what,


thanks
 
Marshal
Posts: 8856
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
Why do you have such a heck an amount of scanners?

Never ever close Scanner object which pointing to System.in as you may never open it again. This is what in fact happened to you.

Create an utility class where you instantiate scanner once and use across your project.

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should never close System.in if you ever want to read from the console again.  After the statements on lines 82, 113 and 158 the code will not be able to read from the console.
 
Liutauras Vilda
Marshal
Posts: 8856
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
Apart from the problem you identified, you have more serious problems, but that would take time to fix.

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.
 
Liutauras Vilda
Marshal
Posts: 8856
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
If I were you, I'd take day off and start over. Probably current code couldn't be used as a reference too. Maybe just text prompts.
 
Liutauras Vilda
Marshal
Posts: 8856
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
Tomorrow night I'll have some time possibly, so could help out a little with a start towards more object oriented code.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I had a look once again. I might was too strict.

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.
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Liutauras,yes that was the reason why I was getting an exception I never knew this about closing the scanner with system.in,when you close it with system.in you can never reopen it? I thought that each scanner is separate so Scanner a = new Scanner(System.in) would be different to Scanner b = new Scanner(System.in) ?

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?

thanks
 
Liutauras Vilda
Marshal
Posts: 8856
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
Do you have some kind of requirements, where we could have a look at very first part, so we'd know what is the task first?
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a project I'm working on to improve my java skills,

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.

thanks =)
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

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?

thanks
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry guys here is the code







^^
code in question,


thanks
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys I figured it out what I did was create a new bufferedReader each time numberOfChildren was updated,now I'm not sure if this is best practice or if there is a better way of doing this?

 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys

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




thanks
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you list the order of the events that you think the program does?
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.
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Thanks
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 any way I can fix this?


Do not create an instance of a output writing class until you  are ready to write to the file.
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no luck =(

I tried changing the code and put it in the enterChildDetails() method so that I created the instance when I was ready to write to the file but still the file seems to get overwritten(or everything wiped) once the program executes

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Norman,yeah the append option would work but no in my case because I want to be able to store the number of children  in a file,if I append to it I will have a file with many numbers,the main reason why I need there to only be one number is that when the program executes I need to be able to read from the file so numberOfChildren will equal how many children were added by previous executions of the program,

thanks
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, do some debugging to see what order the program does these steps:  writes to, closes and reads from a file.
It should be something like:
create output
write to output
close output
create input
read from input
close input
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adam, good job for keep trying, but I still think the current design of your program for you makes things way more complicated than they supposed to be.

After reading your specified requirements, I'd think of having at least such next classes:
  • SummerCampLauncher - would kick off the process
  • SummerCamp - would control entire summer camp's process, logistics
  • Registry - where all children would be registered for a summer camp event
  • Financier/Accountant - who would take control of the payments collection, presentation of prices
  • Payment - ? might be needed too
  • Child
  • Utilities - for having some helper functions


  • 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.



       
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    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
    After you identify classes you may need, carefully think about their responsibilities. Class supposed to be responsible for a sole purpose only.
    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.
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks Liutauras really appreciate all the help =)

    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 =)
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Norm

    this is the order in which the events happen print from console

    ***create input***
    ***read input***
    null
    WELCOME TO THE SUMMER CAMP
    how many children?
    1
    _______SUMMER CAMP________
    PRICE RANGE
    $35 : 6-8
    $25 : 8-10
    $20 : 10-12
    enter the full name of your child
    Bob
    enter the age of your child
    9
    the total price is :25
    do you want to pay now,1 for yes,any other key for no
    1
    ***create output***
    ***write output***
    do you want to exit? press 1 for yes any other key for no
    1
    ***Close input***
    ***close output***
     
    Norm Radder
    Rancher
    Posts: 5008
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The close input should be done before creating for output.

    Did you look at the contents of the file with an editor after the program finished execution to see what was in it?
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I added some lines of code and got the program to do what I want but in my opinion still have not solved the problem because it's overly complicated and probably could be simplified and the text file will be bigger than I want,I want the text file just to hold one number which is the number of children not the number each time the program executes(appended ) surely there is some way of doing this?

    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



    Thanks
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    note I will be re doing the project again,as Liutauras said I need to make it more object orientated,but if anyone could help me solve this small hiccup before I move on that would be great,


    thanks
     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It is often the case that refactoring your code leads to problems going away -- at least they become easier to spot.
     
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, your numberWriter is opened to append, so that's what it's going to do.
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    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

    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.


    @OP
    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.
     
    Norm Radder
    Rancher
    Posts: 5008
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would change the write... method's name:
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Liutauras and hey guys

    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

    thanks guys
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hey guys I've run into a problem,also sorry if the code is a little sloppy,

    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,


    thanks
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    That first call to next() consumes the first name (Mike) and then the call to nextLine() gives you 'Smith'.
     
    Marshal
    Posts: 79151
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adam Chalkley wrote:. . . Mike Smith the first part of the string in this case Mark is cut off and only Smith . . .

    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.
    Features of utility classes:-
  • 1: They only exist to help other code.
  • 2: They only have static members.
  • 2½: There is no need ever to create an instance of a static class.
  • You can find out how to prevent instantiation from the Java® Language Specification. This thread shows how I would write a method to read the next int. Never close that Scanner. The only problem with that version is that it isn't thread‑safe. Note how Stephan and I have taken completely different approaches to Scanners.

    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.
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks guys much appreciated,

    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

    thanks
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Campbell also I tried doing what many other people on forums said but to no avail such as this

    https://stackoverflow.com/questions/27141183/scanner-nextline-occasionally-skips-input

    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 =)
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    still stuck can't figure out a way to fix this =(
     
    Adam Chalkley
    Ranch Hand
    Posts: 545
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I changed the code a little bit and got it working but to be honest it was just trial and error and I still don't know why my changed code worked and I still don't know why I was getting the bugs in my code above this post?

    if anyone could explain I would really really appreciate it I'm still quite lost

     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic