# Spliting intgers 07:34

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-10-2011, 12:47 AM
yosi199
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?
• 03-10-2011, 12:49 AM
Junky
Since 07:34 is a String I don't see what your problem is.
• 03-10-2011, 12:49 AM
sunde887
Why can't you get split to work? ":" looks like a string
• 03-10-2011, 12:56 AM
yosi199
ok you guys were right sorry, my bad lol...

How about spliting ints? how do I do it?
• 03-10-2011, 01:00 AM
Junky
"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
• 03-10-2011, 01:03 AM
yosi199
Quote:

Originally Posted by Junky
"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?
• 03-10-2011, 01:06 AM
Junky
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.
• 03-10-2011, 01:17 AM
yosi199
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 :)
• 03-10-2011, 01:27 AM
gcalvin
Quote:

Originally Posted by yosi199
Integer.parseInt?

Yes.
Quote:

Originally Posted by yosi199
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.
Code:

```        String timeString = "07:34";         String[] timeArray = timeString.split(":");         int hour = Integer.parseInt(timeArray[0]);         int minute = Integer.parseInt(timeArray[1]);```
-Gary-
• 03-10-2011, 01:28 AM
Junky
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.
• 03-10-2011, 01:29 AM
Junky
Or you can just read the code provided :(
• 03-10-2011, 01:30 AM
gcalvin
Quote:

Originally Posted by Junky
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-
• 03-10-2011, 01:30 AM
yosi199
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.
• 03-10-2011, 01:35 AM
yosi199
Quote:

Originally Posted by gcalvin
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.
• 03-10-2011, 01:38 AM
gcalvin
Quote:

Originally Posted by yosi199
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-
• 03-10-2011, 01:39 AM
yosi199
I'm using eclipse. My ultimate goal is to work on android projects but first I will need to get Java nailed down.
• 03-10-2011, 02:02 AM
Junky
"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.
• 03-10-2011, 02:18 AM
gcalvin
Quote:

Originally Posted by Junky
"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-
• 03-10-2011, 02:41 AM
sehudson
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.

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.
• 03-10-2011, 02:59 AM
yosi199
Quote:

Originally Posted by sehudson
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.

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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last