4 Replies - 19616 Views - Last Post: 16 February 2009 - 02:31 AM Rate Topic: -----

#1 MmeLionHead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-February 09

Converting String month to int month

Posted 16 February 2009 - 12:35 AM

I'm practicing on writing code to calculate how many milliseconds one's lived by far.

I got stucked on the part where I'm creating a method to convert(or compare) String month to int month.

And I wish to to use the equals method from the String class to compare to Strings for equality.


I don't know where to start.

Please help.

Thanks.

This is what I have so far (the entire thing)...not even sure if the rest if right.


import java.util.Scanner;
import java.util.Calendar;
import java.lang.String;


public class Birthday {
	
	private static String month;
	private static int monthInt;


	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		System.out.println("In which year were you born?");
		int year = scan.nextInt();
		
		System.out.println("In which month were you born?");
		String month = scan.next();	   
		
		System.out.println("On what day of that month were you born?");
		int date = scan.nextInt();
		


		int monthInt = monthStringToInt(month); // month as an int.

		// I put a little test here for the conversion b/w String month and int month...doesn't seem to work.
		System.out.println(monthInt);


		Calendar birthmonth = Calendar.getInstance();
								birthmonth.set(Calendar.MONTH, monthInt);

		Calendar birthyear = Calendar.getInstance();
		birthyear.set( Calendar.YEAR, year );

		
		Calendar birthdate = Calendar.getInstance();
		birthdate.set( Calendar.DATE, date );

		
	  
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal2.set(year, monthInt, date);
		cal1.set(Calendar.YEAR, Calendar.MONTH, Calendar.DATE);
		long milis1 = cal1.getTimeInMillis();
		long milis2 = cal2.getTimeInMillis();
		long diff = milis2 - milis1;
		System.out.println(diff);
 

	} 
	

	private static int monthStringToInt(String month) {

	int January = Integer.valueOf (Calendar.JANUARY);
	if (month.equals(January ))
	{
		 monthInt = January;
	}
	//Similar to other 11 months

	return monthInt;

	} 
}

This post has been edited by MmeLionHead: 16 February 2009 - 02:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Converting String month to int month

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Converting String month to int month

Posted 16 February 2009 - 12:37 AM

can you please post your code
Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Converting String month to int month

Posted 16 February 2009 - 12:41 AM

to convert from string to integer you can use:
 int number = Integer.valueOf(MyNumberInString)

or to convert number to string you can use:
String MyNumberInString = String.valueOf(number),


to compare a string to another you can use the equals method for instace:
if(S1.equals(S2)){
//your code for the condition goes here
}


We could help youmore if you show us your code and where you have reached so far, and don't forget to post code using code tags :code:
hope this helps, good luck and thanks ;)

This post has been edited by ayman_mastermind: 16 February 2009 - 12:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 MmeLionHead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-February 09

Re: Converting String month to int month

Posted 16 February 2009 - 02:21 AM

I've posted my code. Can someone help me please. Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Converting String month to int month

Posted 16 February 2009 - 02:31 AM

	if (month.equals("January"))
	{
		 monthInt = Calendar.JANUARY;
	}
	//Similar to other 11 months

	return monthInt;

	} 


try this out

This post has been edited by mostyfriedman: 16 February 2009 - 02:33 AM

Was This Post Helpful? 1

Page 1 of 1