I want to parse multiple strings into variables from a .txt file in java. What's the easiest way to do it? -
What's the best way to do this? Should I Use File Class and Scanner? I have never done this before and can not get a solid guide for online, so I thought I would ask here.
Edit:
I have a text file parsing 3 columns, the birth date of the first three ID name is then the actual data.
Edit (from code):
public void readFromFile {file} {try {System.out.println ("success .."); S = new scanner (file); BufferedReader input = new BufferedReader (new FileReader (file)); String jj = faucet; While ((jj = input.readLine ()) = null) {string [] word = jj.split ("\\ t"); String name = ""; String id = ""; String birthday = ""; For (int i = 3; i & lt; words.length; i + = 3) {id = word [i]; Name = word [i + 1]; Date of birth = word [i + 2]; Person p = new person (id, name, birthday); PeopleMap.put (p.id, p); Names.add (p); System.out.println ("New entry added to file:" + name + "\" t "+" id: "+ id +" \\ t "+" birthday "+ date of birth); }}} Hold (IOException e) {}}
The easiest way is to use a text file With your other comment format, it seems that lines are values separated by tabs. As a start, you will probably find it easy to use the scanner. In particular, Scanner.nextLine () is used to split the data into an array by using the string split ("\ t") in pairs (assuming the tabs are separate values).
Comments
Post a Comment