Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File input  RSS feed

 
Alex Munoz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing code to solve a maze and the user is supposed to pick a file that contains the maze. I made sure that the file was in the same folder as the solver. The files are maze1.txt and maze2.txt and these are what print when the code runs instead of the mazes (Which are arrays) when Here is what I've written:


 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Munoz wrote:I am writing code to solve a maze and the user is supposed to pick a file that contains the maze. I made sure that the file was in the same folder as the solver. The files are maze1.txt and maze2.txt and these are what print when the code runs instead of the mazes (Which are arrays) when Here is what I've written:




You code prints out the file object, which should print out the file name (and maybe some identity hashcode -- not sure of this part).

If you want the file contents printed, you will need to open the file, read it in, probably parse it (if you are using some special format), and then print it out based on the loaded maze data. The File object is just an object used to represent the file, it is not the file itself.

Henry
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing a System.out.println(aFile); simply does a toString() call on the aFile object and prints that. If aFile is a java.io.File (<- click for the API) object it prints the "pathname string of this abstract pathname" as the API says it will.

If you want to print the contents of the file you will need to read the file in and print its contents, not simply print the java.io.File representation.
 
Alex Munoz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Alex Munoz wrote:I am writing code to solve a maze and the user is supposed to pick a file that contains the maze. I made sure that the file was in the same folder as the solver. The files are maze1.txt and maze2.txt and these are what print when the code runs instead of the mazes (Which are arrays) when Here is what I've written:




You code prints out the file object, which should print out the file name (and maybe some identity hashcode -- not sure of this part).

If you want the file contents printed, you will need to open the file, read it in, probably parse it (if you are using some special format), and then print it out based on the loaded maze data. The File object is just an object used to represent the file, it is not the file itself.

Henry


So when I created the file object, I have to open the file separately?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this post, Suresh Sajja gave you a Java Tutorials link. Have you read that? I shall close one of your two threads so as to avoid duplicating discussions.

I have added code tags somewhere where you had a block of ##O* because they have better formatting in code tags.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!