Results 1 to 4 of 4
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Arrow want to make an array

    i have code to find next string after a particular string that shows result correctly..

    right now i want to make an array to show the list of my output's names.

    Java Code:
    FileReader fr;	BufferedReader br;
    String result="";
    String word= new String();
    String target = "friend";
    
    
    try{ //read large text file 
    fr = new FileReader ("C:/Users/user/Desktop/java/test.txt");
    br = new BufferedReader(fr);
    Scanner scan = new Scanner(br);
    
    while(scan.hasNext()){  
    result = scan.findWithinHorizon(target,0); 
    
    if(result!=null) {
    word = (scan.next() + scan.findWithinHorizon("", 0));
    
    System.out.println(word);
    output :

    rocky
    jamila
    vicky
    jacky
    adam
    david

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's Saturday and I left my crystal ball at work. You may do well to post an actual question, such as what trouble are you currently having? What's working and what's not working?

  3. #3
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Default

    actually my program should search a friend name from a txt file and show the similarity scoring.

    if i search with the string "vicky" my program should show : named found vicky -100% similar

    but if i search with the string "vick" then it will show: named found vicky - 90% simiar... (something like this)

    so right now i need an array list that will contain all of my friends name and then my searching word will check entire arraylist one by one to get similarity score.


    suppose my txt file contains :
    " the name of my childhood friend rocky. in my school life i had a best friend vicky but right now he staying in new zeland. Juile is my college friend and classmate too but new freind jacky is really close to me. i have another on-line friend adam who talk everynight with me. i have lost one friend david last year by raod accident "

    so far my code is :
    import java.io.*;
    import java.util.*;
    import java.lang.*;

    class searchCompare2 {

    public static double LCS (String S, String T) {
    S=S.toUpperCase();
    T=T.toUpperCase();

    if ((S.equals(""))||(T.equals("")))
    return 0;
    int[][] num = new int [S.length()] [T.length()];

    int maxlen = 0;
    for (int i = 0; i<S.length(); i++) {
    for (int j=0; j<T.length(); j++) {
    if (S.charAt(i) !=T.charAt(j)) num [i][j] = 0;
    else {
    if (( i==0) || (j==0))
    num [i][j]=1;
    else num [i][j] = 1 + num[i-1][j-1];
    if (num[i][j] >maxlen) {
    maxlen = num[i][j];
    }
    }

    }
    }
    return (2.0*maxlen/(S.length()+T.length()))*100;
    }


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

    FileReader fr;
    BufferedReader br;
    String result="";
    String word= new String();
    String target = "friend";
    Scanner scanner = new Scanner(System.in);
    String searchname;
    String find = "";

    try {

    fr = new FileReader ("C:/Users/user/Desktop/java/test.txt");
    br = new BufferedReader(fr);
    Scanner scan = new Scanner(br);


    while (scan.hasNext()) { //scan.next();
    result = scan.findWithinHorizon(target,0);

    if (result !=null)
    word = (scan.next()+""+scan.findWithinHorizon("",0));

    ArrayList<String> names = new ArrayList<String>();
    names.add(word);
    for (int i=0; i< names.size() ; i++) {

    find = names.get(i);

    }

    }

    System.out.print("Enter your Searching name : ");
    searchname = scanner.next();

    System.out.println("The similarity score is: ");
    System.out.println (searchCompare.LCS(""+searchname , ""+find));


    scan.close();
    br.close();
    fr.close();

    }

    catch(IOException ex) {ex.printStackTrace();}
    catch(StringIndexOutOfBoundsException s){ }
    catch(NoSuchElementException nse){}

    catch(Exception e){ System.err.println(e.getMessage()); } } }
    its working but when i search by adam - its showing 100%
    but if i search with rocky - its showing 0%
    also if i search with other name with correct spelling but its showing different value..

    can u pls give an idea to solve this prob .................
    Last edited by doha786; 03-28-2009 at 10:36 PM. Reason: for showing output:

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    One big error I see is empty catch blocks. That's kind of like trying to drive your car with blinders over your eyes. Why not print stack traces in all catch blocks to make sure that no exceptions are being thrown?

Similar Threads

  1. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM
  2. How to make a function?
    By mephisto772 in forum New To Java
    Replies: 5
    Last Post: 02-23-2009, 09:51 AM
  3. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 08:19 AM
  4. simplest way to make 2d array to string
    By Tamu in forum Advanced Java
    Replies: 4
    Last Post: 11-25-2008, 04:50 PM
  5. i need help to make this program
    By masaka in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 01:26 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
  •