Prompt for the file name of the output file to write the formatted words to. Use a File object to test that the file doesn't already exist, and if it does, prompt to check for overwriting the file. Open the file for writing using a PrintWriter. Use a do-while loop to repeat these steps until a valid output file is entered.
Loop, reading words (text between whitespace) from the input file using Scanner.next(), appending them to a line of text being formatted, with one space character between words. Keep appending words until the current word would cause the line to become longer than the output column with. If so, write the line to the output file, reset it to contain just the new word, and continue reading. Continue reading until the end-of-file is encountered, using Scanner.hasNext().
I was trying to code it based on a tutorial but that one writes a line to a file hardcoded in the program. The biggest issue I'm having is the second instruction. Here is what I put together so far:
You're creating a File instance while creating & initializing the Scanner instance "scan":
Scanner scan = new Scanner(new File(inName));
If you want to work always on the same File you'll need to widen the scope of that instance, i'd suggest you create a null instance at the beginning of main() and then initialize/reinitialize when needed. Remember that File objects work with file and directory pathnames, for more info on the File class methods check this
the code you're looking for it's something like this
I can’t see any code in your post which writes anything. I suggest you start in the Java Tutorials, where I think you will find what you need, and lots more.
In fact I don't understand what the point of line 24 is anyway. You just went to a whole lot of trouble to confirm that the file name the user entered actually existed, and then you're throwing that name away and having the user enter it again?
jay russ wrote:Am I not doing this correctly? And yes when I say it does nothing it just goes to the next blank line waiting for the user to enter something.
Of course you're not doing it correctly. If you were, your program would work and you wouldn't be here asking this question. But your program doesn't do what you want, so by definition you aren't doing it correctly.
Anyway I suggested there was something strange about line 24. I don't think you have responded to that. What is it supposed to do?
Yes, but 160 lines of code with no indication of what you think the problem is will put people off replying. Go through some of the links on this page.
jay russ wrote:Is anyone available to take a look at my previous post?
In the exit() method, why are you not using the junk String? Why are you reading nextLine()? That isn’t part of exiting.
What does that mean about length of line? Where do you get the length from? How are you measuring the length of the potential output (it is pretty easy)?
How are you putting several words together (hint: try here)?
You appear to be doing everything in the main method. The ideal length for a main method is one statement. You ought to move most of that code out into different methods. you should have a method which opens files, a method which reads, a method which adds to the output, a method which writes, etc. You should get one of them working (eg read) before you try anything else. You are trying to do too much all at once, and that doesn’t work. Break it into little pieces and then try.
When you get stuck about lengths, try working it out on paper. Not in code. Not until you know what it is supposed to do. Then you can convert it to code.
And despite what it says in some books, you should not use the \n character unless you have been told specifically that \n is required. You should find a brief explanation of that here, and it tells you what do instead.
Now you can actually create your application.
Get rid of that long main method. I think a main method has an ideal length . . . of one line.
Do it in stages.
Start by trying to read words. Create yourself a FileScanner class which has a java.io.File object as a field, and uses that File object to initialise a Scanner (or other means for reading). Give it one (public) constructor which takes the File object as a parameter. Give it methods like nextToken, nextInt, nextBigInteger, hasNextInt, hasNextToken, nextLine, etc. Comparison with the java.util.Scanner class will show that you are creating a “wrapper” round a Scanner object. all the methods I have suggested have counterparts with similar names in the Scanner class. You may suffer a FileNotFoundException in the constructor; that should be re-thrown not caught, so you have to use the throws keyword. You can read about re-throwing Exceptions here. You can also get the most recent Exception; there is a method in the Scanner class which returns that Exception.
Now you can test it like this:Note the way I have used args means, in my version, you must invoke it like thisYou must supply the name of the file, and the "" are to make sure the runtime doesn’ separate it into two on the space.
Now you have got that bit working, you can forget the FileReadingDemo class and work out how to put the individual words together. But that is for later. Don’t try to do it all at once. Get one part working and only then consider the next part.
You can read more about file reading here; the section about scanning tells you about Scanners. There is an example similar to what you want.
Now I don't know how to use it in the main method. I thought something like this:
I only have a couple days until this assignment is due so maybe it's better to just do it all in the main method. This is really difficult when you're not being taught.
jay russ wrote:Can someone show me the correct way to open a Scanner object on a File object, and use the next() method to read each word? I can't figure this out. I put this together.
Well, for starters, I don't see you calling next() anywhere. I see you're using Scanner in your code above, so you should at least be able to make a stab at it.
Also, naming a variable that's used for reading data in as out is not exactly the way to write clear, easy-to-understand code.
This is the section where it displays each word on its own line:
What can I do with that to get it to do what I explained above?
I think it would just be in this section that needs some altering:
I'm appending each word to the string "newReader" but I want it to read up to the max width, flush the string, and then read the next set of words and repeat this until the end of the file.