Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Inserting In a List

Reply
Thread Tools

Inserting In a List

 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      04-02-2013
Dear Group,

I am taking out the files in my desktop folder, as,

File folder = new File("C:\\Users\\subhabrata\\Desktop");

Next, I am trying to take them out one by one as using for loop and name
of each file is converted to String,

for( File name :folder.listFiles()){
String s1=name.toString();
System.out.println("####"+s1);
System.out.print( name );
}

Till this there is no issue, now I am trying to insert name of all the files in an array, generally it can be done, as,

ArrayList<String> myList = new ArrayList<String>();
myList.add(s1);

But the problem I am facing is, if I put it as,

for( File name :folder.listFiles()){
String s1=name.toString();
System.out.println("####"+s1);
System.out.print( name );
ArrayList<String> myList = new ArrayList<String>();
myList.add(s1);
}

I am getting name of files individually but I want to see them as the whole bunch like,

myList=["string1","string2","string3",...."StringN"]

My aim is to check the names then in another for loop and if match is not found with some defined name update the list.

I am slightly new in Java so if anyone can kindly suggest how I should address it.

Regards,
Subhabrata.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-02-2013
On Tue, 2 Apr 2013 03:11:53 -0700 (PDT), http://www.velocityreviews.com/forums/(E-Mail Removed)
wrote, quoted or indirectly quoted someone who said :

>I am getting name of files individually but I want to see them as the whole bunch like,
>
>myList=["string1","string2","string3",...."StringN"]


you will need a loop that runs over the list and builds the combined
string.
--
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes
that went into it. Something was more important than sensory pleasure --
nobody would invent a chair or dish that smelled bad or that made horrible
noises -- why were motors invented noisy? How could they possibly be
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner.
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural
 
Reply With Quote
 
 
 
 
Arved Sandstrom
Guest
Posts: n/a
 
      04-02-2013
On 04/02/2013 07:11 AM, (E-Mail Removed) wrote:
> Dear Group,
>
> I am taking out the files in my desktop folder, as,
>
> File folder = new File("C:\\Users\\subhabrata\\Desktop");
>
> Next, I am trying to take them out one by one as using for loop and name
> of each file is converted to String,
>
> for( File name :folder.listFiles()){
> String s1=name.toString();
> System.out.println("####"+s1);
> System.out.print( name );
> }
>
> Till this there is no issue, now I am trying to insert name of all the files in an array, generally it can be done, as,
>
> ArrayList<String> myList = new ArrayList<String>();
> myList.add(s1);
>
> But the problem I am facing is, if I put it as,
>
> for( File name :folder.listFiles()){
> String s1=name.toString();
> System.out.println("####"+s1);
> System.out.print( name );
> ArrayList<String> myList = new ArrayList<String>();
> myList.add(s1);
> }
>
> I am getting name of files individually but I want to see them as the whole bunch like,
>
> myList=["string1","string2","string3",...."StringN"]
>
> My aim is to check the names then in another for loop and if match is not found with some defined name update the list.
>
> I am slightly new in Java so if anyone can kindly suggest how I should address it.
>
> Regards,
> Subhabrata.
>

This comes up every so often. Guava and Apache StringUtils have
join-type methods for concatenating list elements with a delimiter of
your choice.

Fact is, though, if you just want a readable debugging print of your
List, did you not simply try

System.out.println(myList);

AHS
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-02-2013
(E-Mail Removed) writes:
>for( File name :folder.listFiles()){
>String s1=name.toString();
>System.out.println("####"+s1);
>System.out.print( name );
>ArrayList<String> myList = new ArrayList<String>();
> myList.add(s1);
>}


I see two answers in this NG both not mentioning what is
sticking out like a sore thumb from this code: The line

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

is within the for-loop, while it obviously should appear in
front of it. (There might be other issues I am not aware of,
but this one I saw immediately.)

 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      04-02-2013
On 04/02/2013 08:40 AM, Stefan Ram wrote:
> (E-Mail Removed) writes:
>> for( File name :folder.listFiles()){
>> String s1=name.toString();
>> System.out.println("####"+s1);
>> System.out.print( name );
>> ArrayList<String> myList = new ArrayList<String>();
>> myList.add(s1);
>> }

