Results 1 to 15 of 15
  1. #1
    su5
    su5 is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default how to place each line of a file into a vector

    hi all,
    I want to know how to place each line of a file into a vector.
    Any help is would be great.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Read a line from the file, put it in a collection. Continue until end of file.

    Java Code:
    loop
      read line
      add line to vector
    end loop

  3. #3
    su5
    su5 is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    thanks! I was able to achieve the result.
    The next thing that i want to do is to put the contents of the vector into a text field . Is it possible to do it using a for loop ?

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Small remark: most of the time an ArrayList is better then a Vector, unless it has to be threadsafe.
    Vector or ArrayList -- which is better? - JavaWorld
    Last edited by Jodokus; 04-13-2011 at 09:06 PM. Reason: spelling

  5. #5
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    instantiate StringBuilder

    for each element in array
    append to StringBuilder

    set text of text field to StringBuilder.toString

  6. #6
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @ snotmare
    I meant ArrayList<String>. StringBuilder is more like ArrayList<Character>. But I admit I've hardly any experience with StringBuilder.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    loop
      append text to stringbuilder
    end loop
    set text field text to stringbuilder toString method.

  8. #8
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Why nobody wants to use my ArrayList<String>?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    While an array list of strings is better than a vector, the op asked about a vector, you pointed out that's array lists better. However, the approach is nearly identical with array lists and vectors.

    If you stored it in an arraylist instead of a vector you would still want to use a string builder which has all the elements of the array list appended to it to set the text field with.

  10. #10
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @Sunde
    The original post was about all lines of a file in a vector/list -> a list of strings.
    Next question was contents of a vector/list in a textfield. This suggests a very crowded textfield containing concatenated strings. Sounds like a bad choice of component, but can be reached with all our options: vectors, lists, builders you name it. I've never used this builder but lived very happy without it and able to stuff all textfields I met with text.

    (This becomes a case of honour ;>) )

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are right, an arraylist is better than a vector and a text field may be a poor choice, perhaps he meant a text area, in this case I would not suggest the use of a string builder because text areas allow you to append text. However, in a text field the more efficient approach is to use a builder as opposed to string concatenation, I'm sure on most computers nowadays the difference isn't noticeable, but appending a string to a stringbuilder takes less instructions than concatenating two strings.

    I believe you can view the differences of the two types of concatenation by making one program which concatenates two strings and one which uses a stringbuilder to perform the concatenation. I believe the command is javap to produce(i think it's assembly) and see the difference in instructions used.
    Last edited by sunde887; 04-14-2011 at 01:34 AM.

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Yeah I prefer ArrayList when reading from a file so I can then deal with each line by itself. StringBuilder is if you don't need to deal with each line specifically.

  13. #13
    su5
    su5 is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default unable to place the entire contents of the vector in a JList

    I was able to read from the file and place it in a vector.
    The next thing that i wanted to do was to place the vector contents onto a jlist.But i'm getting only the last element of the vector.
    I'm unable to debug the error.

    the code is as follows :

    private JList packetsList = null;
    public SenderUi throws Exception{
    ..
    vector vector = getInput();
    ..
    }

    public Vector getInput() throws Exception {
    FileInputStream fstream = new FileInputStream("C:\\b.txt");
    DataInputStream is = new DataInputStream(fstream);
    BufferedReader br= new BufferedReader(new InputStreamReader(is));
    String strLine;
    while((strLine = br.readLine()) != null)
    {
    packetVector = new Vector();
    packetVector.addElement(strLine);
    System.out.println(packetVector);
    }
    }

    public Jpanel getListPanel(Vector vector) throws Exception {
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1,1));
    packetsList = new JList(vector);
    JScrollPane scrollPane = new JScrollPane(packetsList);
    panel.add(scrollPane);
    }

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Well, every time through the loop you're creating a new Vector, so what did you expect?

    And please format your code with code tags to retain indenting and legibility.

    db

  15. #15
    su5
    su5 is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  2. Stepping through a text file line by line
    By evanlivingston in forum New To Java
    Replies: 10
    Last Post: 01-29-2011, 04:30 AM
  3. Replies: 8
    Last Post: 12-28-2010, 02:24 PM
  4. how to place cursor at end of each line in JEditorPane
    By prasad.vara in forum AWT / Swing
    Replies: 2
    Last Post: 10-25-2010, 12:32 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •