0

Sorry if this is an obvious question, but i am new to java and not sure how to do it. I tried looking it up online but couldn't really understand what they were talking about. I want to accept an user input for example a date "2014 02 22 21 14". Note that the spaces separate each variable to be stored.

I want to parse that and store the 2014 into a INT variable called year and then store the 02 into a INT variable called month and 22 stored into the day INT variable.

Could someone please show me an example of how to do this it would be great.

  • 2
    You should put a little more effort into it yourself. What didn't you understand about the stuff you looked up? – Davio Oct 15 '14 at 12:12
  • Could you please post what have you tried so far.. – dasrohith Oct 15 '14 at 12:14
  • Take a look at [this question](http://stackoverflow.com/q/26313497/2071828). – Boris the Spider Oct 15 '14 at 12:14
  • 1
    This is a very basic problem. Search google for Oracle's Java Trail, check out `System.in`, and Lists. – Attila Herbert Oct 15 '14 at 12:15
  • Why don't you parse the entire string and store it as a Calendar object then you could access year month day etc... from the calendar object. – brso05 Oct 15 '14 at 12:21
  • possible duplicate of [Converting a date string to a DateTime object using Joda Time library](http://stackoverflow.com/questions/6252678/converting-a-date-string-to-a-datetime-object-using-joda-time-library) – Basil Bourque Oct 15 '14 at 20:31
  • Search StackOverflow.com for "joda parse" and search for "joda getMonthOfYear". You will find hundreds of answers with many code examples. Please, always search before posting. – Basil Bourque Oct 15 '14 at 20:35

3 Answers3

2
public class HelloApp {
    public static void main(String[] args) {
        // Show dateformat to user, so he can enter the format
        System.out.println("The date format is: yyyy mm dd");
        Scanner sc = new Scanner(System.in);
        String dateString =  sc.nextLine();
        String[] tokens = dateString.split(" ");
        int year = Integer.parseInt(tokens[0]);
        int month = Integer.parseInt(tokens[1]);
        int day = Integer.parseInt(tokens[2]);

        System.out.println("Year :" +year);
        System.out.println("month :" +month);
        System.out.println("day :" +day);


    }
}
mirmdasif
  • 6,014
  • 2
  • 22
  • 28
1

Use split(regex):

The input

String input = "2014 02 22 21 14";

Split the input in the parts between spaces

String[] data = input.split(" ");

This array contains 2014,02,22,21 and 14

Get the first item

String yearString = data[0];

This is "2014"

Convert to an int

int year = Integer.parseInt(yearString);

This is the year as number

Have fun coding -Charlie

Charlie
  • 978
  • 1
  • 7
  • 27
0

To add to sadasidha's answer, you don't need to split the string. Scanner can act as a parser, so you can directly access the next variable, without worrying about possibility of having letter being parsed as int:

    Scanner sc = new Scanner(System.in);
    int year = sc.nextInt();
    //and so on

What this does is creates a scanner with inputted string, for example: "2014 02 22 21 14" and then looks for the next integer in that string. Next time you do sc.nextInt(), it will give you the next integer based on space as delimiter.

Read up more on Scanner here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

This is assuming that you know the format will be parsed in correct order. Naturally you would have to check for that if you need all possible exception handling.

lupus137
  • 383
  • 3
  • 10