Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: help with errors

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question help with errors

    I've had some serious troubles getting this class to compile. I'm just looking for a little help with this.
    import java.util.*; //Date
    import java.text.*; //.DateFormat, .SimpleDateFormat
     
    public class Time {
     
    	public int hours = 0;
    	public int minutes = 0;
     
    	//no arg constructor	
    	public Time() { }
     
    	//Default Constructor
    	public Time(int inHours, int inMinutes) {
    		this.hours = inHours;
    		this.minutes = inMinutes;
    	}
     
    	//Hours accessor
    	public int getHours() {
    		return hours;
    	}
    	//Minutes accessor	
    	public int getMinutes() {
    		return minutes;
    	}
     
    	public String fromString(String stringTime) {
     
    		//overrides .Object method toString() to use this classes toString() method
    		//to split 24-hour time to hours and minutes
    		//DEAD CODE--Time newTime = new Time(hours, minutes).toString(stringTime);
     
    		//Convert stringTime to minutes then take the difference between that and 
    		//total minustes in a day (1440)
     
    		//DEAD CODE--int timeDiff = 1440 - stringTime.toMinutes();
     
    		//need to figure out how to determine if newTime is out of range
    		if (!outOfRange()) { 
    			return stringTime;
    		}
    		else {
    			System.out.print("The time is out of range, program terminating");
    		}		
    	} //end fromString method
     
    	@Override
    	public String toString() {
    		//Converts a time object to 24hr format string
    		//Display hours and minutes in 2 spaces each
    		//Display leading zeros for minute field
    		//use : as separator
     
    		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
     
    		try {
    			Date parseTime = timeFormat.parse(new Time(hours, minutes)); //This isn't correct--> "parse(timeFormat)"
    																	 //can't determine what is parsed here.
    			return timeFormat.format(parseTime);
    		}
    		catch (ParseException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public String toAMPMString() {
    		//public instance method
    		//Display hours and minutes in 2 spaces each	
    		//Display leading zeros for minute field	
    		//use : as separator
    		//AM/PM follows the hours and minutes
    		String THour = "";
    		int temp = 0;
     
    		DecimalFormat twoDigits = new DecimalFormat("00");
     
    		// checking for noon and midnight
    		if (hours == 0 || hours == 12) {
    			temp = 12;
    		}
    		// cast hour into correct number of hours since 0000/1200
    		else {
    			temp = hours % 12;
    		}
     
    		// determine whether it is AM or PM by hour
    		if (hours < 12) {
    			THour = "AM";
    		}
    		else { 
    			THour = "PM"; 
    		}
     
    		return temp + ":" + twoDigits.format(hours) + ":" + twoDigits.format(minutes) + " " + THour;
    	}
     
    	@Override
    	public int compareTo(int arrival, int departure) {
    		//String isBefore = arrival.compareTo(departure);
     
    		//Arrival is before departure
    		if (arrival.toMinutes() < departure.toMinutes())
    			return -1;
    		//Times are equal
    		else if (arrival.toMinutes() == departure.toMinutes())
    			return 0;
    		//Arrival is after departure
    		else
    			return 1;
    	}
     
    	protected int toMinutes() {
    		//returns time in minutes since midnight
    		SimpleDateFormat dateFormat = new SimpleDateFormat("HH");
    		Date date = new Date(Time(hours, minutes));
    		int hour = Integer.parseInt(dateFormat.format(date));         
     
    		SimpleDateFormat dateFormat = new SimpleDateFormat("mm");
    		Date date = new Date(Time(hours, minutes));
    		int minute = Integer.parseInt(dateFormat.format(date));
     
    		//Minutes from midnight
    		int minFromMidnight = (hours* 60) + minute;
    	}
     
    	public static boolean outOfRange() {
    		//return true if hours and minutes are not within proper range of values
    		//return false otherwise
    		if ((hours > 23 || hours < 0) && (minutes > 59 || minutes < 0)) {
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
     
    } //end of class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: help with errors

    troubles getting this class to compile
    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with errors

    Time.java:65: cannot find symbol
    symbol : method parse(Time)
    location: class java.text.SimpleDateFormat
    Date parseTime = timeFormat.parse(new Time(hours, minutes)); //This isn't correct--> "parse(timeFormat)"
    ^
    Time.java:110: int cannot be dereferenced
    if (arrival.toMinutes() < departure.toMinutes())
    ^
    Time.java:110: int cannot be dereferenced
    if (arrival.toMinutes() < departure.toMinutes())
    ^
    Time.java:113: int cannot be dereferenced
    else if (arrival.toMinutes() == departure.toMinutes())
    ^
    Time.java:113: int cannot be dereferenced
    else if (arrival.toMinutes() == departure.toMinutes())
    ^
    Time.java:105: method does not override or implement a method from a supertype
    @Override
    ^
    Time.java:123: cannot find symbol
    symbol : method Time(int,int)
    location: class Time
    Date date = new Date(Time(hours, minutes));
    ^
    Time.java:126: dateFormat is already defined in toMinutes()
    SimpleDateFormat dateFormat = new SimpleDateFormat("mm");
    ^
    Time.java:127: date is already defined in toMinutes()
    Date date = new Date(Time(hours, minutes));
    ^
    Time.java:127: cannot find symbol
    symbol : method Time(int,int)
    location: class Time
    Date date = new Date(Time(hours, minutes));
    ^
    Time.java:137: non-static variable hours cannot be referenced from a static context
    if ((hours > 23 || hours < 0) && (minutes > 59 || minutes < 0)) {
    ^
    Time.java:137: non-static variable hours cannot be referenced from a static context
    if ((hours > 23 || hours < 0) && (minutes > 59 || minutes < 0)) {
    ^
    Time.java:137: non-static variable minutes cannot be referenced from a static context
    if ((hours > 23 || hours < 0) && (minutes > 59 || minutes < 0)) {
    ^
    Time.java:137: non-static variable minutes cannot be referenced from a static context
    if ((hours > 23 || hours < 0) && (minutes > 59 || minutes < 0)) {
    ^
    14 errors

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: help with errors

    int cannot be dereferenced
    if (arrival.toMinutes()
    The variable: arrival is not a class object. It is an int. ints do not have methods.

    non-static variable hours cannot be referenced from a static context
    The variable: hours is a class variable that only exists when there is an instance of that class.
    Create an instance of the class and use that reference:
    Time time = new Time(); // create an instance of the class
    time.hours //<<< this way to get to the hours variable in the Time class

    date is already defined in toMinutes()
    This one says in English what the problem is. Read the text of the message! You can only define a variable one time in a method.

Similar Threads

  1. Can't fix my own errors
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 09:20 AM
  2. GUI errors
    By cindisue in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2011, 09:54 AM
  3. Many Errors
    By Woody619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2010, 09:36 PM
  4. Getting errors
    By Nonire in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2010, 12:21 PM
  5. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 04:41 AM