hi all,
I want to know how to place each line of a file into a vector.
Any help is would be great.
Printable View
hi all,
I want to know how to place each line of a file into a vector.
Any help is would be great.
Read a line from the file, put it in a collection. Continue until end of file.
Code:loop
read line
add line to vector
end loop
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 ?
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
instantiate StringBuilder
for each element in array
append to StringBuilder
set text of text field to StringBuilder.toString
@ snotmare
I meant ArrayList<String>. StringBuilder is more like ArrayList<Character>. But I admit I've hardly any experience with StringBuilder.
Code:loop
append text to stringbuilder
end loop
set text field text to stringbuilder toString method.
Why nobody wants to use my ArrayList<String>?
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.
@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 ;>) )
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.
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.
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);
}
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
thanks a lot .. i was able to rectify the error