Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008, 07:45 PM
Member
 
Join Date: Mar 2008
Posts: 4
szimme101 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-24-2008, 05:28 PM
Member
 
Join Date: Mar 2008
Posts: 3
Javashak is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-24-2008, 05:28 PM
Member
 
Join Date: Mar 2008
Posts: 3
Javashak is on a distinguished road
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() {

}


}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-07-2008, 08:02 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 240
DonCash will become famous soon enoughDonCash will become famous soon enough
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:

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:

Code:
for (int a = 0; a < myarray.length; a++){ System.out.println(myarray[a]); }
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 04-07-2008 at 08:04 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you read from a file, and then store the info in an array? szimme101 New To Java 5 07-30-2008 11:30 AM
How to store property file into key value pair Java Tip java.util 0 04-05-2008 12:16 PM
store file kazitula Java Applets 0 02-17-2008 11:45 PM
questions about using array to store profile hien_NU New To Java 6 01-08-2008 07:03 AM


All times are GMT +3. The time now is 11:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org