Results 1 to 14 of 14
  1. #1
    ustar is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default Code does not Compile

    Hi my code below doesn't compile. It says Cannot find symbol variable Time. And I'm pretty sure it well also say cannot find symbol time2. I would really appreciate if some could tell me what is causing the problem.

    /**
    This class compares two military times and
    determines which comes first.
    */
    public class Time
    {
    private int hour;
    private int minute;
    private int Time1x;
    private int Time2x;

    public Time(int anHour, int aMinute)
    {
    hour= anHour/60;
    minute=aMinute%60;

    }

    /**
    Compares this time against another time.
    @param time2 the time to compare with
    @return "before" if this time comes before time2,
    "" if the times are the same, and "after" otherwise
    */
    public String compareWith(Time time2)
    { String speaker;
    Time1x=Time;
    Time2x=time2;
    if(Time2x>Time1x)
    speaker="before";
    else if (Time1x<Time2x)
    speaker="after";
    else
    speaker="Please Try again the Program is Busy";

    return speaker;

    }
    }




    USED TO TEST THE PROGRAM:
    import java.util.Scanner;

    /**
    This program compares two military times and
    determines which comes first.
    */
    public class TimeComparer
    {
    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);

    System.out.println("Please enter the first time in military time (hour minute): ");
    int hour1 = in.nextInt();
    int minute1 = in.nextInt();

    System.out.println("Please enter the second time in military time (hour minute): ");
    int hour2 = in.nextInt();
    int minute2 = in.nextInt();

    Time time1 = new Time(hour1, minute1);
    Time time2 = new Time(hour2, minute2);

    String comp = time1.compareWith(time2);
    System.out.println("The first time is " + comp + " the second time.");
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Java Code:
    Time1x=Time;
    Time2x=time2;
    Where you define Time1x and Time2x?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    ustar is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Java Code:
    Time1x=Time;
    Time2x=time2;
    Where you define Time1x and Time2x?
    Its defined in the main class as
    public class Time
    {
    private int hour;
    private int minute;
    private int Time1x;
    private int Time2x;

    also the complete error message is "Cannot find symbol variable Time".

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by ustar
    "Cannot find symbol variable Time".
    This is not the complete error message. You must have some additional details there, with the line number and all.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Look at the first link I pointed to you.

    Java Code:
    Time1x=Time;
    Where you define that Time variable?

  7. #7
    ustar is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    I'm using bluej and the only message it gives is that. Also the part below is highlighted or the third line to be exact.
    public String compareWith(Time time2)
    { String speaker;
    Time1x=Time;-----------------------------------------This is highlighted along with the error message "Cannot find symbol variable Time".
    Time2x=time2;

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by ustar View Post
    public String compareWith(Time time2)
    { String speaker;
    Time1x=Time;-----------------------------------------This is highlighted along with the error message "Cannot find symbol variable Time".
    Time2x=time2;
    That's true. Where you define that Time variable in your class? I cannot see any.

  9. #9
    ustar is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    hey thanks for the help, so i just defined the Time variable and the Time2 variable.
    But the next error I'm getting is "incompataible types- found Time but expected int

    with the following part highlighted
    { String speaker;
    Time1x=Time;
    Time2x=time2; ---------------------------------------- this line is highlighted

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes, the types are not compatible.

    Time2x is an int, primitive data type.

    Java Code:
    private int Time2x;
    But the time2 is an object of Time.

    Java Code:
    public String compareWith(Time time2)
    You have to change those things accordingly, to your application. What you are suppose to do with the Time class?

  11. #11
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Just saying, couldn't you just use two integers and determine which number is bigger since its military time? ie if (Time1 > Time2)
    And don't ask for it as hours and minutes. Just ask for it like 1430. That's the way military time is usually displayed anyways. This way you don't need all those classes. I generally try to avoid using multiple classes in simple problems like this one. Of course, I may be misinterpreting your comments, as they are kinda confusing. What I'm getting from them is that you are making a program that accepts two military times and tells you which one is earlier in the day. In the future, try to explain what your program does outside of the code. I realize it's all in your comments, but it's a lot simpler if it can all be in one block.
    It seems like that would be a much much simpler solution to the problem, and the simpler the program, the less likely it is that you're going to have bugs.

  12. #12
    ustar is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    Hi this is my new code and it does compile but results are not as expected.

    /**
    This class compares two military times and
    determines which comes first.
    */
    public class Time
    {
    private int hour;
    private int minute;
    private int Time1x;
    private int Time2x;
    private int Time;
    private int time2;
    //the function of this prgram is to find the difference between to military times
    // what can me done convert the first time into hours and minutes. THen do the same thing for the second time. After that subtract
    //the difference from each other

    public Time(int anHour, int aMinute)
    {
    hour= anHour/60;
    minute=aMinute%60;

    }

    /**
    Compares this time against another time.
    @param time2 the time to compare with
    @return "before" if this time comes before time2,
    "" if the times are the same, and "after" otherwise
    */

    // I need Help on the part below

    // I need Help on the part below

    // I need Help on the part below

    // I need Help on the part below
    public String compareWith(Time time2)
    { String speaker;
    Time1x=Time;
    //also I can't set Time2x=time2 it ssays int variable expected

    if(Time2x>Time1x)
    speaker="before";
    else if (Time1x<Time2x)
    speaker="after";

    else
    speaker="";

    return speaker;

    }
    }


    Tester Class
    import java.util.Scanner;

    /**
    This program compares two military times and
    determines which comes first.
    */
    public class TimeComparer
    {
    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);

    System.out.println("Please enter the first time in military time (hour minute): ");
    int hour1 = in.nextInt();
    int minute1 = in.nextInt();

    System.out.println("Please enter the second time in military time (hour minute): ");
    int hour2 = in.nextInt();
    int minute2 = in.nextInt();

    Time time1 = new Time(hour1, minute1);
    Time time2 = new Time(hour2, minute2);

    String comp = time1.compareWith(time2);
    System.out.println("The first time is " + comp + " the second time.");
    }
    }

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by ustar View Post
    Hi this is my new code and it does compile but results are not as expected.
    Then something wrong with your implementation or the logic of your application. Go through the code again and check. Best things you've to do is that take a paper and write down your logic there and check it with your code.

    What you are exactly doing with your code?

  14. #14
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    The problem is that you seem have many more fields than you really need to define a time class, unless there are unstated requirements.

    You need two fields -> Hour and Minute.

    You have 6. Why?

    Now you want to compare two time objects.
    Java Code:
    public String compareWith(Time time2)
    { String speaker;
       Time1x=Time;  //This Time and Time1x
    //also I can't set Time2x=time2 it says int variable expected
    
       if(Time2x>Time1x)
          speaker="before";
        else if (Time1x<Time2x)
           speaker="after";
        else
           speaker="";
    
       return speaker;
    
    }

    Now follow the logic by hand. Your constructor code sets hour and minute. Ok so far.

    Now look at the compare function after creating time objects.
    Time1x=Time; //This Time and Time1x variables are from the calling object i.e. Time1 in the expression time1.compare(time2);
    These are set to default values of zero and have not been changed.
    Similarly:
    if(Time2x>Time1x)Time2x likewise is zero - it has never been set.

    Why would this function work when the values you are comparing are never referenced?

    Some of your previously posted code has referenced variables with the same name, but where one was a local variable to main, and the other was an object field variable. Make sure you understand the difference and the scope of each variable.

Similar Threads

  1. Online IDE to edit and compile code?
    By DarrenReeder in forum Other IDEs
    Replies: 1
    Last Post: 02-06-2011, 01:58 AM
  2. error while compile code using javac
    By suri in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 09:10 AM
  3. Code will not compile
    By ShotGunRockets in forum New To Java
    Replies: 17
    Last Post: 05-10-2009, 03:31 AM
  4. Compile/Execute code in Java app
    By Doctor Cactus in forum New To Java
    Replies: 5
    Last Post: 12-16-2008, 09:58 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
  •