2 Replies - 566 Views - Last Post: 11 June 2012 - 05:59 AM Rate Topic: -----

#1 ColourFulife  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-June 12

how to insert txt into arraylist

Posted 11 June 2012 - 12:06 AM

hi,all..i have trouble to insert my txt data into arraylist. my data is in character and i need to calculate that data.
The example of data is accccctttgaggtgtaggtca
i already read the file using bufferedreader and now i need to insert it into arraylist.

here is my code:

public class Main {
    public static void main(String[] args) {
        
        ArrayList Main = new ArrayList();
        String line = null;
        File file = new File("rs191872612 _ 702.fasta");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
 
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;
 
            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text)
                        .append(System.getProperty(
                                "line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        // show file contents here
        System.out.println(contents.toString());
        

    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to insert txt into arraylist

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: how to insert txt into arraylist

Posted 11 June 2012 - 05:35 AM

Hey I haven't wrote in Java in a few years but would it not just be
main.add(Text) 
but i could be wrong. Here is a web site that explains java arraylists.

http://www.java-samp...?tutorialid=234

This post has been edited by m_wylie85: 11 June 2012 - 05:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: how to insert txt into arraylist

Posted 11 June 2012 - 05:59 AM

m_wylie85 is correct, however remember that Java is case sensitive so 'main' is very different from 'Main'.

But in a more pressing matter, you have no reason to have an arraylist called Main. Not only in Java are local variable camel cased - 'main', but you really shouldn't be naming a variable 'main' at all. It's a very confusing name considering that it's in the 'main' method. Name it something more descriptive.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1