>
> I see two answers in this NG both not mentioning what is
> sticking out like a sore thumb from this code: The line
>
> ArrayList<String> myList = new ArrayList<String>();
>
> is within the for-loop, while it obviously should appear in
> front of it. (There might be other issues I am not aware of,
> but this one I saw immediately.)
>

You've got a point there. Well, many pairs of eyes in a review is
always better than one.

AHS
 
Reply With Quote
 
Joerg Meier
Guest
Posts: n/a
 
      04-02-2013
On Tue, 2 Apr 2013 03:11:53 -0700 (PDT), (E-Mail Removed) wrote:

> I am taking out the files in my desktop folder, as,


> [...]


> I am getting name of files individually but I want to see them as the whole bunch like,


> myList=["string1","string2","string3",...."StringN"]


You need to move the list assignment out of the loop, like so:

final File folder = new File("G:\\Media\\TV Show");
final ArrayList<String> myList = new ArrayList<String>();
for (final File name : folder.listFiles()) {
final String s1 = name.toString();
System.out.println("####" + s1);
System.out.print(name);
myList.add(s1);
}

Otherwise, you create a new, empty list for every file name, and delete the
previous one.

You can then print out your list like so:

System.out.println(myList);

Which is really short hand for the more generic version:

System.out.println(Arrays.toString(myList.toArray( )));

> My aim is to check the names then in another for loop and if match is not found with some defined name update the list.


It's really not clear to me what you mean by that. You will need to restate
it or explain it in more detail.

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
 
Reply With Quote
 
Steven Simpson
Guest
Posts: n/a
 
      04-02-2013
On 02/04/13 11:11, (E-Mail Removed) wrote:
> for( File name :folder.listFiles()){
> String s1=name.toString();
> System.out.println("####"+s1);
> System.out.print( name );
> ArrayList<String> myList = new ArrayList<String>();
> myList.add(s1);
> }


(Stefan has already noted that the list must be created before the
loop. What you have here is one list created per item in the folder,
and each of these lists is discarded.)

> I am getting name of files individually but I want to see them as the whole bunch like,
>
> myList=["string1","string2","string3",...."StringN"]


(Arved and Roedy have mentioned some ways to get this, though I think
you are after something different. Read on...)

> My aim is to check the names then in another for loop and if match is not found with some defined name update the list.


Your loop seems to have two purposes: 1) display all items in a certain
format; 2) build an internal List object.

Perhaps I've misinterpreted your aim, but your wish to generate a list
of names in a string with a given format leads me to think you're under
the impression that you need to display the names somehow in order to
perform the check you mention. This is not necessary (nor useful,
except for purposes of diagnosing problems). You should do the check on
the List object, not on the formatted String.

Firstly, if you must create a List, there's a quick way to create a
fixed-size one from the array provided by listFiles():

List<File> files = Arrays.asList(folder.listFiles());

Note that this is a list of File, not String; the code below does not
need a List<String>. If you really do need a List<String>, you could do
something similar with folder.list() instead of folder.listFiles().

Next, you could scan the List for a matching file. Loop over it, and
record when you find it:

boolean found = false;
for (File file : files) {
if (file.getName().equals("foo")) {
found = true;
break; // No need to go further.
}
}

However, unless you need to keep the List around for other purposes, you
could just use the array directly from listFiles():

boolean found = false;
for (File file : folder.listFiles()) {
if (file.getName().equals("foo")) {
found = true;
break; // No need to go further.
}
}

But it would be even simpler, if you're just looking for a particular
leafname (like "foo"), rather than a leafname matching a given pattern
(like anything beginning with "foo"), to create a File object
representing that name, and checking whether the file exists:

File candidate = new File(folder, "foo");
boolean found = candidate.exists();

Note that, confusingly, java.io.File represents a file name, not a file,
so 'new File(...)' does not actually create a file.

> and if match is not found with some defined name update the list.


Not sure how you intend to update the List. If it's supposed to
represent the list of files in the folder, updating the List won't
create the missing file. Perhaps you want the rest of the program to
suppose it exists; I don't know.

