Results 1 to 10 of 10

Thread: Beginner needs help with file I/O...

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    8

    Beginner needs help with file I/O...

    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...


  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I would recommend the StringTokenizer class.
    Code:
    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;
    	}
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    8
    That works perfectly! THANK YOU!!!!!! I really appreciate your time in giving the code.

    Microsoft Visual C++ 6.0
    Microsoft Visual Studio 6.0
    JBuilder 6

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Very nice, but main doesn't ever throw (all exceptions are caught by the try-catch block), so the declaration isn't necessary.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    8
    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?
    Microsoft Visual C++ 6.0
    Microsoft Visual Studio 6.0
    JBuilder 6

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    New Member
    Join Date
    Dec 2002
    Location
    never can tell
    Posts
    2
    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    8

    Maybe something like this?

    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:

    Code:
       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.
    Microsoft Visual C++ 6.0
    Microsoft Visual Studio 6.0
    JBuilder 6

  10. #10
    New Member
    Join Date
    Dec 2002
    Location
    never can tell
    Posts
    2
    ok, thanks.
    jrmorgan - i sent you a PM

Posting Permissions

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



Click Here to Expand Forum to Full Width