Created
May 31, 2012 22:26
-
-
Save TyOverby/2846763 to your computer and use it in GitHub Desktop.
Splits a string by commas and converts them into integers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SplitInput { | |
public static void main(String... args){ | |
// This is your input string. It can be read from the console or | |
// via other methods of io, but I just have it the code for the hell of it | |
String input = "20,40,30,54,17"; | |
// Now we split the string into an array of strings with the "split" method. | |
// It creates an array of strings that were seperated by the comma. | |
String[] afterSplit = input.split(","); | |
// Now we will want to go through and convert our array of strings into | |
// an array of integers, so we will initialize a new array of ints. | |
int[] converted = new int[afterSplit.length]; | |
// Loop through and convert | |
for(int i=0;i<afterSplit.length;i++){ | |
// Using "Integer.parse()", you can convert the string into an int | |
converted[i] = Integer.parseInt(afterSplit[i]); | |
} | |
// Print all of our newly created integers. | |
for(int i:converted){ | |
System.out.println(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment