Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default Spliting intgers 07:34

    Hello, I'm trying to get an hour input from the user. The user will type an hour in a format of 07:34 and then I will need to split and get the numbers from each side of the":".

    I tried to work with split() method but it accepts only strings. how can I do this?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Since 07:34 is a String I don't see what your problem is.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why can't you get split to work? ":" looks like a string

  4. #4
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    ok you guys were right sorry, my bad lol...

    How about spliting ints? how do I do it?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    "splitting ints" makes no sense. Especially without clarification. How do you want to split them?

    1234 --> 1 2 3 4
    1234 --> 1 234
    1234 --> 12 34
    1234 --> 123 4

  6. #6
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    "splitting ints" makes no sense. Especially without clarification. How do you want to split them?

    1234 --> 1 2 3 4
    1234 --> 1 234
    1234 --> 12 34
    1234 --> 123 4
    I need someway to brake an 07:34 input to a two different ints:

    x = 7
    y = 34

    I have no clue how to do it. Thought I should use split and then cast the string back to ints so I can run calculation on these values. Any better way?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You are on the right track but you cannot cast Strings to ints. How do you convert a String to an int? Surely you have already learnt this. Hint: you need to call a method.

  8. #8
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    You wont believe how early I am at the learning level :)

    Integer.parseInt?


    ok now another issue...
    I have this to split the string:

    public class splitHours {

    public static String split(String x){

    String testString = x;
    testString.split(":");

    How do I take the two, new, splited outputs and put them into some intgers say b and c?

    PS I have read the Java API for explantion and searched for google but I'm having trouble understanding the descriptions in the API and google brought me here :)

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by yosi199 View Post
    Integer.parseInt?
    Yes.
    Quote Originally Posted by yosi199 View Post
    ok now another issue...
    I have this to split the string:

    public class splitHours {

    public static String split(String x){

    String testString = x;
    testString.split(":");
    This code doesn't make any sense at all. First of all, you're just using the split() method -- you don't need to declare it yourself. (And even if you did, it wouldn't be static.) You're missing the most important part. The split() method returns a String[] -- an array of String objects. You need to declare an array variable to catch what it returns. Then you can call Integer.parseInt() on each element of the array.
    Java Code:
            String timeString = "07:34";
            String[] timeArray = timeString.split(":");
            int hour = Integer.parseInt(timeArray[0]);
            int minute = Integer.parseInt(timeArray[1]);
    -Gary-

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    The spilt method returns an array of Strings. You need to assign that returned value to a variable that you can use. Have you learnt about arrays. If not then now is the time.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Or you can just read the code provided :(

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by Junky View Post
    Or you can just read the code provided :(
    Didn't feel like a homework dump, so I went ahead with an illustration. If this is indeed a homework assignment, then shame on me.

    -Gary-

  13. #13
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    I've learnt about arrays and now will be a good time to practice it some more :)

    Thank you for your help guys I think I got it now, slowly the info unfold in my head.

  14. #14
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Didn't feel like a homework dump, so I went ahead with an illustration. If this is indeed a homework assignment, then shame on me.

    -Gary-
    NOT AT ALL! This is not a homework dump, I'm studying Java on my own for my personal geeky fun and because of that I have no one to consult with. Also I have this program in mind that I want to create and I've decided to learn Java on the go while I'm already working on this project. It makes it alot more fun when you get to work on something you love to program.

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by yosi199 View Post
    NOT AT ALL! This is not a homework dump, I'm studying Java on my own for my personal geeky fun and because of that I have no one to consult with. Also I have this program in mind that I want to create and I've decided to learn Java on the go while I'm already working on this project. It makes it alot more fun when you get to work on something you love to program.
    That's what I thought. Are you using an IDE, or text editor and command-line tools?

    -Gary-

  16. #16
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    I'm using eclipse. My ultimate goal is to work on android projects but first I will need to get Java nailed down.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    "Homework dump" is beside the point. If they want to learn then they will do so by figuring things out for themselves. Just spoonfeeding the code doesn't help them learn regardless if it is for homework or not.

  18. #18
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by Junky View Post
    "Homework dump" is beside the point. If they want to learn then they will do so by figuring things out for themselves. Just spoonfeeding the code doesn't help them learn regardless if it is for homework or not.
    I would agree with that most of the time, and you've seen me put that philosophy into practice many times. I made a judgment call here that an illustration would be more helpful in this particular case. I stand by it. I seriously doubt I've hampered anybody's learning today.

    -Gary-

  19. #19
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default

    If your "07:34" is a string (lets call it myString),

    in Java there is a method called split, that takes in a string and creates a string array whose elements are everything between your delimiter (in your case ':').

    So you could do something like
    String myString = "07:34";
    String[] myStringArray= myString.split(":");


    At this point, your myStringArray will have 2 elements in it, and look like this:

    myStringArray[0]="07"
    myStringArray[1]="34"

    Remember, myStringArray is a string array, so your 07 and 34 values are still strings. If you need to manipulate them as integers, you will have to convert the string array to an integer array. If you have a large array, the easiest way would be to walk through a for loop and convert each element in your string array to an integer.

    //Declare your new Integer array
    int[] myNewIntArray = new int[myStringArray.length];

    //Walk through the values in your String array and convert each element to a string/add it to your integer array

    for (int i = 0; i < myStringArray.length; i++) {

    myNewIntArray[i] = Integer.parseInt(myStringArray[i]);
    }

    At this point you have an integer array with your new integer values

    myNewIntArray[0]=7
    myNewIntArray[1]=34

    Now, you are free to do any integer operations on either of the 2 array elements.

  20. #20
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sehudson View Post
    If your "07:34" is a string (lets call it myString),

    in Java there is a method called split, that takes in a string and creates a string array whose elements are everything between your delimiter (in your case ':').

    So you could do something like
    String myString = "07:34";
    String[] myStringArray= myString.split(":");


    At this point, your myStringArray will have 2 elements in it, and look like this:

    myStringArray[0]="07"
    myStringArray[1]="34"

    Remember, myStringArray is a string array, so your 07 and 34 values are still strings. If you need to manipulate them as integers, you will have to convert the string array to an integer array. If you have a large array, the easiest way would be to walk through a for loop and convert each element in your string array to an integer.

    //Declare your new Integer array
    int[] myNewIntArray = new int[myStringArray.length];

    //Walk through the values in your String array and convert each element to a string/add it to your integer array

    for (int i = 0; i < myStringArray.length; i++) {

    myNewIntArray[i] = Integer.parseInt(myStringArray[i]);
    }

    At this point you have an integer array with your new integer values

    myNewIntArray[0]=7
    myNewIntArray[1]=34

    Now, you are free to do any integer operations on either of the 2 array elements.

    This has got to be the best informative answer I got. Not because he "gave me the code" but because he explained it to me in a very simple and clear way. That was what I'm missing, a bit of a different approch to explain this than the one I got from my book and the Java API.

    He's comments about the code is what will make me understand it better.

    Not everyone who come here with questions look for the easy way out. I know it damn good that I wont gain anything from copy-pasting code. I'm here to study, I just study different than you I guess.

Page 1 of 2 12 LastLast

Similar Threads

  1. problem in spliting a text
    By vipinvijay in forum New To Java
    Replies: 3
    Last Post: 01-10-2011, 11:09 PM
  2. Spliting a string (45 Characters)
    By BobAmin in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-20-2010, 10:45 AM
  3. spliting a string and checking each token's format
    By Implode in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 09:41 PM
  4. Spliting the Panel to three panels
    By suraw in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 07:05 PM
  5. Spliting desktop
    By nagarte in forum AWT / Swing
    Replies: 5
    Last Post: 05-05-2008, 01:56 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
  •