CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2018
    Posts
    2

    Angry Search for a repeated subtsring and print out all the string value only once in java

    Hi

    I am trying to read the a string that shows the version name from a text file and print it. I only want to print the string once as it occurs a few times in the text log file. I managed it with the code below that should do it but keep it compilation errors. the string I am interested in the file is as follow: Version software 219 build. The error I am getting is: The constructor FileReader(String) is undefined


    Code:
     Map<String,Integer> map=new HashMap<String, Integer>();
        		    FileReader freader = new FileReader("C:\\abc.log");
        		    BufferedReader br = new BufferedReader(freader);
        		    String s, t = "";
        		    while((s = br.readLine()) != null) {
        		    t = t + s;
        		    }
        		    freader.close();
        		    String b[] = t.split("\n");
        		    int j = 0;
        		    while(j < b.length) {
        		        String c[] = b[j].split(" ");
        		        for(int i = 0; i < c.length; i++) {
        		            if(c[i].equals("Version")) {
        		                String r = "";
        		                for(int k = i; k < i+4; k++)
        		                    r = r + c[k] + " ";
        		                map.put(r, j);
        		            }
        		    }
        		        j++;
        		    }
        		    Map<String, Integer> mapResult;
        			for (Entry<String, Integer>  entry : mapResult.entrySet()) {
        		         String distance = entry.getKey();
        		         Integer value = entry.getValue();
        		         System.out.println(entry.getKey());  
        		     }
        		          }
    Can anyone check how to fix it and if it work on their set up?

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: Search for a repeated subtsring and print out all the string value only once in j

    Quote Originally Posted by hm99 View Post
    The error I am getting is: The constructor FileReader(String) is undefined
    Are you sure you have imported everything you should? Add these,

    import java.io.File;
    import java.io.FileReader;

    Are you sure you haven't defined your own FileReader class. Try this to make sure you're using the right FileReader,

    java.io.FileReader freader = new java.io.FileReader("C:\\abc.log");
    Last edited by wolle; February 17th, 2018 at 03:16 AM.

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Search for a repeated subtsring and print out all the string value only once in j

    The readLine method stops reading data when it finds a lineend and returns the data without the lineend character. The split won't find the \n
    Concatenating s and t without an intervening character would concatenate the end of one line with the start of the next line.

    Note: single letter variable names makes the code hard to read and understand. variable names should describe the data they hold.
    Norm

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
  •  





Click Here to Expand Forum to Full Width

Featured