Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: Parse part file

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    how could i instantiate the output of my example class to the hashmap
    Are you asking how to add an instance of your class to a hashmap?
    Look at the HashMap API doc for how to add a value to it.

    ow could i give an automatic id generation for each record.
    Append a global counter to a prefix: "ID-" + counter;

  2. #22
    karthinkin is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Hi Guys, after holiday once again I'm started working on the same program.

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;
    import java.util.ArrayList;
     
     
     
    public class exam {
     
     
    private static final String BEGIN = null;
    private static final String END = null;
     
     
     
     
    public static void main(String args[])
     
    {
            try {
                BufferedReader in = new BufferedReader(new FileReader("D:/juin6.part"));
                List<String> myList = new ArrayList<String>();
                String str;
                while ((str = in.readLine()) != null) {
               
                    myList.add(str);
                    myList.remove(args);
                  
                    System.out.println(str);       
                } 
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
           
     
     
    HashMap<Integer,Person> hmap= new HashMap<Integer,Person>();
     
    Person pr=new Person();
     
    Contat ct = getContact();
    pr.setCt(ct);
     
    String fName = getFName();
    pr.setFName(fName);
     
    String name = getName();
    pr.setName(name);
     
    Integer intergetvalue = getKey();
    hmap.put(intergetvalue, pr);
     
    Set set = hmap.entrySet();
     
    Iterator i = set.iterator();
     
    while(i.hasNext()){
      Map.Entry me = (Map.Entry)i.next();
      //System.out.println(me.getKey());
    // System.out.println(me.getValue());
     
                    }
            }
     
     
     
    }
     
     
     
            private static Integer getKey() {
                    return null ;
            // TODO Auto-generated method stub
           
    }
     
     
     
     
            private static Contat getContact() {
            // TODO Auto-generated method stub
            return null;
    }
     
     
     
     
            private static String getName() {
            // TODO Auto-generated method stub
                   
            return null;
    }
     
     
     
     
            private static String getFName() {
            // TODO Auto-generated method stub
            return null;
    }
     
     
     
     
            @Override
            public String toString() {
                    return "exam [getClass()=" + getClass() + ", hashCode()=" + hashCode()
                                    + ", toString()=" + super.toString() + "]";
            }
    }
     
     
     
    /*
            public static Person getPerson() {
                    return Person;
            }
    }
     
            */
    This is the file I'm reading :

    BEGIN
    N:Arun_niit
    FN:Arun_niit
    EMAIL:nura_ice@yahoo.co.in
    END

    I'm able to read the entire file in array list but I want to read without Begin and End; so i tried using "remove" and "replace" both didn't work. Basically I want to put all these infuriation form a file into an arraylist and then read N: FN: EMAIL:

    Thanking U guys.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I want to read without Begin and End
    Your code should check for the lines/Strings that you don't want in your arraylist and skip them.

    i tried using "remove" and "replace" both didn't work
    Can you show us what you did that didn't work?

  4. #24
    karthinkin is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Hi Guys,
    I managed to generate output like this :
    Java Code:
    EMAIL  nura_ice@yahoo.co.in  giorgiacolombo89@libero.it  karthycse@yahoo.co.in  sreemathan@gmail.com  kart2006@gmail.com  karthikrangaraj@yahoo.com  akkireddych@gmail.com  KUMARg_81@yahoo.com  
    FN  Arun_niit  Hải Anh B‘ng Mũn  Giorgia Colombo  Rebecca Flynn  Ben' Foden  karthik keyan  Funkybuddha Marbella  Mathan  karthik Rangaraj  Akky Reddy  Paul Michael Regan  Mathieu Robert  Celine Rodriguez  Keryn Sahota  Frankie Sandford  Pixie Sandford  Jackie Cuppycakes Smith  Rochelle TheSats  Veera_Kumar  
    N  Arun_niit  B‘ng MũnHải  ColomboGiorgia  FlynnRebecca  FodenBen'  keyankarthik  MarbellaFunkybuddha  Mathan  Rangarajkarthik  ReddyAkky  ReganPaul  RobertMathieu  RodriguezCeline  SahotaKeryn  SandfordFrankie  SandfordPixie  SmithJackie  TheSatsRochelle  Veera_Kumar
    This is my XML Schema :

    Java Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <contacts>
    
    <contact>
    		<N>Arun_niit</N>
    		<FN>Arun_niit</FN>
    	<EMAILS>
    		<EMAIL>
    			<type>Yahoo</type>
    			<value>nura_ice@yahoo.co.in</value>
    		</EMAIL>
    		<EMAIL>
    			<type>Gmail</type>
    			<value>nura_ice@gmail.com </value>
    		</EMAIL>
    
    		
    	</EMAILS>
    </contact>
    
    
    </contacts>
    This is my .xsd file for the above xml schema:

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
      <xs:element name="contacts">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="contact"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="contact">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="N"/>
            <xs:element ref="FN"/>
            <xs:element ref="EMAILS"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="N" type="xs:NCName"/>
      <xs:element name="FN" type="xs:NCName"/>
      <xs:element name="EMAILS">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="EMAIL"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="EMAIL">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="type"/>
            <xs:element ref="value"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="type" type="xs:NCName"/>
      <xs:element name="value" type="xs:string"/>
    </xs:schema>
    Any one can tell me how could i generate a XML file which includes the output of my java program.

    This is my Java Program:(I have changed my program)

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
     
    public class contactmatrix {
     
    public static void main(String args[]) throws FileNotFoundException
    {
    // your input file with city, state values
    File file = new File("D:/Juin6.part");
    
    // data structure to hold mapping of state to list of EMAIL,FN,N
    SortedMap<String, List<String>> map = new TreeMap<String, List<String>>();
    
    
    // scan file by line and populate data structure
    Scanner scanner = new Scanner(file).useDelimiter("\\n");
    while (scanner.hasNext()) {
       String line = scanner.next();
    
       // only process lines with a  :
       if (line.contains(":")) {
           // split the line on the comma and extract EMAIL,FN,N
        
           String[] parts = line.split(":");
           String N = parts[0].trim();
           String FN = parts[1].trim();
           //String EMAIL = parts[2].trim();
           
           //tryin to remove url form file ; String removeLineFromFile = "URL";
           FileUtil util = new FileUtil();
           util.removeLineFromFile("D:/Juin6.part", "URL:");
           
           // if the state doesn't exist in the map yet, create it
           List<String> FirstName = map.get(N);
           if (FirstName == null) {
            FirstName = new ArrayList<String>();
               map.put(N,FirstName);
           }
    
           // add the Name to FN if it doesn't contain
           if (!FirstName.contains(FN)) {
            FirstName.add(FN);
           }
          
       }
    }
    
    // iterate over the states for output
    for (String N : map.keySet()) {
       // build up a string for each N,FN,EMAIL
       StringBuilder sb = new StringBuilder();
    
       // start with the Name
       sb.append(N + "  ");
    
       // now append the FName
       List<String> FirstName = map.get(N);
       for (String FN : FirstName) {
           sb.append(FN + "  ");
       }
    
       // remove the last comma
      // sb.delete(sb.length() - 1, sb.length());
    
       // print out the finished line
       String output = sb.toString();
       System.out.println(output);
    		}
     	}
    
    }
    Please provide me some detail explanation if possible. Thnking U guys.


    karthik

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Parse file into arrayList
    By zenitis in forum New To Java
    Replies: 8
    Last Post: 05-02-2011, 09:01 AM
  2. help to parse an xhtml file
    By nijil in forum New To Java
    Replies: 0
    Last Post: 02-20-2010, 11:37 AM
  3. How to parse a .db file ?
    By aminov in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 05:11 PM
  4. Replies: 7
    Last Post: 05-23-2008, 04:46 AM
  5. how to parse an xml file
    By oregon in forum XML
    Replies: 3
    Last Post: 08-01-2007, 05:53 PM

Tags for this Thread

Posting Permissions

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