Reading from a file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Reading from a file

  1. #1
    Join Date
    May 2005
    Posts
    115

    Reading from a file

    Lets say i have a file "userDistance.txt" with the contents:

    Legend:
    A B C D E

    Distance:
    {0}
    {10 0}
    {11 12 0}
    {11 12 13 0}
    {11 12 13 14 0}

    Using Scanner and FileInputStream, how do i extract the data for legend and distance and store:
    1. ABCDE in to String called legend
    2. The distance matrix into a multidimensional array

    I have tried things like
    Code:
    if(inputStream.next()=="Legend:")
    
    or nextLine()
    To detect the 1st line with the word Legend and extract all the characters after Legend, but it doesnt work.

    Please help, thx guys...
    Last edited by Ant_Magma; 08-26-2005 at 04:52 PM.

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    This help you get started:

    Code:
    BufferedReader in = new BufferedReader(new FileInputStream("userDistance.txt"));
    
    String key = in.readLine();
    if (key.equals("Legend"))
    {
        //break the string up into tokens using a "space" as the delimiter
        String[] legend = in.readLine().split(" ");
    }

  3. #3
    Join Date
    May 2005
    Posts
    115
    Oh Phaelax, thx man..after reading ur code i guess i know what was wrong with mine..

    I used == instead of equals. I keep on forgetting that String is an object..

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2001, 01:49 PM
  2. Replies: 3
    Last Post: 08-23-2001, 12:01 PM
  3. How to use Java to lock a file
    By Cynthia Leslie in forum Java
    Replies: 3
    Last Post: 06-09-2001, 07:43 PM
  4. Correction: Reading Summary/Shortcut File Properties
    By Millard Melnyk in forum VB Classic
    Replies: 8
    Last Post: 06-03-2000, 04:03 PM
  5. Correction: Reading Summary/Shortcut File Properties
    By Millard Melnyk in forum VB Classic
    Replies: 0
    Last Post: 06-02-2000, 03:12 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center