10 Replies - 804 Views - Last Post: 21 September 2011 - 09:02 AM Rate Topic: -----

#1 monkeyjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-August 11

Date Formatting Program

Posted 20 September 2011 - 01:10 PM

I have asked a lot of questions as of late and I really want to learn this language.

Before I start I am not asking for anyone to JUST give me the answer, but I would like help on where to go and what it means when I am told to add blank here.


My assignment this week deals with creating a date program. I have to have a user input and it must follow the correct formatting or else the program needs to inform the user that it is incorrect. Format is 02/11 (month/day) It will then convert the numbers into "February 11"

Here is what I have so far. I am stuck and could use a little push in the right direction.

import java.util.Scanner;
public class DateDriver
{
	public static void main (String [] args)
	{

		Scanner scan = new Scanner(System.in);
		Date date = new Date();
		System.out.println("Enter a date in the form mm/dd ('q' to quit): ");
		String quit = scan.next();
		if (quit.equals("q") || quit.equals("Q"))
		{
			//

		}
		else
		{
			System.out.println("");
		}
	}
}




public class Date
{
	int month;
	int day;

	public Date(int month, int day)
	{
		month = month;
		day = day;
	}

	public int getMonth()
	{
		return month;
	}
	public int getDay()
	{
		return day;
	}
}




Thanks for all the help in advanace. I would like to stress that I want to do the work I just do not understand exactly what I am doing.

This post has been edited by macosxnerd101: 20 September 2011 - 01:12 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Date Formatting Program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,791
  • Joined: 27-December 08

Re: Date Formatting Program

Posted 20 September 2011 - 01:14 PM

Hint- take a look at using an array for the month names. If the month is February, then its indicator in the input is 2, so it's position in the array is 1 (remember arrays start at 0).
Was This Post Helpful? 0
  • +
  • -

#3 monkeyjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-August 11

Re: Date Formatting Program

Posted 20 September 2011 - 01:18 PM

Great suggestion...I should place the array in data.java not the driver class. SO I need to build a constructor method so the driver will be able to grab the memory of say ArrayList[1] = February
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,791
  • Joined: 27-December 08

Re: Date Formatting Program

Posted 20 September 2011 - 01:19 PM

An array is not the same thing as an ArrayList. I would place the array in the Date class, yes. This is one of the few instances in which I would use static. It makes sense that this array is associated with the class, not the Objects.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7895
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Date Formatting Program

Posted 20 September 2011 - 01:33 PM

It might be worth putting a dummy first element to make the array contents align with the incoming content.
String[] months = {"", "January", "February"...}


would mean
System.out.println(months[1]);

returns "January" instead of February.

The down side of this is you have to remember that you did it this way if you iterate the array.

As for static, yes, unless you intend to have many calendars with different month names it makes sense to store that data once as a static array.
Was This Post Helpful? 0
  • +
  • -

#6 monkeyjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-August 11

Re: Date Formatting Program

Posted 20 September 2011 - 01:58 PM

Something like this?


public class Date
{
	int month;
	int day;
	public static String[] months = {null, "January", "February", "March", "April", "May", "June", "July", "August",
									 "September", "October", "November", "December"};


	public Date(int month, int day)
	{
		month = month;
		day = day;
	}

	public static String[] getMonths()
	{
		return months;
	}



}





When I go into my driver and type
System.out.println("The date is " + months[1]);
As a test I get a cannot find symbol error
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,791
  • Joined: 27-December 08

Re: Date Formatting Program

Posted 20 September 2011 - 02:01 PM

You have to access the static member by using the syntax Date.months[1], since it is in the Date class. I would make your array final as well. Other than that, you seem to be on the right track.
Was This Post Helpful? 0
  • +
  • -

#8 monkeyjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-August 11

Re: Date Formatting Program

Posted 20 September 2011 - 02:32 PM

My assignment now calls for a 1-parameter Date Constructor. Im assuming I already created that correct? But Now I have to make that constructor perform and complete error checking on a variable called dateStr.


What would be my best course of action? Would it be a bunch of If and Else ifs statements?

This post has been edited by monkeyjam: 20 September 2011 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7895
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Date Formatting Program

Posted 20 September 2011 - 04:00 PM

Quote

My assignment now calls for a 1-parameter Date Constructor. Im assuming I already created that correct?


public Date(int month, int day)


That looks like two arguments to me. Could be wrong, but I think I'm pretty good with numbers up to four or five.

Quote

What would be my best course of action? Would it be a bunch of If and Else ifs statements?



First thing to do is not to write any code. After you don't write any code, continue to not write any code while you figure out what sort of validation you want to do. This might require that you think about what format you expect dateSTr to have. Once you've figured out what you need to check for, you can then think about how to check it. Might involve some if statement, but that's to be determined after you know what you need to check for.
Was This Post Helpful? 0
  • +
  • -

#10 monkeyjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-August 11

Re: Date Formatting Program

Posted 21 September 2011 - 08:15 AM

Ok I decided to play the array inside the 1 parameter constructor. But in my driver I can not seem to use it in conjunction with the scanner class.

Also I was curious how I had a "/" for the format so that the scanner needs to user to input "/"

import java.util.Scanner;
public class FiermonteRobertDateDriver
{
	public static void main (String [] args)
	{

		Scanner scan = new Scanner(System.in);
		Date date;
		System.out.println("Enter a date in the form mm/dd ('q' to quit): ");
		scan date = scan.next();
		if (date.equals("q") || date.equals("Q"))
		{
			//

		}
		else
		{
			System.out.println("The date is " + date);
		}
	}
}



public class Date
{
	int month;
	int day;
	public static String[] months = {null, "January", "February", "March", "April", "May", "June", "July", "August",
									 "September", "October", "November", "December"};


	public Date(String[] months, int day)
	{
		months = months;
		day = day;
	}

	public static String[] getMonths()
	{
		return months;
	}



}



Would it be easier for me to so have a different scanner, and then place and If statment.

import java.util.Scanner;
public class FiermonteRobertDateDriver
{
	public static void main (String [] args)
	{

		Scanner scan = new Scanner(System.in);
		Date date;
		System.out.println("Enter a date in the form mm/dd ('q' to quit): ");
		String quit = scan.next();
		if (quit.equals("q") || quit.equals("Q"))
		{
			//

		}
		else if (quit.equals("1/02"))
			{
				System.out.println("The date is " + Date.months[1] + " 02");
			}
		}
	}



Was This Post Helpful? 0
  • +
  • -

#11 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Date Formatting Program

Posted 21 September 2011 - 09:02 AM

Also

public class Date
{
	int month;
	int day;

	public Date(int month, int day)
	{
		month = month;
		day = day;
	}


is simply assigning to the parameter month it's own value. Same thing for day. It should be
public class Date
{
	int month;
	int day;

	public Date(int month, int day)
	{
		this.month = month;
		this.day = day;
	}


So say: "assign to my instance variable month the value of the variable month received as parameter"
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1