Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: processing a text file into a format in java

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default processing a text file into a format in java

    i have text file of this form:
    0 file:/home/lenovo/mallet/cleantweet/2428741 1 0.2406223358908781 3 0.15934924694515487 0 0.1308610400682012 7 0.11295822676896845 9 0.08688547882921284 8 0.08077578857630009 4 0.0784313725490196 6 0.06045751633986928 5 0.029269678886047173 2 0.020389315146348393
    1 file:/home/lenovo/mallet/cleantweet/28397802 1 0.3631535947712418 7 0.1323529411764706 0 0.0988562091503268 8 0.08905228758169935 3 0.07026143790849673 4 0.06004901960784314 9 0.05800653594771242 5 0.049836601307189546 6 0.04861111111111111 2 0.0298202614379085
    ..more entries like this.
    I need to store the double values in an 2-d array. the last value in this path is id file:/home/lenovo/mallet/cleantweet/2428741. and integer values are some topics. the array should be like this a[u1][t1]= 0.1308610400682012 a[u2][t2]=0.020389315146348393 etc..
    code =java
    String line[],a[];
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<Integer> u = new ArrayList<Integer>();
    List<Map<Integer,Double>>prob;

    public static void main(String[] args) throws FileNotFoundException, IOException, Exception {

    String s=null,usr[];
    String str[]=null;
    int i=0,user=0;
    lda2 ld= new lda2();
    FileReader fr = new FileReader("/home/lenovo/mallet/mydata_inferdoctopics");
    BufferedReader br = new BufferedReader(fr);
    while ((s = br.readLine()) != null){
    ld.list.add(s);
    // System.out.println(ld.list
    ld.a=s.split("\t");
    System.out.println(ld.a.length);
    usr = ld.a[1].split("/");
    ld.u.add(Integer.parseInt(usr[5]));
    user++;
    }

    String[] listarray = new String [ld.list.size()];
    int mat[][]= new int [user][10];

    for( i=0;i<ld.list.size();i++){
    listarray[i] = ld.list.get(i);
    // System.out.println(listarray[i]);
    }

    double b[];
    for(int k=0;k<listarray.length;k++){
    System.out.println(listarray[k]);
    str= listarray[k].split("\t");
    Map<Integer,Double> pr = new TreeMap<Integer,Double>();

    for(int j=2;j<str.length{
    // System.out.println(str[j]);

    pr.put(Integer.parseInt(str[j]),Double.parseDouble(str[j+1]));
    j=j+2;
    }
    // ld.prob.add(pr);
    int key[];

    for (Map.Entry<Integer,Double> entry : pr.entrySet()) {
    System.out.println("z:"+entry.getKey()+"\t"+"prob: "+ entry.getValue());

    }




    }
    [/code]
    am i going in correct way?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: processing a text file into a format in java

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  2. [SOLVED] JAVA Text File Processing with REGEX
    By tonionio in forum Java Theory & Questions
    Replies: 9
    Last Post: February 28th, 2013, 05:01 AM
  3. Reading from Text File and Processing Information
    By royalslim in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 13th, 2010, 03:27 PM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Format some text with Java
    By vampire in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 18th, 2010, 11:30 AM

Tags for this Thread