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

receiving data from a *.txt file for arrays  RSS feed

 
Randal Mortell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new Java so forgive me if I use the incorrect terms. Anyway I'm trying to figure out how to receive data from *.txt files for arrays. In my class book it says I can receive this information from Scanner but doesn't go into detail on how that is possible. Any help or advice would be appreciated. I'm unfamiliar with showing code on here but the following is the code from my book. Not sure if I'm supposed to copy paste code here or something else. Thank you for your help.



Here is some of the content of the *.txt file. It's way to long to post the whole thing.

1 1 76.4 0.92
1 2 77.7 0.93
1 3 77.7 0.93
1 4 77.7 0.93
1 5 77.7 0.93
1 6 77.7 0.93
1 7 77.7 0.93
1 8 77.7 0.93
1 9 77.7 0.93
2 1 76.4 0.92
2 2 77.7 0.93
2 3 77.7 0.93
2 4 74.7 0.93
 
Paul Clapham
Sheriff
Posts: 22491
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randal, welcome to the Ranch!

First of all thanks for formatting the code in such a readable way, it's a lot better than a lot of newcomers manage.

But anyway, your question: you've got the code and you've got the text file, and if I understand it right you don't know how to connect the two together. The hint is here:



So, can I assume that you are using the command line to do your work, and not some development environment? Because that's what is implied by that comment. The first step is to compile the class -- have you done that yet? If not then you should. The second step is to run the compiled class, and you'd do that like this:

java Weather < path.to.the.file.txt


The "<" in that command is what the comment means by "input redirection" and the result of using it is that System.in (see that in the code?) is connected to the contents of the file to the right of the "<". Make sure you use the full path to the file, or alternatively make sure it's in the current working directory.
 
Randal Mortell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been using Eclipse for my class. I haven't used the command prompt for much. The video for the book showed how to use it. I was wondering what the path or route could be to receive the information from the text file through scanner. It basically says you can do it but doesn't really go into how to do so. I guess the way to word it would be I would like to link or reference the text file in the java file to allow arrays to read that data. Maybe I misread and Scanner isn't the path to take. I've seen information online about Buffers but we haven't touched any of that in class or in my book so that may be over my head but I can try to follow.
Thank you for the quick reply.
 
Paul Clapham
Sheriff
Posts: 22491
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea how to tell Eclipse to redirect System.in to use a particular file (even though I've been using Eclipse for years now). But you could change the program to read from the file directly instead of System.in -- that's what I would have done in the first place unless I was sure the people I was teaching had already had some training in using the command line.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I have no idea how to tell Eclipse to redirect System.in to use a particular file (even though I've been using Eclipse for years now). But you could change the program to read from the file directly instead of System.in -- that's what I would have done in the first place unless I was sure the people I was teaching had already had some training in using the command line.


Probably a bit advanced for this forum but this one way how to redirect from eclipse:

Create an Ant target and launch it as "Run External" from Eclipse, here are the steps:

I have one input file to read from: res\in.txt and one for the output: res\out.txt
Create a build.xml with the targets you require (this is just an example):


In Eclipse go to: Run->External Tools->External Tools Configurations->Ant Build-> New Launch Configuration use the following configuration:

Section Main

Buildfile: ${workspace_loc:/Tests/build.xml}

Base Directory: ${workspace_loc:/Tests}

*Note: Tests is the name of my Eclipse project

Now you can run your project by clicking in the Run Extenal tool bar button and change the input or output files as you needed


Also if you are using Eclipse version 4.5 or higher
Run Configuration -> Common -> tick Allocate console (necessary for input), tick input file. In the textfield enter full path address to input file.
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randal Mortell wrote:In my class book it says I can receive this information from Scanner but doesn't go into detail on how that is possible.
I think some suggestions were over complicated a bit.

As far as I understand, you just need to read the file content using Scanner class.Please go through this tutorial and see if it is what you need.

[edit] fixed link
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras is correct; it is easy to read the file. Now, finding the file is usually harder. If you can cope with lots and lots of import declarations, I suggest you use a file chooser to find your file. The simplest way to do that is simply to open the chooser but it will start looking in some root folder. The link will tell you how to make it look in a particular directory, or even how to make it look for certain types of file.I can see at least one spelling error in that code which will stop it compiling, so check all the names of the methods carefully. The showXXXDialog methods take an argument which tells the JVM which display Component to appear in front of, but you haven't got any display Components. So null allows it to appear in front of nothing, which usually means centre of the screen. Warning: if nothing happens within 15 seconds, minimise all your other windows in case the dialogue is hiding behind something. The loop means that if you push the cancel button, the chooser will apeear again and again until you give it a file to choose. The showXXXDialog methods return an int to tell the JVM whether you pushed the OK button or the Cancel button.

Remember always to close a Scanner pointing to a file; there is a better way to do it than in the tutorial link: look here.
You should be able to work out what to do with the Scanner from the link Liutauras showed you. Whether you use the next() method of Scanner, or nextXXX() or nextLine depends on how you intend to use the contents of the file, and also whether you know the file's structure. Beware: using nextLine after nextAnythingElse can cause problems, but I know the solution

[edit]… and welcome to the Ranch again
 
Randal Mortell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the information and tutorials.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randal Mortell wrote:Thank you . . .
Our pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!