PDA

Click to See Complete Forum and Search --> : Beginner needs help with file I/O...


jrmorgan56
Dec 1st, 2002, 04:48 PM
Lets say I have a data file delimited with semicolons like this:

item1description;12.50;18.99;10
item2description;9.25;12.00;35
item3description;19.60;25.75;4

I am wanting to be able to read each line and take from it, for example, the third field. What technique should I try using? I can use BufferedReader and the readLine()....I try to use loops nested in a for loop to search each character for the ';' but really dont know where to go from there. I can print the entire file out, but what should I be doing to get only certain fields? Can someone out there point me in a general direction (just give me advice...still want to try and figure this out myself....part of the learning process) ? If I didn't provide enough information for anyone to help me, just let me know and thanks in advance...

:)

crptcblade
Dec 1st, 2002, 05:24 PM
I would recommend the StringTokenizer class.

import java.util.*;
import java.io.*;

class Test
{
public static void main(String[] args) throws Exception
{
try
{
String s;
BufferedReader bf = new BufferedReader(new FileReader("c:\\test.txt"));

while ((s=bf.readLine())!=null)
{
String sRet = getSubItem(s,2);

if (sRet!=null) System.out.println(sRet);
}

bf.close();
}
catch(Exception e) { System.out.println(e.toString()); }
}

private static String getSubItem(String s, int index)
{
StringTokenizer st = new StringTokenizer(s,";");
int x = 0;

while(st.hasMoreTokens())
{
String sTemp = st.nextToken();

if (x==index)
return sTemp;
else if (x<index)
x++;
else
break;
}

return null;
}
}


:)

jrmorgan56
Dec 1st, 2002, 07:36 PM
That works perfectly! THANK YOU!!!!!! I really appreciate your time in giving the code.

:D

CornedBee
Dec 2nd, 2002, 07:08 AM
Very nice, but main doesn't ever throw (all exceptions are caught by the try-catch block), so the declaration isn't necessary.

jrmorgan56
Dec 2nd, 2002, 08:58 AM
Even so, isn't that still considered good practice, putting the 'throws' in? At least, that is what they teach new computer science students....probably not true with professionals. Is that a matter of style vs. textbook approach?

crptcblade
Dec 2nd, 2002, 09:06 AM
Originally posted by CornedBee
Very nice, but main doesn't ever throw (all exceptions are caught by the try-catch block), so the declaration isn't necessary.

That's a typo. I cut 'n pasted the main from another file I had open, and forgot to remove the throws. You are right, it's totally unnecessary.

:)

CornedBee
Dec 2nd, 2002, 11:06 AM
I guessed something along those lines.

Originally posted by jrmorgan56
Even so, isn't that still considered good practice, putting the 'throws' in? At least, that is what they teach new computer science students....probably not true with professionals. Is that a matter of style vs. textbook approach?

No. It is considered good practice (and it is necessary) to explicitly declare the throws for exceptions that might be thrown and don't derive from RuntimeException. It is also considered good practice to declare the throws for exceptions that derive from RuntimeException if they are passed on from a function that explicitly mentions they could be thrown.
But it is not considered good practice to declare that a functions throws an exception when it doesn't.

pigpen
Dec 15th, 2002, 08:21 PM
hey folks, i'm a newbie here, and this place looks very helpul. im also a JAVA beginner and i have a question pertaining to the code above. what if when you read in the datafile, you want the data so that you can use one or more of the fields in another class? i guess you would use an array, but how would that go? i have a datafile that's delimited with commas, it has text values, int values, and double values that make up one record, much like the datafile described by jrmorgan56, and i need to multiply 2 of the fields together for each record, and then sum all that up. did anyone understand all that jibberish? i guess im stuck on how to create and access the array correctly from another class.

jrmorgan56
Dec 15th, 2002, 08:45 PM
Well, I can give you an example of what I did...without knowing exactly what you need, it is hard to say. Keep in mind that you should have a "data" class with get and set methods. Once you set up each object in the array, you can use the accessor/mutator methods like this:


public double getWholeAmt() {
double sum = 0.0;
double total = 0.0;
int quant = 0;
for (int i = 0; i < item.length; i++) {
if (item[i] == null)
break;
total = total + item[i].getWholesale();
quant = quant + item[i].getQuantity();
sum = sum + total * quant;
total = 0.0;
quant = 0;
}
return sum;
}


item is the name of my array. I have stretched out the code to try and illustrate what I am doing. This is the first time I have tried to help anyone and I am a beginner. If it looks like I am stumbling too, please let me know.

pigpen
Dec 15th, 2002, 08:56 PM
ok, thanks.
jrmorgan - i sent you a PM