Results 1 to 14 of 14
Thread: Code does not Compile
- 03-04-2010, 03:38 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
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.");
}
}
- 03-04-2010, 04:04 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Where you define Time1x and Time2x?Java Code:Time1x=Time; Time2x=time2;
- 03-04-2010, 04:04 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you post your complete error message?
- 03-04-2010, 04:09 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
- 03-04-2010, 04:12 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
This is not the complete error message. You must have some additional details there, with the line number and all.
Originally Posted by ustar
- 03-04-2010, 04:14 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Look at the first link I pointed to you.
Where you define that Time variable?Java Code:Time1x=Time;
- 03-04-2010, 04:15 AM #7
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
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;
- 03-04-2010, 04:18 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-04-2010, 04:28 AM #9
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
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
- 03-04-2010, 04:36 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, the types are not compatible.
Time2x is an int, primitive data type.
But the time2 is an object of Time.Java Code:private int Time2x;
You have to change those things accordingly, to your application. What you are suppose to do with the Time class?Java Code:public String compareWith(Time time2)
- 03-04-2010, 07:35 PM #11
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
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.
- 03-05-2010, 02:42 AM #12
Member
- Join Date
- Feb 2010
- Posts
- 7
- Rep Power
- 0
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.");
}
}
- 03-05-2010, 04:00 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-06-2010, 04:54 PM #14
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
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
-
Online IDE to edit and compile code?
By DarrenReeder in forum Other IDEsReplies: 1Last Post: 02-06-2011, 01:58 AM -
error while compile code using javac
By suri in forum New To JavaReplies: 3Last Post: 01-20-2010, 09:10 AM -
Code will not compile
By ShotGunRockets in forum New To JavaReplies: 17Last Post: 05-10-2009, 03:31 AM -
Compile/Execute code in Java app
By Doctor Cactus in forum New To JavaReplies: 5Last Post: 12-16-2008, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks