Results 1 to 4 of 4

Thread: String to ints?

  1. #1
    hunkeelin is offline Member
    Join Date
    Feb 2014
    Rep Power

    Default String to ints?

    Anyone can help?

    public Date(String s) {


    such that

    it will return

    in short Date("a/b/c")
    return (a,b,c)
    Last edited by hunkeelin; 02-05-2014 at 06:14 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: String to ints?

    I am not sure how the given Strings will work out as a Date, but if you are looking at replacing / with , then the String class has methods for it. Hint: the name of the method resembles the word "replace".
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: String to ints?

    Well, first off, you should brush up on Java syntax. Date won't return anything because it is apparently a constructor for a class Date.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Rep Power

    Default Re: String to ints?

    Yes, this isn't an actual int to string problem.

    To answer your actual question:

    Assuming that you were given a string that has a date, such as "2/5/2014," the way to break it up is called tokenizing.

    Here is the syntax:
    Java Code:
    String[] tokens = stringname.split("splitby");
    where "stringname" should be replaced by the name of your string, and "splitby" should be replaced by where you want to "cut."

    It is a handy command if, you example, you have a sentence and you want to break it into words.
    It'll make "You are a lost dog" into a string array = {"You","are","a","lost","dog"}, removing the spaces.
    Then there is Integer.parseInt(str), which will convert the string to an int.

    Here is how you should apply it.

    Java Code:
      public static int[] dateToken(String inputDate) {
        String[] tokenDate = inputDate.split("/");
        int[] dateNum = new int[tokenDate.length];
        for(int i = 0; i < dateNum.length; i++) {
          dateNum[i] =  Integer.parseInt(tokenDate[i]);
        return dateNum;
    Java Code:
    will return an int array with three numbers - {2,5,2013}.
    Last edited by AlexGraal; 02-05-2014 at 04:06 PM.

Similar Threads

  1. Identifying Ints and Non Ints in a string
    By Mnelson in forum New To Java
    Replies: 6
    Last Post: 07-07-2012, 12:08 AM
  2. How do I split a string of ints at every space
    By josho493 in forum New To Java
    Replies: 6
    Last Post: 04-18-2012, 11:45 PM
  3. Sum of odd ints 0-n
    By falkon114 in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 08:36 AM
  4. Help with ints
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 03:53 PM
  5. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 06:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts