Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 416,725 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,907 people online right now.Registration is fast and FREE... Join Now!



Converting String month to int month Rate Topic: -----

#1 MmeLionHead  Icon User is offline

  • New D.I.C Head
  • Pip

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


Dream Kudos: 0

Share |

Converting String month to int month

Posted 15 February 2009 - 11:35 PM

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 - 01:24 AM

Was This Post Helpful? 0
  • +
  • -


#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • Icon

Reputation: 487
  • Posts: 3,857
  • Joined: 24-October 08


Dream Kudos: 625

Expert In: Learning

Re: Converting String month to int month

Posted 15 February 2009 - 11:37 PM

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

#3 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • Icon

Reputation: 110
  • View blog
  • Posts: 1,852
  • Joined: 12-December 08


Dream Kudos: 575

Re: Converting String month to int month

Posted 15 February 2009 - 11:41 PM

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: 15 February 2009 - 11:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 MmeLionHead  Icon User is offline

  • New D.I.C Head
  • Pip

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


Dream Kudos: 0

Re: Converting String month to int month

Posted 16 February 2009 - 01: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
  • Icon

Reputation: 487
  • Posts: 3,857
  • Joined: 24-October 08


Dream Kudos: 625

Expert In: Learning

Re: Converting String month to int month

Posted 16 February 2009 - 01: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 - 01:33 AM

Was This Post Helpful? 1



Fast Reply

  

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users