Results 1 to 4 of 4
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default why does this program not execute

    The program below does not run. I don't underdstand why.:(

    Date.java
    Java Code:
    import java.util.Calendar;
    
    public class Date 
    {
    	Calendar time = Calendar.getInstance();
    	
    	private int i_day;
    	private String day;
    	private int date;
    	private int i_month;
    	private String month;
    	private int year;
    	
    	public Date()
    	{
    		i_day = time.get(Calendar.DAY_OF_WEEK);
    		day = getDay(i_day);
    		
    		i_month = time.get(Calendar.MONTH);
    		month = getMonth(i_month);
    		
    		date = time.get(Calendar.DATE);
    		
    		year = time.get(Calendar.YEAR);
    	}
    	
    	
    	private String getDay(int day)
    	{
    		switch(day)
    		{
    		case 1: {return "Monday";}
    		case 2: {return "Tuesday";}
    		case 3: {return "Wednesday"; }
    		case 4: {return "Thursday"; }
    		case 5: {return "Friday"; }
    		case 6: {return "Saturday"; }
    		case 7: {return "Sunday"; }
    		default: {return "NoDay";}
    		}
    	}
    	
    	public String getMonth(int month)
    	{
    		switch(month)
    		{
    		case 1: return "January";
    		case 2: return "February";
    		case 3: return "March";
    		case 4: return "April";
    		case 5: return "May";
    		case 6: return "June";
    		case 7: return "July";
    		case 8: return "August";
    		case 9: return "September";
    		case 10: return "October";
    		case 11: return "November";
    		case 12: return "December";
    		default: return "NoMonth";
    		}
    	}
    	
    	public String printDate()
    	{
    		return (day+" "+date+" "+"month"+" "+ year);
    	}
    }
    TimeTeller.java
    Java Code:
    public class TimeTeller {
    	public static void main(String[] args) 
            {
    		Date today = new Date();
    		today.printDate();
    	}
    
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Define "does not run". Does it compile? Are there exceptions? Do you expect it to print something out? Add some println statements to make sure it enters and runs the appropriate methods.

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi - in your main method, you are creating a new Data object. And then, you invoke the printDate() method on it. The printDate() method only returns a string, it does not print it. So in your main method, try:
    Java Code:
    System.out.println(today.printDate());
    Best,
    --user0--

  4. #4
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

Similar Threads

  1. Replies: 9
    Last Post: 11-19-2009, 04:31 PM
  2. Execute the commands in Jsp Program
    By swetha_2008 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-17-2008, 06:04 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. Execute a new program in java
    By mathias in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:42 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
  •