• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

File input

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 75663
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic