• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Read variables from text file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file with the following form:

2
d 6 1000
w 4 600


I setted this file as input file in my java project. Now, how can make java read as a variable:

1. The number in the first line
2. And in the other lines, three different variables: 1. The first letter 2. the second number 3. the third number (they are seoarated with space)

Please help me!
 
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to use the Scanner class's nextLine method to read lines from the file
and then  use the String class's split method to separate the tokens on that line.
The Integer class has a method to convert a String to an int
 
Marshal
Posts: 71103
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

A agree about a Scanner but I personally wouldn's use nextLine(). I would prefer the nextXXX() methods, of which there are about ten different kinds (if you include next()).
There isn't a Scanner method to read a single letter; that would be inconsistent with its design which divides the input into tokens. You can simply read the whole token, or use charAt(0) for the first letter.
 
Norm Radder
Rancher
Posts: 3968
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with using the nextXXX methods is that the new-line character is not recognized.  If there is any irregularity in tokens on a line in the input, nextXXX will get out of sync with the data.
If each line is more likely to contain related data, nextLine will make certain that like data is processed together and not across new-line boundaries.
 
Campbell Ritchie
Marshal
Posts: 71103
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:. . . the new-line character is not recognized.

It counts as whitespace, which is included in the default delimiter. You can of course change the delimiter.

If there is any irregularity in tokens on a line . . . .

There should be a specification of the format of that file (in this case one one‑letter word and two numbers except on the first line); the nextXXX() calls must conform to that specification. If there is irregularity in the inputs, I think, the user should be sending the file back as corrupted and ask it be repaired.
 
John Howards
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me exactly the code beacause I am so confused

 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by writing a program that uses the Scanner class to read all the lines  (using nextLine) or tokens (using next) in the file and print them.
There will need to be a loop that continues until all the data has been read,  The Scanner class has methods to detect the end of the data: (hasNextXXX)
When that  works then move on to parsing the data as required.
 
Popeye has his spinach. I have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic