Monday, May 26, 2014

populate an array from txt file or scanner




hello everyone,




I have a simple txt file, each line simply containing 1 word.




I would like each work to represent an index of the array




im having some difficulty populating an array from either a txt file or a scanner.




i seem to be able to fill the scanner so to speak with the contents of the text file but not the array. I don't know how to syntax it




ill attach my file an also paste my code below.




I would appreciate some help.




thanks




import java.io.File;



import java.io.FileNotFoundException;



import java.util.Scanner;



import java.util.Arrays;




public class ReadFile {



public static void main(String[] args) {




// Location of file to read



File file = new File("dictionary.txt");




try {



Scanner sc = new Scanner(file);




int count = 0;



while (sc.hasNextLine()) {



count++;



sc.nextLine();



}



System.out.println(count);




while (sc.hasNextLine()) {



String line = sc.nextLine();



System.out.println(line);




}



sc.close();



} catch (FileNotFoundException e) {



e.printStackTrace();



}



}



}







No comments:

Post a Comment