Results 1 to 5 of 5

Thread: Vector create

  1. #1
    Warren is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Vector create

    how can i take every line of an input file and put in a Vector?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    public class FileToVector {
        private JScrollPane getContent() {
            Vector<String> vector = new Vector<String>();
            JTextArea textArea = new JTextArea();
            File file = new File("FileToVector.java");
            Scanner scanner = null;
            try {
                scanner = new Scanner(file);
            } catch(FileNotFoundException e) {
                System.out.println("file not found: " + e.getMessage());
            }
            while(scanner.hasNextLine()) {
                String line = scanner.nextLine();
                vector.addElement(line);
            }
            scanner.close();
            for(int j = 0; j < vector.size(); j++)
                textArea.append(vector.get(j) + "\n");
            return new JScrollPane(textArea);
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new FileToVector().getContent());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    Warren is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    :o sorry I meant a .txt file not a .java. (by the way i dunno what JFrame, JTextArea, JScrollPane are, are they tools used a lot?)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    sorry I meant a .txt file not a .java.
    Substitute a text file name in place of the java file. It should work okay.
    (by the way i dunno what JFrame, JTextArea, JScrollPane are, are they tools used a lot?)
    Yes.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    YNC, your question is important, so please ask it in a new thread. Thanks for your cooperation. I'm locking this long-dead thread.

Similar Threads

  1. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 07:33 PM
  2. Declaring Vector
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 08:14 PM
  3. Array or Vector
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 06:19 PM
  4. array vs Vector
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 07:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •