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 2 of 2

Thread: Trying to modify class so when toString method returns in different format than given

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to modify class so when toString method returns in different format than given

    So I'm trying to get a return in the format "month number, number" ex. February 11, 2012. In the beginning I enter all in numbers. I'm stumped on where to begin. Should I create an string array ?
    public class DaysBetween
    {
      public static void main(String[] args)
      {
     
        Scanner conIn = new Scanner(System.in);
        int day, month, year;
        boolean cont = true;
        String input;
        do	// Added do while loop, so user gets asked if he wants to repeat operation.
        {
        	    System.out.println("Enter two 'modern' dates: month day year");
        		System.out.println("For example, January 12, 1954, would be: 1 12 1954");
        		System.out.println();
        		System.out.println("Modern dates occur after " + Date.MINYEAR + ".");
        		System.out.println();
     
        		System.out.println("Enter the first date:");
        		if(conIn.hasNextInt())
        		{
        				month = conIn.nextInt();
    	 				day = conIn.nextInt();
        				year = conIn.nextInt();
        				Date date1 = new Date(month, day, year);
     
        		}
        			else
        			{
        				conIn.next();
        				System.out.print("Illegal input was entered. ");
        				return;
        			}
     
     
        		System.out.println("Enter the second date:");
        			month = conIn.nextInt();
        			day = conIn.nextInt();
        			year = conIn.nextInt();
        		Date date2 = new Date(month, day, year);
     
     
     
        	if  ((date1.getYear() <= Date.MINYEAR) /////Cannot find symbol
    	    		||
    		 	(date2.getYear() <= Date.MINYEAR))
          		System.out.println("You entered a 'pre-modern' date.");
        	else
        		{
          			System.out.println("The number of days between");
          			System.out.print(date1); /////Cannot find symbol
          			System.out.print(" and ");
          			System.out.print(date2);
          			System.out.print(" is ");
          			System.out.println(Math.abs(date1.lilian() - date2.lilian()));///Cannot find symbol
          			System.out.print("and it has been ");
          			System.out.print(Math.abs(date2.mjd())); // Added to print days between now and Nov 17, 1858.
          			System.out.println(" days since November 17, 1858");
          			System.out.print("and it has been ");
          			System.out.print(Math.abs(date2.djd())); // Added to print days between now and Jan 1, 1900.
          			System.out.println(" days since January 1, 1900");
          			System.out.print("Do you want to continue? (Y/N): "); // Added to ask user if they want to continue.
     
        		}
        }
        while ("Y".equalsIgnoreCase(conIn.next().trim())); //Added so if user enters "yes" program will start over.
     }
    }

    Edit: I have figured out the problem with my toString method, but have encountered another problem. I am supposed to verify that the input are integers and if not, I'm supposed to tell user "wrong input" and close. I thought I can put a if-else statment, but when I build I get an error "Cannot find symbol" refering to date1 in the 2nd "if" statement. Any clue on what I'm doing wrong?
    Last edited by slayer; February 13th, 2012 at 02:47 PM.


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Trying to modify class so when toString method returns in different format than g

    Hey slayer,

    Do you mind reading This? It makes it lot easier for us to help you.

    What type of parameter is your toString method receiving? What is it's purpose?
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  2. [SOLVED] Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: July 9th, 2011, 11:14 AM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 06:00 AM
  4. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 05:44 AM