• 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

How to end this loop

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I am writing to a file, but it never ends, it keeps asking for more input please help!!


Here is the main class
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When would you expect istream.read()) to return -1? That is, how is the JVM to know that you're done providing your input? Maybe you're thinking once you hit return it should be done? That wouldn't work, because then how would we ever enter multiple lines interactively?

You need a way to explicitly tell your code that you're done. If you want it to accept only one line, then calling Scanner.nextLine() or BufferedReader.readLine() is how you would get all your input--no need for a loop even. Otherwise you can read a fixed number of lines or characters, or have your user input a special string, like "QUIT" when he's done.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, you just need to enter the end-of-stream character if you're using System.in as your input stream. Just hit ENTER then Control-D (the end of stream character) and that should terminate the loop.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Actually, you just need to enter the end-of-stream character if you're using System.in as your input stream. Just hit ENTER then Control-D (the end of stream character) and that should terminate the loop.



Heh. I'd totally forgotten about that. Although on Windows I think it's ctl-Z.

While this approach should work, it's not standard and I wouldn't use it in most cases. If you were writing a command shell maybe, but otherwise, I would avoid it.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right about Ctrl-Z in Windows but I'm not sure about your assertion that it's not standard. Standard input is, after all, normally associated with user input from the command line. Also, the JavaDocs for java.io.InputStream say specifically that -1 represents end-of-stream. Can you clarify what you mean when you say that it's not standard?

http://stackoverflow.com/questions/3603047/find-the-end-of-stream-for-cin-ifstream
 
Nick Dook
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much guys, tried the ctrl-z + end of stream key, but it didn't work.
So i decided to use this instead.

I have a problem though, I have to take 7 inputs from the user and inserting it into a text file in one line, separating each word by a comma, any suggestions?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[quote=Junilu LacarAlso, the JavaDocs for java.io.InputStream say specifically that -1 represents end-of-stream. Can you clarify what you mean when you say that it's not standard?


I didn't mean that looking for -1 to indicate end of input is non-standard. That is certainly standard.

I mean the looking for end-of-stream on standard in is not standard. At least not the normal route for most Java programs that I've seen. I know in Unix it's not uncommon for interactive programs to accept ctl-D to mean "I'm done, you can exit now," but in my experience, with Java. probably since it runs on various platforms and environments, it's more common to look for a special input from the user, such as "Q".

@OP: Either approach is valid. Pick the one that's makes the most sense to you for the situation at hand. More important than which one you pick is whether you understand the two approaches and what the difference between them is. If not, don't hesitate to ask for clarification.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Dook wrote:Thanks very much guys, tried the ctrl-z + end of stream key, but it didn't work.
So i decided to use this instead.

I have a problem though, I have to take 7 inputs from the user and inserting it into a text file in one line, separating each word by a comma, any suggestions?



"Didn't work" doesn't really tell us anything, and I don't see any test for end of stream there. It looks like you took part of the approach I suggested, but didn't get the whole point, and at the same time are trying to use the other suggested approach.

If you can provide an SSCCE(⇐click) and indicate exactly what you mean by "doesn't work," we'll be better able to help you.
 
Nick Dook
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks very much for all the help.....I got through with my question using the following code below:




My next question is how do I remove a line of data from a text file, by prompting the user which line to delete?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Dook wrote:My next question is how do I remove a line of data from a text file, by prompting the user which line to delete?



You don't. What you do instead is to generate an entirely new text file which contains all of the lines except the one which was to be deleted. Then delete the old file, and rename the new file to have the same name that the old file used to have.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Dook wrote:thanks very much for all the help.....I got through with my question using the following code below:





Since you're dealing with text, not bytes, use a Writer, not an OutputStream, such as a BufferedWriter wrapped around a FileWriter. And rather than putting explicit "\r\n" in your Strings, let the Writer output the platform-specific EOL for you.


Also, no need to initialize a to the empty string, since you never use that value. Just String a; is sufficient.

Also, you'd do well to find a more descriptive variable name than "a".


My next question is how do I remove a line of data from a text file, by prompting the user which line to delete?



Read the file line by line, writing each line out to a temp file, but skip writing the line to be removed. Then rename the temp file to the original (possibly having to remove the original first).
 
Nick Dook
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this is what i wrote for my remove method()



My error is that it says "Could not delete file". Please help!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That could be because the file is still opened by your program or another program, or you don't have permission to delete the file.
 
Nick Dook
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am calling the same text file multiple times in my program, so I am not sure if that is causing it to not remove the line of text.
But I don't know how to fix that problem,please help:
I have 2 methods calling the same text file.

Can I initialise the file in the main?Will that fix my problem?

@Jeff thanks , I used the FileWriter instead and it helped me(by not putting the \r\n).
reply
    Bookmark Topic Watch Topic
  • New Topic