If you really do want to update the list, you'll have to create it as
one you can resize:

List<File> files = new ArrayList<File>(Arrays.asList(folder.listFiles())) ;

Read this inside out:

1. listFiles(): Get the list of filenames as an array.
2. asList(): Create a 'List view' of the array.
3. new ArrayList(): Create a modifiable list with the same contents as
the array.




--
ss at comp dot lancs dot ac dot uk

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-02-2013
On Tue, 2 Apr 2013 03:11:53 -0700 (PDT), (E-Mail Removed)
wrote, quoted or indirectly quoted someone who said :

>myList=["string1","string2","string3",...."StringN"]



FastCat, my replacement for StringBuilder has a toCommaList that
exports a comma-separated set of strings in one long string.

see http://mindprod.com/products.html#FASTCAT
--
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes
that went into it. Something was more important than sensory pleasure --
nobody would invent a chair or dish that smelled bad or that made horrible
noises -- why were motors invented noisy? How could they possibly be
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner.
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-02-2013
(E-Mail Removed) wrote:
> I am taking out the files in my desktop folder, as,


"Taking out" I understood initially as "deleting", but I gather you mean "printing" or
"displaying".


> File folder = new File("C:\\Users\\subhabrata\\Desktop");


You can also use forward slashes.

> Next, I am trying to take them out one by one as using for loop and name
> of each file is converted to String,


If you indented properly your scope issues would be easier to see. The first block
doesn't have any, but the one further down does.

> for( File name :folder.listFiles()){
> String s1=name.toString();
> System.out.println("####"+s1);
> System.out.print( name );
> }


I should think you'd use 'getPath()' or 'getName()' on the 'name' instance.

> Till this there is no issue, now I am trying to insert name of all the files
> in an array, generally it can be done, as,


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


That is not an array, that is a 'List'.

> myList.add(s1);


> But the problem I am facing is, if I put it as,


Again, proper indentation would reveal any scope issues.

> for( File name :folder.listFiles()){
> String s1=name.toString();
> System.out.println("####"+s1);
> System.out.print( name );
> ArrayList<String> myList = new ArrayList<String>();
> myList.add(s1);
> }


Now here you have a scope issue. You create a new 'List' in each iteration, add only
one item to it, then throw it away. Nowhere do you create a 'List' that survives one
loop iteration.

> I am getting name of files individually but I want to see them as the whole bunch like,


You show no code that demonstrates how you determine what "you" are "getting".

Please follow
http://sscce.org/

> myList=["string1","string2","string3",...."StringN"]


In that case, declare your 'List' in a scope that survives the loop.

> My aim is to check the names then in another for loop and if match
> is not found with some defined name update the list.


Study "scope" and "access" in Java.

> I am slightly new in Java so if anyone can kindly suggest how I should address it.


What is "slightly"?

--
Lew
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      04-02-2013
On 4/2/2013 10:46 AM, Lew wrote:

> (E-Mail Removed) wrote:
>> I am taking out the files in my desktop folder, as,


> "Taking out" I understood initially as "deleting", but I gather you mean "printing" or
> "displaying".


I think he means "reading." As in he reads the file entries with these
lines of code:

>> File folder = new File("C:\\Users\\subhabrata\\Desktop");
>> ... folder.listFiles()...


>
>> Next, I am trying to take them out one by one as using for loop and name
>> of each file is converted to String,


Again, "take... out" for reading from an array.

To subhaba: "Iterate over" is often used to describe reading from an
array in a loop (for-loop or other loop).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting into DB table with date from Generic List =?Utf-8?B?U3Jpbmk=?= ASP .Net 0 11-07-2006 04:33 AM
inserting into a list John Salerno Python 15 03-08-2006 06:02 AM
Linked list inserting items from two different funcion Kay C++ 1 09-03-2004 03:48 PM
Urgent:Inserting records from datagrid through drop down list Muhammad Usman ASP .Net 1 10-16-2003 01:39 PM
Inserting an empty element in a list (STL) Massimiliano Alberti C++ 3 09-24-2003 05:48 PM



Advertisments