aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Reading a text file and storing the values in appropriate places Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reading a text file and storing the values in appropriate places " Watch "Reading a text file and storing the values in appropriate places " New topic
Author

Reading a text file and storing the values in appropriate places

leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
Hi all,
I am a complete beginner in JAVA and now have to do this for a class.
Task is to read a text file and store the values in appropriate places. Its hard to describe the assignment, so all i want to know as a starting step is once you have read a file, how do you do basic things like using the text file to calculate averages.
For example if a line from the text file is
12/1/2009 13 444 43
this line includes a date and three different numbers.
How do i break these apart and calculate the average of these three numbers and set the average to that particular date. So when a user selects that date from a dropdown box the average should be displayed.
I am just soo Lost and I just want someone to explain to me the code and just a starting tip.

So far i have got:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class bufferedReaderPractice {
public static void main(String[] a) {

FileReader fr;
try {
String thisLine = null;
fr = new FileReader (new File("calc.txt"));
BufferedReader br = new BufferedReader (fr);
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();



}
br.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I'd really appreciate you guys answering, it would mean a lot!! Thank you in advance!!
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

You already have the data in line variable, right? So only thing you need is to split it. Look into split method of String class, and next time please use code tags while you post you code.



Swastik
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
Oh my god, thank you soo much for answering, I will def. do so except i don't know how to do it
But Thank you anyways!
what does the "for" statement do....i don't understand.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

I believe you know about loops right? while,do while and for. Now in this example you are splitting the variable line and storing it in the array data. The for loop iterates through the data array and prints the elements.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
Oh Okay I understand. But with what are we saying that this my line is being splitted into three parts, I mean the date as one part and the other three numbers as other parts. Also when I Run this I get

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\f0\fs20 4/2/2009,5,4,3\par
{\*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\f0\fs20 3/1/2008,1,3,4\par

Is this supposed to be the correct output?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

leila la wrote:But with what are we saying that this my line is being splitted into three parts.




Without looking the contents of your file it's tough to say whether you are getting the correct output or not.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
First off, Thank you soo much for the quick reply! Means a lot!
It is ok, I got confused with something else.

Now If i wanted to do calculations with these numbers how abouts would i go doing it.

And can i create an array for each part of the text file and store like first number in one array and stuff like that?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

I guess you should first proceed. We are always here to help you.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
Ok great I shall attempt to do something, can you just tell me how to reply and post my code in a way like you did? Im new to this site..new to everything technological lol
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

When you post a code, you will find a code button on the top. Click that and and place your code inside that.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6



your code should be with that code /code thing.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
I think thats what i did, it looks like java code to me..hmm
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

Thank you.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
No problem, will i get a reply to my question...pretty pleaaaaaaaaaaaaase?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a text file and storing the values in appropriate places