Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

first JavaRanch question

 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a question that should be simple. I'm working on a small program for an introductory class. The program takes a string and prints to a file with Scanner and PrintWriter. I only need to print the contents of the file in the command screen before closing the program. The textbook says it can be done with FileReader and FileWriter. Here is my guess:



But it doesn't work. How do I print the contents of the file?
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get each line of the file from your scanner as a String.

Look at the Javadoc API for Scanner for a method that would do this.

Then take that String and print it.
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Scanner method to take a line as a string is nextLine();



I get an error "No Line Found"

Does it matter that the file name is order.out?
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the problem. It was all to do with the place of the line to close the file:



I had to close the file before using FileReader.
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question: why does the font size vary between code snippets? I'm copying from the same file, but the font size is too small in some of the posts...
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesse Crockett:
Another question: why does the font size vary between code snippets? I'm copying from the same file, but the font size is too small in some of the posts...


I am not sure what you mean. You mean you see different font sizes in posts here? I am not sure how that is even possible and it sounds like a buggy browser to me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic