Results 1 to 4 of 4

Thread: String to ints?

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

    Default String to ints?

    Anyone can help?

    public Date(String s) {

    ???????
    }

    such that

    Date("1250/345/759")
    it will return
    (1250,345,759)

    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 online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    992
    Rep Power
    2

    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 online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    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.

    Regards,
    Jim
    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
    Posts
    184
    Rep Power
    0

    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;
      }
    Then,
    Java Code:
    dateToken("2/5/2014");
    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
  •