X-Received: by 10.224.110.68 with SMTP id m4mr10931547qap.2.1364924772835; Tue, 02 Apr 2013 10:46:12 -0700 (PDT) X-Received: by 10.50.140.65 with SMTP id re1mr1482206igb.13.1364924772792; Tue, 02 Apr 2013 10:46:12 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!t2no30475303qal.0!news-out.google.com!v17ni6816qad.0!nntp.google.com!ca1no21065395qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Tue, 2 Apr 2013 10:46:12 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T NNTP-Posting-Host: 69.28.149.29 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <5bbcf921-bf9c-44a7-a604-b714ca62bb96@googlegroups.com> Subject: Re: Inserting In a List From: Lew Injection-Date: Tue, 02 Apr 2013 17:46:12 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com comp.lang.java.programmer:23186 subhaba...@gmail.com 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 myList = new ArrayList(); 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 myList = new ArrayList(); > 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