Results 1 to 4 of 4
  1. #1
    szimme101 is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Question How would you get information from a file and then store it in an array?

    Here is my problem:

    I am going to have to use a bufferedReader to read a file containing information on 10 vehicles. And after I read that file I want to store the info in Vehicle [] V.


    Where should I start?

    Thanks

    Steve

  2. #2
    Javashak is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    Hey steve,
    following is some code that read a text file and stores them in an array.Its not a buffered reader but im sure itl give you some idea.

  3. #3
    Javashak is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;


    public class FileIO extends JFrame implements ActionListener{

    private int lineCount;
    private String[] A;
    private String[] nodeLine = new String[2];
    private int[] L;
    private int[] R;

    private JTextArea display=new JTextArea();
    private JButton read=new JButton("Read From File");

    private JTextField nameField=new JTextField(20);
    private JLabel prompt=new JLabel("Filename:",JLabel.RIGHT);
    private JPanel commands=new JPanel();

    public FileIO()
    {super("Read data from file");
    read.addActionListener(this);

    commands.setLayout(new GridLayout(2,2,1,1));
    commands.add(prompt);
    commands.add(read);
    commands.add(nameField);

    //for graphing

    display.setLineWrap(true);
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add("North",commands);
    this.getContentPane().add(new JScrollPane(display));
    this.getContentPane().add("Center",display);
    }
    public void readTextFile(JTextArea display,String fileName) {

    try {BufferedReader coStream=new BufferedReader(new FileReader(fileName));

    String line=coStream.readLine();
    int i = 1;
    lineCount = 1;
    while(line!=null) {
    line=coStream.readLine();
    lineCount = lineCount + 1;
    i = i + 1;
    }
    coStream.close();
    }

    catch(FileNotFoundException e)
    {display.setText("IO ERROR:File NOT Found:" + fileName + "\n");

    e.printStackTrace();
    }
    catch(IOException e)
    {
    display.setText("IO ERROR:" +e.getMessage()+"\n");
    e.printStackTrace();
    }

    A = new String[lineCount];
    L = new int[lineCount];
    R = new int[lineCount];

    try {BufferedReader inStream=new BufferedReader(new FileReader(fileName));

    String line=inStream.readLine();
    A[0] = line;
    int i = 1;
    while(line!=null) {
    display.append(line+"\n");
    line=inStream.readLine();
    A[i] = line;
    i = i + 1;
    }
    inStream.close();
    }

    catch(FileNotFoundException e)
    {display.setText("IO ERROR:File NOT Found:" + fileName + "\n");

    e.printStackTrace();
    }
    catch(IOException e)
    {
    display.setText("IO ERROR:" +e.getMessage()+"\n");
    e.printStackTrace();
    }

    for (int i = 0; i < (lineCount-1); i++) {
    nodeLine = A[i].split(" ");
    if (nodeLine[0].equals("-")) {
    nodeLine[0] = "0";
    }
    if (nodeLine[1].equals("-")) {
    nodeLine[1] = "0";
    }
    L[i] = Integer.parseInt(nodeLine[0]);
    R[i] = Integer.parseInt(nodeLine[1]);
    }

    printStuff();
    calculate();

    }
    public void actionPerformed(ActionEvent evt)
    {
    String fileName=nameField.getText();
    if(evt.getSource()==read)
    {
    display.setText("");

    readTextFile(display, fileName);

    }
    else System.out.println("Hello World");
    }

    public void printStuff() {
    for (int i = 0; i < (lineCount-1); i++) {
    System.out.println(L[i]);
    }
    System.out.println("---");
    for (int i = 0; i < (lineCount-1); i++) {
    System.out.println(R[i]);
    }
    }

    public void calculate() {

    }


    }

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Thumbs up

    Hello JavaShak. Thanks for your input. In future - Please put the [code] [/ code] tags (minus the space after /) around your code. This makes it alot easier for people to read.

    Hey Szimme101, take a look at this code below. This is exactly what you need to make it work:

    Java Code:
    FileInputStream in = new FileInputStream("yourfile.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    		
    String[] myarray;
    myarray = new String[9];
    		
    for (int i = 0; i < myarray.length; i++){
    myarray[i] = br.readLine();
    }
    		
    in.close();
    You can print out all the array values by using this:

    Java Code:
    for (int a = 0; a < myarray.length; a++){
    System.out.println(myarray[a]);
    }
    Last edited by DonCash; 04-07-2008 at 06:04 PM.
    Did this post help you? Please me! :cool:

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  2. How to store property file into key value pair
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-05-2008, 10:16 AM
  3. store file
    By kazitula in forum Java Applets
    Replies: 0
    Last Post: 02-17-2008, 09:45 PM
  4. questions about using array to store profile
    By hien_NU in forum New To Java
    Replies: 6
    Last Post: 01-08-2008, 05:03 AM

Posting Permissions

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