¿Cómo puede elegir una línea de un archivo de texto y transformarla en un objeto de matriz?

Okay this is code and I need to somehow take a line from the textfile and transform into an array object. like p[0] = "asdasdasd"

public class Patient2 {
    public static void main(String args[])

        int field = 0;
        String repeat = "n";
        String repeat1 = "y";
        Scanner keyIn = new Scanner(System.in);

        // FILE I/O
              // Open the file that is the first 
              // command line parameter
              FileInputStream fstream = new FileInputStream("Patient.txt");
              BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
              String strLine;
              //Read File Line By Line
              while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
              System.out.println (strLine);
              //Close the input stream
                }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
        ArrayList<Patient1> patients=new ArrayList<Patient1>();
        Patient1 p =new Patient1();
        //set value to the patient object

preguntado el 01 de febrero de 12 a las 04:02

Can you be more specific as to what is the pattern of the line which you want to transform into an array and where you want to set it ? -

2 Respuestas

Instead of printing it to console you can add it to List<String>

List<String> lines = new ArrayList<String>();
while ((strLine = br.readLine()) != null)   {
   // Print the content on the console
   System.out.println (strLine);

Nota: your code can be much cleaner, you can handle closing resources in finally

Respondido 01 Feb 12, 08:02

Okay I need to transform the string into an array like p[0] = ""; - user1181810

You never know what is the length of text file so better to use List and then you can covert this list into array - Jmj

Solo usa un ArrayList<String> con add(strline);
y use toArray(new String []) to get the array after input stream has been closed.

 ArrayList<String> list = new ArrayList<String>();

 while ((strLine = br.readLine()) != null) {

 String [] s = list.toArray(new String []);

Respondido 01 Feb 12, 08:02

So initialize it? :p Make sure it's a data field / class variable. - rtheunissen

poner ArrayList<String> list = new ArrayList<String>(); between your class declaration and your main método. - rtheunissen

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.