Results 1 to 15 of 15
- 04-13-2011, 08:40 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
- 04-13-2011, 08:49 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
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
- 04-13-2011, 06:32 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
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 ?
- 04-13-2011, 06:58 PM #4
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? - JavaWorldLast edited by Jodokus; 04-13-2011 at 10:06 PM. Reason: spelling
- 04-13-2011, 07:14 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
instantiate StringBuilder
for each element in array
append to StringBuilder
set text of text field to StringBuilder.toString
- 04-13-2011, 09:47 PM #6
@ snotmare
I meant ArrayList<String>. StringBuilder is more like ArrayList<Character>. But I admit I've hardly any experience with StringBuilder.
- 04-14-2011, 01:08 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
Java Code:loop append text to stringbuilder end loop set text field text to stringbuilder toString method.
- 04-14-2011, 01:37 AM #8
Why nobody wants to use my ArrayList<String>?
- 04-14-2011, 01:40 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
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.
- 04-14-2011, 02:07 AM #10
@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 ;>) )
- 04-14-2011, 02:23 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
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 02:34 AM.
- 04-14-2011, 02:53 AM #12
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.
- 04-18-2011, 09:03 AM #13
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
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);
}
- 04-18-2011, 10:22 AM #14
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
- 04-18-2011, 01:05 PM #15
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
How to read file line by line with fixed number of characters
By trkece in forum New To JavaReplies: 1Last Post: 02-13-2011, 04:09 PM -
Stepping through a text file line by line
By evanlivingston in forum New To JavaReplies: 10Last Post: 01-29-2011, 05:30 AM -
Reverse search a Text file from last line to top line
By Jman85 in forum New To JavaReplies: 8Last Post: 12-28-2010, 03:24 PM -
how to place cursor at end of each line in JEditorPane
By prasad.vara in forum AWT / SwingReplies: 2Last Post: 10-25-2010, 01:32 PM -
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 11:18 AM
Bookmarks