Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading in .txt files

 
Leah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to get my .txt file read into an array in my program. The file has ten lines which are tokenized into two parts with ":". The problem is that only the first line is read in and then repeated ten times instead of ten different lines being read. The same thing happens no matter what index number I use. How can I get it to read the next nine lines?? Any help is much appreciated 'cause I'm going insane!!!
Leah
PS My email addy doesn't work at the moment!
[This message has been edited by Leah (edited September 25, 2000).]
 
Eric Edwards
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leah:
Hi,
I am trying to get my .txt file read into an array in my program. The file has ten lines which are tokenized into two parts with ":". The problem is that only the first line is read in and then repeated ten times instead of ten different lines being read. The same thing happens no matter what index number I use. How can I get it to read the next nine lines?? Any help is much appreciated 'cause I'm going insane!!!
Leah
PS My email addy doesn't work at the moment!
[This message has been edited by Leah (edited September 25, 2000).]

I didn't put mine into an array, but it reads in the entire text file, line by line, and manipulates the data. See if you can adapt this to your needs.
import java.io.*;
import java.util.*;
import java.text.*;
public class NewFormatReport
{
String item = " ";
String s = " ";
int quantity = 0;
double price = 0.00;
double value = 0.00;

public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new
FileReader("C:\\price.dat"));
NewFormatReport fr = new NewFormatReport();
fr.readData(in);
}
catch(IOException e)
{
System.out.print("Error: " + e);
System.exit(1);
}
}
public void readData(BufferedReader is) throws IOException
{
while ((s = is.readLine()) != null)
{
StringTokenizer t = new StringTokenizer(s, ",");
item = t.nextToken();
quantity = Integer.parseInt(t.nextToken());
price = Double.parseDouble(t.nextToken());
value = quantity * price;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String fx = nf.format(value);
System.out.println(item + " " + quantity + " " + price + " " + fx);
}
}
}

 
Vim Win
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leah,
You can use BufferedReader to read the lines from the text like this :
BufferedReader in
= new BufferedReader(new FileReader(filename));
This will create a buffer from wherein which you can use
in.readLine() to extract line by line as Strings (which can be stored in an array of strings). Then stored strings can be tokenized using StringTokenizer. This should work fine and I have tried doing like that.
Bye.
JVRN.
 
Leah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Eric and JVRN! I'll give those a whirl. The sooner I can finish this project the better!!
Leah
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic