[Solved] My search function doesn't work as it should, and i have no idea why

Discussion in 'Plugin Development' started by blackwolf12333, Jun 2, 2012.

Thread Status:
Not open for further replies.
  1. Hi
    I have a search function and some other functions which use it, as you can see in this class:
    Code:
    package tk.blackwolf12333.grieflog;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
     
    import tk.blackwolf12333.grieflog.api.IGriefLogSearcher;
     
    public class GriefLogSearcher implements IGriefLogSearcher {
     
        private boolean addExtraLines = true;
        List<File> files;
        File[] searchFiles;
       
        protected boolean isAddExtraLines() {
            return addExtraLines;
        }
     
        protected void setAddExtraLines(boolean addExtraLines) {
            this.addExtraLines = addExtraLines;
        }
       
        protected String searchFile(String ...text) {
           
            String data = "";
            try {
                files = new ArrayList<File>();
                File file = new File("logs/");
                String[] list = file.list();
               
                for(int i = 0; i < list.length; i++)
                {
                    files.add(new File(list[i]));
                }
                File[] searchFiles = new File[files.size()];
                searchFiles = files.toArray(searchFiles);
               
                for(int i = 0; i < searchFiles.length; i++)
                {
                    FileReader fileReader = new FileReader(searchFiles[i]);
                    BufferedReader br = new BufferedReader(fileReader);
                    String line = "";
                   
                    if(text.length == 1)
                    {
                        while((line = br.readLine()) != null)
                        {
                            if(line.contains(text[0]))
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                    else if(text.length == 2)
                    {
                        while((line = br.readLine()) != null)
                        {
                            if(line.contains(text[0]) && line.contains(text[1]))
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                    else if(text.length == 3)
                    {
                        while((line = br.readLine()) != null)
                        {
                            if(line.contains(text[0]) && line.contains(text[1]) && line.contains(text[2]))
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                    else if(text.length == 4)
                    {
                        while((line = br.readLine()) != null)
                        {
                            if(line.contains(text[0]) && line.contains(text[1]) && line.contains(text[2]) && line.contains(text[3]))
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                    else
                    {
                        return "To many arguments";
                    }
                   
                   
                   
                    br.close();
                    fileReader.close();
                }
               
            } catch (Exception e) {
                e.printStackTrace();           
            }
           
            if(data.length() > 0)
            {
                return data;
            }
           
            return data;
        }
       
        @Override
        public String searchText(String arg0)
        {
            return searchFile(arg0);
        }
       
        @Override
        public String searchText(String arg0, String arg1)
        {
            return searchFile(arg0, arg1);
        }
       
        @Override
        public String searchText(String arg0, String arg1, String arg2)
        {
            return searchFile(arg0, arg1, arg2);
        }
       
        @Override
        public String searchPos(String[] pos)
        {
            if(pos.length > 3)
            {
                return "To many arguments";
            }
            String xyz = pos[0] + ", " + pos[1] + ", " + pos[2];
            return searchFile(xyz);
        }
     
        @Override
        public boolean searchGriefLog(String text, String file, Player p) {
           
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
               
                if(isAddExtraLines())
                {
                    p.sendMessage(ChatColor.BLUE + "+++++++++++GriefLog+++++++++++");
               
                    while((line = br.readLine()) != null)
                    {
                        if(line.indexOf(text) >= 0)
                        {
                            p.sendMessage(line);
                        }
                    }
                   
                    p.sendMessage(ChatColor.BLUE + "++++++++++GriefLogEnd+++++++++");
                }
                else
                {
                    while((line = br.readLine()) != null)
                    {
                        if(line.indexOf(text) >= 0)
                        {
                            p.sendMessage(line);
                        }
                    }
                }
               
                br.close();
                fileReader.close();
               
                return true;
               
            } catch (Exception e) {
                e.printStackTrace();           
            }
            return false;
        }
     
        @Override
        public boolean searchGriefLog(String text, File file, Player p) {
           
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
               
                if(isAddExtraLines())
                {
                    p.sendMessage(ChatColor.BLUE + "+++++++++++GriefLog+++++++++++");
               
                    while((line = br.readLine()) != null)
                    {
                        if(line.indexOf(text) >= 0)
                        {
                            p.sendMessage(line);
                        }
                    }
                   
                    p.sendMessage(ChatColor.BLUE + "++++++++++GriefLogEnd+++++++++");
                }
                else
                {
                    while((line = br.readLine()) != null)
                    {
                        if(line.indexOf(text) >= 0)
                        {
                            p.sendMessage(line);
                        }
                    }
                }
               
                br.close();
                fileReader.close();
               
                return true;
               
            } catch (Exception e) {
                e.printStackTrace();           
            }
            return false;
        }
     
        @Override
        public void readReportFile(String file, Player p) {
           
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
               
                p.sendMessage(ChatColor.RED + "+++++++++ReportStart+++++++++");
                while((line = br.readLine()) != null)
                {
                        p.sendMessage(line);
                }
                p.sendMessage(ChatColor.RED + "++++++++++ReportEnd+++++++++");
               
                br.close();
                fileReader.close();
               
            } catch (Exception e) {
                e.printStackTrace();           
            }
        }
       
       
    }
    
    The function that doesn't work as it should is the searchText(String ...text) function, when i use it 2 times, i get the same line 2 times, when i use it 3 times i get the same line 3 times, and i have no idea why, i hope you guys get what i mean:p
    Thanks in advance
    greetz blackwolf12333

    Am i asking the wrong question?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  2. Offline

    r0306

    blackwolf12333
    You are using the constructor String with an Array of strings and also, why do you have ...text instead of text? You should split the string inside the method, so all you would have to call up is:
    Code:
    searchFile("your string here");
    
    Then the string will get split inside the method.
     
  3. That didn't work, i still get the line i requested + 1 every time i try to use this function...maybe that's not really clear, if so please tell me, then i could post a picture which shows the problem i think.
    greetz blackwolf12333

    EDIT: Hmm, i fixed it:p forgot to add a return in each if statement:p
    But the most important thing still doesn't happen, because what i want to happen is that this function also searches the files in the directory "logs/" and that doesn't happen yet:(

    I'll post my new code here, because i changed it a bit...
    Code:
    package tk.blackwolf12333.grieflog;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
     
    import tk.blackwolf12333.grieflog.api.IGriefLogSearcher;
     
    public class GriefLogSearcher implements IGriefLogSearcher {
     
        private boolean addExtraLines = true;
        ArrayList<File> files = new ArrayList<File>();
       
        protected boolean isAddExtraLines() {
            return addExtraLines;
        }
     
        protected void setAddExtraLines(boolean addExtraLines) {
            this.addExtraLines = addExtraLines;
        }
       
        public GriefLogSearcher() {
            files.add(GriefLog.file);
            File file = new File("logs/");
            String[] list = file.list();
            System.out.print(list.length);
           
            for(int i = 0; i < list.length; i++)
            {
                files.add(new File(list[i]));
            }
        }
       
        protected String searchFile(String ...text) {
           
            String data = "";
            try {
               
                File[] searchFiles = new File[files.size()];
                searchFiles = files.toArray(searchFiles);
               
                if(text.length == 1)
                {
                    for(int i = 0; i < searchFiles.length; i++)
                    {
                        System.out.print("debug");
                        FileReader fileReader = new FileReader(searchFiles[i]);
                        BufferedReader br = new BufferedReader(fileReader);
                        String line = "";
                       
                        while((line = br.readLine()) != null)
                        {
                            if(line.indexOf(text[0]) >= 0)
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                }
                if(text.length == 2)
                {
                    for(int i = 0; i < searchFiles.length; i++)
                    {
                        System.out.print("debug");
                        FileReader fileReader = new FileReader(searchFiles[i]);
                        BufferedReader br = new BufferedReader(fileReader);
                        String line = "";
                        while((line = br.readLine()) != null)
                        {
                            if(line.contains(text[0]) && line.contains(text[1]))
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                }
                if(text.length == 3)
                {
                    for(int i = 0; i < searchFiles.length; i++)
                    {
                        System.out.print("debug");
                        FileReader fileReader = new FileReader(searchFiles[i]);
                        BufferedReader br = new BufferedReader(fileReader);
                        String line = "";
                        while((line = br.readLine()) != null)
                        {
                            if(line.contains(text[0]) && line.contains(text[1]) && line.contains(text[2]))
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                }
                if(text.length == 4)
                {
                    for(int i = 0; i < searchFiles.length; i++)
                    {
                        System.out.print("debug");
                        FileReader fileReader = new FileReader(searchFiles[i]);
                        BufferedReader br = new BufferedReader(fileReader);
                        String line = "";
                        while((line = br.readLine()) != null)
                        {
                            if(line.contains(text[0]) && line.contains(text[1]) && line.contains(text[2]) && line.contains(text[3]))
                            {
                                data += line + System.getProperty("line.separator");
                            }
                        }
                    }
                }
                return data;
            } catch (Exception e) {
                e.printStackTrace();           
            }
           
            return data;
        }
       
        @Override
        public String searchText(String arg0)
        {
            return searchFile(arg0);
        }
       
        @Override
        public String searchText(String arg0, String arg1)
        {
            return searchFile(arg0, arg1);
        }
       
        @Override
        public String searchText(String arg0, String arg1, String arg2)
        {
            return searchFile(arg0, arg1, arg2);
        }
       
        @Override
        public String searchPos(int x, int y, int z)
        {
            String xyz = z + ", " + y + ", " + z;
            return searchFile(xyz);
        }
     
        @Override
        public boolean searchGriefLog(String text, String file, Player p) {
           
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
               
                if(isAddExtraLines())
                {
                    p.sendMessage(ChatColor.BLUE + "+++++++++++GriefLog+++++++++++");
               
                    while((line = br.readLine()) != null)
                    {
                        if(line.contains(text))
                        {
                            p.sendMessage(line);
                        }
                    }
                   
                    p.sendMessage(ChatColor.BLUE + "++++++++++GriefLogEnd+++++++++");
                }
                else
                {
                    while((line = br.readLine()) != null)
                    {
                        if(line.contains(text))
                        {
                            p.sendMessage(line);
                        }
                    }
                }
               
                br.close();
                fileReader.close();
               
                return true;
               
            } catch (Exception e) {
                e.printStackTrace();           
            }
            return false;
        }
     
        @Override
        public boolean searchGriefLog(String text, File file, Player p) {
           
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
               
                if(isAddExtraLines())
                {
                    p.sendMessage(ChatColor.BLUE + "+++++++++++GriefLog+++++++++++");
               
                    while((line = br.readLine()) != null)
                    {
                        if(line.contains(text))
                        {
                            p.sendMessage(line);
                        }
                    }
                   
                    p.sendMessage(ChatColor.BLUE + "++++++++++GriefLogEnd+++++++++");
                }
                else
                {
                    while((line = br.readLine()) != null)
                    {
                        if(line.contains(text))
                        {
                            p.sendMessage(line);
                        }
                    }
                }
               
                br.close();
                fileReader.close();
               
                return true;
               
            } catch (Exception e) {
                e.printStackTrace();           
            }
            return false;
        }
     
        @Override
        public void readReportFile(String file, Player p) {
           
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
               
                p.sendMessage(ChatColor.RED + "+++++++++ReportStart+++++++++");
                while((line = br.readLine()) != null)
                {
                        p.sendMessage(line);
                }
                p.sendMessage(ChatColor.RED + "++++++++++ReportEnd+++++++++");
               
                br.close();
                fileReader.close();
               
            } catch (Exception e) {
                e.printStackTrace();           
            }
        }
       
       
    }
    
    So, the thing this code still doesn't do, and i really have no idea why it doesn't ... is searching the files in the "logs/" directory, which it should do...
    I hope you guys can help me:)
    greetz blackwolf12333

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. The first thing I think of whas something whit case sensitive search, bt I dont thik thats the problem whoever after looking to the code
     
  5. case doesn't matter that much:p most of the time i am searching for numbers so...
     
  6. also, why you keep doing
    Code:java
    1. if(text.length == 1){
    2. ...
    3. }if(text.length == 2){
    4. ...
    5. }if(text.length == 3){
    6. ...
    7. }if(text.length == 4){
    8. ...
    9. }

    and not
    Code:java
    1. for(int i = 0; i < searchFiles.length; i++)
    2. {
    3. System.out.print("debug");
    4. FileReader fileReader = new FileReader(searchFiles[i]);
    5. BufferedReader br = new BufferedReader(fileReader);
    6. String line = "";
    7. while((line = br.readLine()) != null)
    8. {
    9. for(String txt : text)
    10. { if(line.contains(txt))
    11. {
    12. data += line + System.getProperty("line.separator");
    13. break; // Breaks for loop
    14. }
    15. }
    16. }
    17. }[/i]
     
  7. should that break statement be there, because i need all the lines in that file which contain the specified text, and second, as you can see in the whole class i have other functions that must return a string that contains all the lines in the files that contain all the given arguments, not only one.
    greetz blackwolf12333
     
  8. yes, that break stament needs there, its used to break the for loop to loo[p the entered tekst
     
  9. Well, thanks for helping, but it didn't work yet..:/ The function still doesn't return anything, and i am 100% sure it should...
    greetz blackwolf12333
     
  10. Can you try this:
    Code:java
    1.  
    2. protected String searchFile(String ...text) {
    3.  
    4. String data = "";
    5. try {
    6.  
    7. File[] searchFiles = new File[files.size()];
    8. searchFiles = files.toArray(searchFiles);
    9. for(int i = 0; i < searchFiles.length; i++)
    10. {
    11. System.out.print("debug");
    12. FileReader fileReader == null;
    13. try
    14. {
    15. fileReader = new FileReader(searchFiles[i]);
    16. BufferedReader br = new BufferedReader(fileReader);
    17. String line = "";
    18. while((line = br.readLine()) != null)
    19. {
    20. for(String txt : text)
    21. { System.out.print("debug: checking if line "+line+" of file "+searchFiles[i]+" contains "+txt);
    22. if(line.contains(txt))
    23. {
    24. System.out.print("debug: line "+line+" of file "+searchFiles[i]+" contains "+txt);
    25. data += line + System.getProperty("line.separator");
    26. break; // Breaks for loop (for(String txt : text))
    27. }
    28. }
    29. }
    30. }
    31. finally
    32. {
    33. if(fileReader != null)fileReader.close();
    34. }
    35. }
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. }
    39.  
    40. return data;[/i][/i][/i]
     
  11. Tried it, it nicely outputs this line of code:
    Code:
    System.out.print("debug: checking if line "+line+" of file "+searchFiles+" contains "+txt);
    but when you remove that line, because the only thing it does is spamming the console, the line:
    Code:
    System.out.print("debug: line "+line+" of file "+searchFiles+" contains "+txt);
    does not output anything...:(
    greetz blackwolf12333

    Ok, now i don't get it anymore, i now have another function, in another class, which does work...
    here is the function that does work:
    Code:
    public void searchText(String text, CommandSender p) {
           
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            for(File searchFile : searchFiles)
            {
                try {
                    System.out.print(searchFile.getAbsolutePath());
                    FileReader fileReader = new FileReader(searchFile);
                    BufferedReader br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf(text) >= 0) {
                            p.sendMessage(line);
                        }
                    }
     
                    br.close();
                    fileReader.close();
     
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    but when i try to use the same code in this class, it doesn't work...: this is the class i gave you guys before
    Code:
    package tk.blackwolf12333.grieflog;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
     
    import tk.blackwolf12333.grieflog.api.IGriefLogSearcher;
     
    public class GriefLogSearcher implements IGriefLogSearcher {
     
        private boolean addExtraLines = true;
        String data = "";
        ArrayList<File> files = new ArrayList<File>();
     
        protected boolean isAddExtraLines() {
            return addExtraLines;
        }
     
        protected void setAddExtraLines(boolean addExtraLines) {
            this.addExtraLines = addExtraLines;
        }
     
        public GriefLogSearcher() {
            files.add(GriefLog.file);
           
            File file = new File("logs/");
            String[] list = file.list();
     
            for (String element : list) {
                files.add(new File("logs" + File.separator + element));
            }
        }
     
        @Override
        public String searchText(String arg0) {
           
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            for(File searchFile : searchFiles)
            {
                try {
                    System.out.print(searchFile.getAbsolutePath());
                    FileReader fileReader = new FileReader(searchFile);
                    BufferedReader br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf(arg0) >= 0) {
                            data += line + System.getProperty("line.separator");
                        }
                    }
     
                    br.close();
                    fileReader.close();
     
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return data;
        }
     
        @Override
        public String searchText(String arg0, String arg1) {
           
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            for(File searchFile : searchFiles)
            {
                try {
                    System.out.print(searchFile.getAbsolutePath());
                    FileReader fileReader = new FileReader(searchFile);
                    BufferedReader br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if ((line.indexOf(arg0) >= 0) && (line.indexOf(arg1) > 0)) {
                            data += line + System.getProperty("line.separator");
                        }
                    }
     
                    br.close();
                    fileReader.close();
     
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return data;
        }
     
        @Override
        public String searchText(String arg0, String arg1, String arg2) {
     
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            for(File searchFile : searchFiles)
            {
                try {
                    System.out.print(searchFile.getAbsolutePath());
                    FileReader fileReader = new FileReader(searchFile);
                    BufferedReader br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if ((line.indexOf(arg0) >= 0) && (line.indexOf(arg1) > 0) && (line.indexOf(arg2) > 0)) {
                            data += line + System.getProperty("line.separator");
                        }
                    }
     
                    br.close();
                    fileReader.close();
     
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return data;
        }
     
        @Override
        public String searchPos(int x, int y, int z) {
            String xyz = z + ", " + y + ", " + z;
            StringBuffer sb = new StringBuffer();
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            for(File searchFile : searchFiles)
            {
                try {
                    System.out.print(searchFile.getAbsolutePath());
                    FileReader fileReader = new FileReader(searchFile);
                    BufferedReader br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf(xyz) >= 0) {
                            sb.append(line);
                        }
                    }
     
                    br.close();
                    fileReader.close();
     
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return    sb.toString();
        }
     
        @Override
        public boolean searchGriefLog(String text, String file, Player p) {
     
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
     
                if (isAddExtraLines()) {
                    p.sendMessage(ChatColor.BLUE + "+++++++++++GriefLog+++++++++++");
     
                    while ((line = br.readLine()) != null) {
                        if (line.contains(text)) {
                            p.sendMessage(line);
                        }
                    }
     
                    p.sendMessage(ChatColor.BLUE + "++++++++++GriefLogEnd+++++++++");
                } else {
                    while ((line = br.readLine()) != null) {
                        if (line.contains(text)) {
                            p.sendMessage(line);
                        }
                    }
                }
     
                br.close();
                fileReader.close();
     
                return true;
     
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
     
        @Override
        public boolean searchGriefLog(String text, File file, Player p) {
     
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
     
                if (isAddExtraLines()) {
                    p.sendMessage(ChatColor.BLUE + "+++++++++++GriefLog+++++++++++");
     
                    while ((line = br.readLine()) != null) {
                        if (line.contains(text)) {
                            p.sendMessage(line);
                        }
                    }
     
                    p.sendMessage(ChatColor.BLUE + "++++++++++GriefLogEnd+++++++++");
                } else {
                    while ((line = br.readLine()) != null) {
                        if (line.contains(text)) {
                            p.sendMessage(line);
                        }
                    }
                }
     
                br.close();
                fileReader.close();
     
                return true;
     
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
     
        @Override
        public void readReportFile(String file, Player p) {
     
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                String line = "";
     
                p.sendMessage(ChatColor.RED + "+++++++++ReportStart+++++++++");
                while ((line = br.readLine()) != null) {
                    p.sendMessage(line);
                }
                p.sendMessage(ChatColor.RED + "++++++++++ReportEnd+++++++++");
     
                br.close();
                fileReader.close();
     
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public String search(String... text) {
            String data = null;
     
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
            for (int i = 0; i < searchFiles.length; i++) {
                System.out.print("bla3");
                if (searchFiles[i].isFile()) {
                    System.out.print("bla2");
                    try {
                        String line = null;
                        BufferedReader br = new BufferedReader(new FileReader(new File("logs") + File.separator + searchFiles[i]));
                        while ((line = br.readLine()) != null) {
                            if (line.contains(text[0])) {
                                data += line;
                                break;
                            }
                        }
                        return data;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
     
            return data;
        }
     
    }
    
    As you can see i used almost the same code in the functions of the class as i used in the function that did work...
    So the new question is now, why did that one function work and the functions in this class don't??
    greetz blackwolf12333

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  12. Mayby because you using indexOf()?, also your not closing the file if something go wrong while reading the file
     
  13. as far as i can see i am closing the file every time:p and it should not matter if i use indexOf or contains...
     
  14. if there is an IOExceptio while reading the file, it skips directly to the cathc, block, skipping the close file
     
  15. Hmm ok, i'll fix that
     
  16. use the finaly for that isrecomment
     
  17. Hmm, ok, but that doesn't solve the problem, look at this function, and tell me if it should work or not, i know it isn't the best way of doing it but...
    Code:
    public String searchPos(int x, int y, int z) {
            String xyz = z + ", " + y + ", " + z;
            StringBuffer sb = new StringBuffer();
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            FileReader fileReader = null;
            BufferedReader br = null;
           
            for(File searchFile : searchFiles)
            {
                try {
                    System.out.print(searchFile.getAbsolutePath());
                    fileReader = new FileReader(searchFile);
                    br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf(xyz) >= 0) {
                            sb.append(line);
                        }
                    }
     
                } catch (Exception e) {
                    if((fileReader != null) && (br != null))
                    {
                        try {
                            br.close();
                            fileReader.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                   
                    e.printStackTrace();
                }
            }
            return    sb.toString();
        }
    greetz blackwolf12333
     
  18. you not closing the file when there is no exception, your not closing the file when there was an Error, close files at the finaly block to cover al cases.
    May be the problem that inside the file its "x,y,z" and inside the code "x, y, z" or something?
     
  19. the x, y, z is correct, i wrote the part where it gets written to the file too, so i am sure that should be it..., and ok, indeed still had to add it in the final statement, will do that now...

    Ok, now my problem is, this function does work:
    Code:
    public void searchText(String text, CommandSender p) {
           
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            FileReader fileReader = null;
            BufferedReader br = null;
           
            for(File searchFile : searchFiles)
            {
                try {
                    fileReader = new FileReader(searchFile);
                    br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf(text) >= 0) {
                            p.sendMessage(line);
                        }
                    }
     
                } catch (Exception e) {
                    if((fileReader != null) && (br != null))
                    {
                        try {
                            br.close();
                            fileReader.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                   
                    e.printStackTrace();
                } finally {
                    if((fileReader != null) && (br != null))
                    {
                        try {
                            br.close();
                            fileReader.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                }
            }
        }
    and this function doesn't, and it is almost the same...:
    Code:
    public String searchPos(int x, int y, int z) {
            String xyz = z + ", " + y + ", " + z;
            StringBuffer sb = new StringBuffer();
            File[] searchFiles = new File[files.size()];
            searchFiles = files.toArray(searchFiles);
           
            FileReader fileReader = null;
            BufferedReader br = null;
           
            for(File searchFile : searchFiles)
            {
                try {
                    fileReader = new FileReader(searchFile);
                    br = new BufferedReader(fileReader);
                    String line = "";
     
                    while ((line = br.readLine()) != null) {
                        if ((line.indexOf(xyz) >= 0) || (line.contains(xyz))) {
                            sb.append(line);
                        }
                    }
     
                } catch (Exception e) {
                    if((fileReader != null) && (br != null))
                    {
                        try {
                            br.close();
                            fileReader.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                   
                    e.printStackTrace();
                } finally {
                    if((fileReader != null) && (br != null))
                    {
                        try {
                            br.close();
                            fileReader.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
            return    sb.toString();
        }
    The only difference is that with the first function i am directly sending the results to the sender of the command and with the second one i am first adding it to an StringBuffer, and then i return the string of the StringBuffer...
    greetz blackwolf12333

    Fixed it, one hell of an epic fail...see this:
    Code:
    String xyz = z + ", " + y + ", " + z;
    it should be x y z, not zyz...:( i feel so stupid now...:(
    anyway thanks for helping:D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
Thread Status:
Not open for further replies.

Share This Page