ReadFile

Discussion in 'Plugin Development' started by wouter0100, Jul 13, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    wouter0100

    Hey

    How can i do this simple?
    In the file stand this:
    [name of the player] [yyyy/mm/dd/hh]

    Code:
    http://pastebin.com/0tydrr9E
    (It need to check if the times are same ore lower then true else false.)
    It need to give true ore false back, but dont work?
    I hope you can help me.

    Thanks :)

    sorry, but nobody?
    i need to fix this fast..
    and i need to learn how i fix this :(!
    (my first plugin and in flops :()

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

    Hretsam

  3. Offline

    wouter0100

    uu, thanks, i'am looking now xD
    //edit
    never match something :(?
     
  4. Offline

    Hretsam

    I ment about getting the data out an formatting it

    As for the dates, if they all follow the same format, parse it and compare the long values.
     
  5. Offline

    wouter0100

    That works..
    Only the check if the current time is lower ore some then the time in the file don't work good.
    Show Spoiler

    Code:
    		        			if(Integer.parseInt(s_temp2[0]) >= Integer.parseInt(s_temp[0]))
    		        			{
    		        				log(""+s_temp2[0]+" "+s_temp[0]);
    		        				if(Integer.parseInt(s_temp2[1]) >= Integer.parseInt(s_temp[1]))
    			        			{
    		        					log(""+s_temp2[1]+" "+s_temp[1]);
    		        					if(Integer.parseInt(s_temp2[2]) <= Integer.parseInt(s_temp[2]))
    				        			{
    		        						log(""+s_temp2[2]+" "+s_temp[2]);
    		        						if(Integer.parseInt(s_temp2[3]) <= Integer.parseInt(s_temp[3]))
    		    		        			{
    		        							log(""+s_temp2[3]+" "+s_temp[3]);
    		        							in.close();
    		        							return true;
    		    		        			}else{
    		    		        				in.close();
    		    		        				return false;
    		    		        			}
    				        			}else{
    				        				in.close();
    				        				return false;
    				        			}
    			        			}else{
    			        				in.close();
    			        				return false;
    			        			}
    		        			}else{
    		        				in.close();
    		        				return false;
    		        			}
    
     
  6. Offline

    Hretsam

    something like:
    Code:
    public boolean ReadFile(Player player, String checkdate) {
            String strLine;
            String[] split;
            String p = player.getName();
    
            DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            Date date = format.parse(checkdate), inputDate;
    
            try {
                FileInputStream fstream = new FileInputStream(playersListFile);
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader bw = new BufferedReader(new InputStreamReader(in));
    
                while ((strLine = bw.readLine()) != null) {
    
                    split = strLine.split(" ");
    
                    //log("ReadFile out: ."+p+". ."+split[0]+".");
    
                    if (p.compareTo(split[0]) == 0) {
    
                        inputDate = format.parse(split[1]);
    
                        if (inputDate.getTime() <= date.getTime()) {
                            return true;
                        } else {
                            return false;
                        }
                        in.close();
                    }
                    return false;
                }
                in.close();
            } catch (Exception e) {
                log("Error: " + e.getMessage());
            }
    
            return false;
        }
     
  7. Offline

    wouter0100

    Code:
    Description    Resource    Path    Location    Type
    Type mismatch: cannot convert from java.util.Date to java.sql.Date    StarterIsAGod.java    /StarterIsAGod/src/me/Wouter0100/StarterIsAGod    line 364    Java Problem
    Type mismatch: cannot convert from java.util.Date to java.sql.Date    StarterIsAGod.java    /StarterIsAGod/src/me/Wouter0100/StarterIsAGod    line 379    Java Problem
    
     
  8. Offline

    Hretsam

    what are your imports?
     
  9. Offline

    wouter0100

    Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.sql.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.HashMap;
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import org.bukkit.util.config.Configuration;
    
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
     
  10. Offline

    Hretsam

    import java.util.date not java.sql.date
     
  11. Offline

    wouter0100

    in my script go something wrong with split.
    1 player works fine.
    But if the 2e join, it go wrong :(.

    //edit:
    Code:
    Description	Resource	Path	Location	Type
    Unhandled exception type ParseException	StarterIsAGod.java	/StarterIsAGod/src/me/Wouter0100/StarterIsAGod	line 364	Java Problem
    Unreachable code	StarterIsAGod.java	/StarterIsAGod/src/me/Wouter0100/StarterIsAGod	line 386	Java Problem
    
    
     
  12. Offline

    s1mpl3x

    whole class would help alot
     
  13. Offline

    Hretsam

    btw, if you havn't noticed the code thinks all your dates are in the format day/month/year (with slashes!)
     
  14. Offline

    wouter0100

  15. Offline

    Hretsam

    Use this, and post the outcome
    Code:
    public boolean ReadFile(Player player, String checkdate) {
            String strLine;
            String[] split;
            String p = player.getName();
            DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            Date date, inputDate;
    
            try {
                date = format.parse(checkdate);
            } catch (ParseException pe) {
                System.out.println(checkdate + " - " + pe.getMessage());
            }
            try {
                FileInputStream fstream = new FileInputStream(playersListFile);
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader bw = new BufferedReader(new InputStreamReader(in));
    
                while ((strLine = bw.readLine()) != null) {
    
                    split = strLine.split(" ");
    
                    //log("ReadFile out: ."+p+". ."+split[0]+".");
    
                    if (p.compareTo(split[0]) == 0) {
    
                        try {
                            inputDate = format.parse(split[1]);
                        } catch (ParseException pe) {
                            System.out.println(split[1] + " - " + pe.getMessage());
                            return false;
                        }
    
                        if (inputDate.getTime() <= date.getTime()) {
                            return true;
                        } else {
                            return false;
                        }
                        in.close();
                    }
                    return false;
                }
                in.close();
            } catch (Exception e) {
                log("Error: " + e.getMessage());
            }
    
            return false;
        }
     
  16. Offline

    s1mpl3x

    1. error: surround with try/catch
    2. error: the in.close() can't be reached because in the if/else above you already return on both cases
     
  17. Offline

    wouter0100

    , i think dad i have used the wrong code, but iam very sleepy.
    Nothing...

    and how can i fix this?
    and i go to sleep now ...
     
  18. Offline

    Hretsam

    Nothing, did you run the code?

    1. i fixed that with my code
    2. remove the 1in.close under
    Code:
                      if (inputDate.getTime() <= date.getTime()) {
                             return true;
                        } else {
                             return false;
                         }
                         in.close(); //<----- this one
     
  19. Offline

    wouter0100

    Okay, thanks, i'am back and now i wil test it good :)
    //edit
    i got this error:

    Code:
    Description    Resource    Path    Location    Type
    The local variable date may not have been initialized    StarterIsAGod.java    /StarterIsAGod/src/me/Wouter0100/StarterIsAGod    line 391    Java Problem
    
    I use Eclipse and he say change this:
    Date date, inputDate;
    to this:
    Date date = null, inputDate;

    and time is
    yyyy/MM/dd/kk (kk = hour?)

    and why we create a time?
    The checktime is the current time, and the filetime that is in the file, that we need to read.
     
  20. Offline

    Hretsam

    If checktime is the current time, a better thing todo is
    Date date = new Date(); this wil create a new object with the current time.

    And yes kk is hour
     
  21. Offline

    wouter0100

    I changed System.out.println to log.

    And nothing in the server console? (Log works!!!)
     
  22. Offline

    Hretsam

    Let it print out something before it enters the try catch.
    Also, make sure the method is called!
     
  23. Offline

    wouter0100

  24. Offline

    Hretsam

    Ok, what is the result?
     
  25. Offline

    wouter0100

    sorry that you need to wait solong..
    but something go wrong :(
     
  26. Offline

    Hretsam

    I see the problem, the 7 is 1 long, not 2.x
    You need to split the string (like you did before) and check if the length is 2 (for month, day & hours). If the length is not 2 just add a "0" in front of it, then combine the string again and run it trough the formatter.
     
  27. Offline

    wouter0100

    edit
    sorry thats not all.
     
  28. Offline

    Hretsam

    try this:
    (hope you understand the code)
    Code:
    public boolean ReadFile(Player player, String checkdate) {
            String strLine;
            String[] split;
            String p = player.getName();
    
            DateFormat format = new SimpleDateFormat("yyyy/MM/dd/kk");
            Date currentDate = new Date(), inputDate;
    
            try {
                FileInputStream fstream = new FileInputStream(playersListFile);
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader bw = new BufferedReader(new InputStreamReader(in));
    
                while ((strLine = bw.readLine()) != null) {
    
                    split = strLine.split(" ");
    
                    //log("ReadFile out: ."+p+". ."+split[0]+".");
    
                    if (p.compareTo(split[0]) == 0) {
    
                        split = split[1].split("/");
    
                        try {
                            inputDate = format.parse(split[0]
                                    + "/" + (split[1].length() == 2 ? split[1] : "0" + split[1])
                                    + "/" + (split[2].length() == 2 ? split[2] : "0" + split[2])
                                    + "/" + (split[3].length() == 2 ? split[3] : "0" + split[3]) );
                        } catch (ParseException pe) {
                            System.out.println("ParseException " + pe);
                            inputDate = new Date();
                        }
    
                        if (inputDate.getTime() <= currentDate.getTime()) {
                            return true;
                        } else {
                            return false;
                        }
                        in.close();
                    }
                    return false;
                }
                in.close();
            } catch (Exception e) {
                log("Error: " + e.getMessage());
            }
    
            return false;
        }
     
  29. Offline

    wouter0100

    Code:
                                if (inputDate.getTime() <= currentDate.getTime()) {
                                    return true;
                                } else {
                                    return false;
                                }
                                in.close(); <<< away?!?
                            }
                            return false;
     
  30. Offline

    Hretsam

    yes, sorry used old code
     
Thread Status:
Not open for further replies.

Share This Page