Printing Out Month Name Given Month Number

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 7243 Views - Last Post: 18 April 2012 - 04:04 AM Rate Topic: -----

#1 citnadxela  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 41
  • Joined: 05-February 11

Printing Out Month Name Given Month Number

Posted 05 February 2011 - 03:18 PM

Hello. I am currently in need of help writing this program. I must create a program that prints out the formal date given a user inputting the numerical date. For example, user inputs 02 05 2011, the program should output February 5, 2011. I must use a string to hold all of the month names and a substring to access the month names. I am having the hardest trying to figure out how to access the month name given the number of month and any help will be appreciated. I cannot use Calender or date formatting. This is my code thus far:

import java.util.Scanner;

public class assignment4b {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		System.out.println("Please enter a date (MM DD YYYY):");
		int month = in.nextInt();
		int day = in.nextInt();
		int year = in.nextInt();
		String months = "January February March April May June July August September October November December ";
		String January = months.substring(0,7);
		String February = months.substring(8,16);
		String March = months.substring(17,22);
		String April = months.substring(23,28);
		String May = months.substring(29,32);
		String June = months.substring(33,37);
		String July = months.substring(38,42);
		String August = months.substring(43,49);
		String September = months.substring(50,59);
		String October = months.substring(60,67);
		String November = months.substring(68,76);
		String December = months.substring(77,85);
		System.out.println("Your date is: " +month+" "+day+" "+year++);
		
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Printing Out Month Name Given Month Number

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 03:21 PM

Why do you need to use a String? An array would be much easier:
String[] months = new String[]{"January","February","March",..."December"};
String name = months[monthNumber-1];



If this isn't a valid option, you'll have to use if statements:
if(monthNumber == 1)
   //january
else if(monthNumber == 2)
   //february
//etc.


Was This Post Helpful? 0
  • +
  • -

#3 citnadxela  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 41
  • Joined: 05-February 11

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 03:22 PM

Well the directions in the assignment states to use a very long String to hold all of the month names separated by spaces.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 03:23 PM

In that case, see my second suggestion.
Was This Post Helpful? 1
  • +
  • -

#5 citnadxela  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 41
  • Joined: 05-February 11

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 03:27 PM

is it possible to do it if without using if and else statements because that's where my trouble is. i know how to do it using an if statement, but this class i'm taking hasn't learned that yet and we must access the month name using substrings. thank you so much for your help though.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 03:31 PM

Given the month format inputs, you cannot access only the month without using arrays or if statements. These are so basic, chances are, your instructor won't mind. Consult your instructor for the specifics.
Was This Post Helpful? 0
  • +
  • -

#7 citnadxela  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 41
  • Joined: 05-February 11

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 03:58 PM

My instructor said I cannot and only allowed to use Strings and substrings. This seems impossible for me to do. I don't know how to approach it.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 04:01 PM

Your instructor is asking the impossible. I will happily +vote anyone who can prove me wrong that the exact month name can be procured and printed without using if statements or arrays.
Was This Post Helpful? 0
  • +
  • -

#9 citnadxela  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 41
  • Joined: 05-February 11

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 04:02 PM

as of right now, i have the code outputting the user's input:

Please enter a date (MM DD YYYY):
04 30 1991
Your date is: 4 30 1991


basically, i need a way to change the output of that "4" to "April" and/or whatever month number the user enters to the month name.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 04:03 PM

So use an if statement. If you cannot use if statements, you cannot accomplish this program. If your instructor is asking for you to not use if statements, your entire class will fail this assignment.

Edit: To clarify, I am not suggesting abandoning substring().
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 04:03 PM

View Postmacosxnerd101, on 05 February 2011 - 06:01 PM, said:

Your instructor is asking the impossible. I will happily +vote anyone who can prove me wrong that the exact month name can be procured and printed without using if statements or arrays.

I'll give you the +1 right away
Was This Post Helpful? 1
  • +
  • -

#12 citnadxela  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 41
  • Joined: 05-February 11

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 04:07 PM

Here are the official instructions in case you wanted to see it.

Create a program that will print out the formal date given a numerical date. Use a single very long string to hold all of the month names separated by spaces. Then use the substring function to access the month name that you need.

Examples:
Please enter a date (MM DD YYYY): 01 22 2000
Your date is: January 22, 2000
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 04:10 PM

So that doesn't mean you can't use an if statement. Conditionals (if statements, switch blocks) and loops (while, do-while, for, foreach) are basic to programs of any real complexity. Your instructor cannot prohibit their use and expect you all to complete your programs.
Was This Post Helpful? 1
  • +
  • -

#14 citnadxela  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 41
  • Joined: 05-February 11

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 04:24 PM

import java.util.Scanner;

public class assignment4b {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		System.out.println("Please enter a date (MM DD YYYY):");
		int month = in.nextInt();
		int day = in.nextInt();
		int year = in.nextInt();
		String months = "January February March April May June July August September October November December ";
		String January = months.substring(0,7);
		String February = months.substring(8,16);
		String March = months.substring(17,22);
		String April = months.substring(23,28);
		String May = months.substring(29,32);
		String June = months.substring(33,37);
		String July = months.substring(38,42);
		String August = months.substring(43,49);
		String September = months.substring(50,59);
		String October = months.substring(60,67);
		String November = months.substring(68,76);
		String December = months.substring(77,85);
		if(month == 1)
			month = January;
			else if(month == 2)
			mont = February;
			else if(month == 3)
			month = March;
			else if(month == 4)
			month = April;
			else if(month == 5)
			month = May;
			else if(month == 6)
			month = June;
			else if(month == 7)
			month = July;
			else if(month == 8)
			month = August;
			else if(month == 9)
			month = September;
			else if(month == 10)
			month = October;
			else if(month == 11)
			month = November;
			else
			month = December;
		System.out.println("Your date is: " +month+" "+day+" "+year++);
		
	}
}


This is my code now, but it's giving my if statements an error saying it cannot convert from String to int ??

Nevermind. Stupid me, I found my error. Thank you so much Marcos for your help.
Was This Post Helpful? -1
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Printing Out Month Name Given Month Number

Posted 05 February 2011 - 06:38 PM

This is RIDICULOUS code
Use the String class split() method using " " as separator to isolate each motnh name
Hardcoding the letters positions of the beginning and letter of each month is HORRIBLE
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2