import java.io.*; import java.util.Formatter; import java.util.Scanner; import java.util.Vector; public class read_file { private Formatter make_file; private Vector<String>names; private int counter; private Vector<Integer>age; void FileInput() { File input = new File("example.txt"); if(!input.exists()) { try{make_file = new Formatter("example.txt");System.out.println("A file had to be made, named "+input);} catch(IOException e){System.out.println("File was unable to be made");} } else { try { Scanner reader = new Scanner(new FileInputStream(input)); while(reader.hasNext()) { names.add(reader.nextLine()); age.add(reader.nextInt()); counter++; } }catch(Exception e){System.out.println("The file could not be read");} } } void PrintFile() { for(int i=0;i<counter;i++){System.out.println(names.get(i)+" "+age.get(i));} } public static void main(String[] args) { read_file r = new read_file(); r.FileInput(); r.PrintFile(); } }
I am playing with different way to take input from file it is not working. keeps popping up. The file is being created though. Is a vector a good way to handle this? I read some bad things about vectors in Java but I used them all the time in C++. What is the deal with that?
The files look like:
name age
name age
name age
etc........