Results 1 to 9 of 9
  1. #1
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default Why dosent return new string

    How to reload a class?

    Here's a example
    Class Car.getCar(); returns car name string from a file.

    Car car = new Car();
    car.getCar(); returns Honda

    MODIFIE CAR FILE(REPLACE HONDA WITH BMW)

    car modifCar = new Car();

    modifCar.getCar(); returns Honda

    if I restart my app then it returns bmw.

    Why dosent the second one return bmw?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What? :confused:
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    I made a app where I can modifie a file.

    If I run my app then it displays that file.
    Car car = new Car();
    System.out.println(car.getCar());

    OUTPUT: honda

    I have fileWatcher what tracks modifications.(fileWatcher with timer)

    if file is modified then it makes a new object called Car modifcar = new Car();
    In this case I changed honda to bmw;

    and after that it displays
    System.out.println(modifcar.getCar());

    OUTPUT: honda

    My english is bad, sorry!!

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You probably didn't change the file.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Could be any number of things.
    Since we can't see your code we can't say what assumption you have made that is incorrect.

  6. #6
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Class for saving
    The object what will be saved comes from a jSpinner
    Java Code:
    /**
     *Class for saving notify timer time
     * @author kaarel
     */
    public class AddNotify {
        
        //Default date
        String date = null;
    
        public AddNotify(Object e){
    
            //Convert Object to String
            date = ObjectToString(e);
    
            //save date to file
            SaveToFile(date);
    
    
        }
    
        //Convert Spinner object  to string
        private String ObjectToString(Object date){
    
            //Convert object to string
            String allDate = date.toString();
    
            //Spilt into array to get time
            String [] allDateSplit = allDate.split(" ");
    
            //Get time from array
            String time = allDateSplit[3];
    
    
    
            //Return date
            return time;
        }
    
        //Save time to file
        private void SaveToFile(String date){
    
    
            try{
                //Create file
                FileWriter write = new FileWriter("notify.dat");
                    BufferedWriter out = new BufferedWriter(write);
    
    
            //Write to file
            out.write(date);
    
            //Close stream
            out.close();
    
            }catch(IOException e){System.err.println(e.getMessage());}
        }
    
    }

    CLASS FOR GENERATING DATE FOR TIMER

    TimerDate timerdate = new TimerDate();
    timerdate.getDate() returns a modified date for the notification timer

    Java Code:
    /**
     *Class for making date for Notify timer
     * @author
     */
    public class TimerDate {
    
        //Get time array
        static String [] timeArr = TimeToArray();
    
        //Needed Data to run task once per day
       public final static  long fonce_per_day = 1000*60*60*24;
        final static   int fone_day = 0;
        final static   int hour = Integer.parseInt(timeArr[0]);
        final static  int minute = Integer.parseInt(timeArr[1]);
    
    
    
        public TimerDate(){
    
    
    
        }
    
        //Get time from file
        public static String getTime(){
    
            //String for time
            String time = "null";
            try{
    
            FileInputStream stream = new FileInputStream("notify.dat");
            //Get object of datainputstream
            DataInputStream dis = new DataInputStream(stream);
            //read file
            BufferedReader br = new BufferedReader(new InputStreamReader(dis));
    
            //String for time
            time = br.readLine();
    
            //Close stream
            stream.close();
    
            }catch(Exception e){ System.err.println(e.getMessage());}
    
           //Return time
           return time;
        }
    
        //Create array from time(hour, minut, sec)
        private static String[] TimeToArray(){
    
            //Array for time
            String[] time = getTime().split(":");
    
            //Return time array
            return time;
    
        }
    
    
        public Date getDate(){
    
        //Setup calendar
            Calendar calendar = new GregorianCalendar();
    
            calendar.add(Calendar.DATE,fone_day);
    
            Calendar result = new GregorianCalendar(
              calendar.get(Calendar.YEAR),
              calendar.get(Calendar.MONTH),
              calendar.get(Calendar.DATE),
              hour,
              minute
        );
    
            //return date for timer
            return result.getTime();
        }
    
    }
    THIS IS MY NOTIFICATION TIMER CLASS
    Java Code:
    public final  class Notify extends Display  {
    
        //Default Decleration
        String date = null;
    
         //GUI timer running
        boolean GTimerRunning = false;
    
    
    
        public Notify(){
    
            //Check time file for modifications
            TimerTask task = new FileWatcher(new File("notify.dat")){
    
                //If file was modified run this method
                protected void Changes(){
    
                    //restart notify timer
                    GTimerRunning = false;
                    
                    
                    //If GTimerRunning is false, restart notfy timer
                    if(!GTimerRunning){
    
                        //Get new Time
                        TimerDate ModifDate = new TimerDate();
                        Date modifiedDate = ModifDate.getDate(); <---EXAMPLE: OUT PUT STILL 05:30PM, IF I RESTART MY APP THEN IT DISPLAYS THAT NEW DATE THAT I INSERTED.
                        System.out.println(new TimerDate().getDate());
                        
                        
                        //Display main GUI when time is reached
                        Timer TimerGUI = new Timer();
    
                        TimerGUI.scheduleAtFixedRate(new TimerTask() {
                            public void run() {
    
                                dis.setVisible(true);
                             
    
    
                            }
                         },modifiedDate,ModifDate.fonce_per_day);
                    
                    
                    GTimerRunning = true;
                    
                    }
    
    
                }
    
            };
    
    
            //Modification timer
            Timer ModiTimer = new Timer();
            ModiTimer.schedule(task,0, 1000);
    
    
    
            //Display main GUI when time is hit
            if(!GTimerRunning){
    
                //Get date for timer
                TimerDate timerDate = new TimerDate();
                 Date newDate = timerDate.getDate();  <------EXAMPLE: OUTPUT= 05:30PM
    
                Timer TimerGUI = new Timer();
    
                TimerGUI.scheduleAtFixedRate(new TimerTask() {
                            public void run() {
    
                                 dis.setVisible(true);
    
    
    
                            }
                },newDate,timerDate.fonce_per_day);
    
                GTimerRunning = true;
            }
    
            
    
    
           
            
        }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Quote Originally Posted by karq View Post
    Class for saving
    The object what will be saved comes from a jSpinner
    Java Code:
    /**
     *Class for saving notify timer time
     * @author kaarel
     */
    public class AddNotify {
        
        //Default date
        String date = null;
    
        public AddNotify(Object e){
    
            //Convert Object to String
            date = ObjectToString(e);
    
            //save date to file
            SaveToFile(date);
    
    
        }
    
        //Convert Spinner object  to string
        private String ObjectToString(Object date){
    
            //Convert object to string
            String allDate = date.toString();
    
            //Spilt into array to get time
            String [] allDateSplit = allDate.split(" ");
    
            //Get time from array
            String time = allDateSplit[3];
    
    
    
            //Return date
            return time;
        }
    
        //Save time to file
        private void SaveToFile(String date){
    
    
            try{
                //Create file
                FileWriter write = new FileWriter("notify.dat");
                    BufferedWriter out = new BufferedWriter(write);
    
    
            //Write to file
            out.write(date);
    
            //Close stream
            out.close();
    
            }catch(IOException e){System.err.println(e.getMessage());}
        }
    
    }
    Is the Object a java.util.Date? If so then just use a SimpleDateFormat to turn it into a suitable String instead of faffing with all that stuff in ObjectToString. At least then you should have a fair idea of what's being sent to the file.

    Quote Originally Posted by karq View Post
    CLASS FOR GENERATING DATE FOR TIMER

    TimerDate timerdate = new TimerDate();
    timerdate.getDate() returns a modified date for the notification timer

    Java Code:
    /**
     *Class for making date for Notify timer
     * @author
     */
    public class TimerDate {
    
    // SNIP stuff.
    
            FileInputStream stream = new FileInputStream("notify.dat");
            //Get object of datainputstream
            DataInputStream dis = new DataInputStream(stream);
            //read file
            BufferedReader br = new BufferedReader(new InputStreamReader(dis));
    That DataInputStream is a waste of time...and I wouldn't be surprised if that's not munging your data somehow. Get rid of it.

    Quote Originally Posted by karq View Post
    Java Code:
            //String for time
            time = br.readLine();
    
            //Close stream
            stream.close();
    
            }catch(Exception e){ System.err.println(e.getMessage());}
    
           //Return time
           return time;
        }
    Close the buffered reader, not the file stream. The reader should close the stream. And do this in a finally block.

    I'm going to snip the rest of that because I'm having trouble making head nor tail of it.

    First sort out how you read from and write to the file, using standard classes. Then stick some debugging code in there, printing out what's read in when it's read in, and what's written out. I see precious little of that in there.

  8. #8
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Then stick some debugging code in there, printing out what's read in when it's read in, and what's written out. I see precious little of that in there.
    This did the trick :) Printed everything out and found the bug.

    Here's the solution
    Java Code:
    public Date getDate(){
    
        //Get info from file
        String [] timeArr = TimeToArray();
    
        //Needed Data to run task once per day
       
        final   int fone_day = 0;
        final   int hour = Integer.parseInt(timeArr[0]);
        final  int minute = Integer.parseInt(timeArr[1]);
    
        //Setup calendar
            Calendar calendar = new GregorianCalendar();
    
            calendar.add(Calendar.DATE,fone_day);
    
            Calendar result = new GregorianCalendar(
              calendar.get(Calendar.YEAR),
              calendar.get(Calendar.MONTH),
              calendar.get(Calendar.DATE),
              hour,
              minute
        );
            System.out.println("timerDate:" + result.getTime());
            //return date for timer
            return result.getTime();
        }
    I also fixed the ObjectToString(used SimpleDateFormat), deleted DataInputStream and closed the bufferedreader.


    THANKS ALOT :)

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    No problem.

    System.out.println() is your friend.
    :)

    ETA: Apparently there's a way of marking threads as SOLVED...

Similar Threads

  1. why dosent the 2d array change?
    By TheBreadCat in forum New To Java
    Replies: 11
    Last Post: 05-15-2010, 01:20 PM
  2. How do i return a random String from an array?
    By Grendel0 in forum New To Java
    Replies: 9
    Last Post: 03-11-2010, 10:11 AM
  3. Static String Return Type
    By Java_Developer in forum New To Java
    Replies: 17
    Last Post: 10-03-2009, 06:07 PM
  4. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 04:56 AM
  5. HashMap String Return
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 03:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •