Read columns from a file ...

Posted on 2006-04-25
Last Modified: 2010-03-31
Hello experts,

  I have a file:

  Name, gender, age, phone, address
  A,  F. 12, 123456, ABCDE
  B, M, 13, 111111, BCDEF

 What would be the best (fast way) to read the data in the file and
 then put (Name, address) in a Hashmap ?

Question by:meow00
    LVL 8

    Assisted Solution

    LVL 86

    Assisted Solution

    LVL 30

    Assisted Solution

    LVL 27

    Accepted Solution

    If your format will always remain the same, then you could use something like the following.  

    import java.util.*;
    public class meow {
      public static void main(String args[]) {
             BufferedReader br = new BufferedReader(new FileReader("meow00.txt"));
             HashMap map = new HashMap();
             String line = "this will hold current line";
             line = br.readLine(); // header not put
             while((line = br.readLine()) != null){
                                          String[] tokens = line.split(",");
            }catch(FileNotFoundException fnfe){System.out.println("fnfe");}
             catch(IOException ioe){System.out.println("ioe");}
    LVL 27

    Expert Comment

    Maybe you should add this line as well  
    catch(ArrayIndexOutOfBoundsException e){System.out.println("problem with format");}
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now