Split, join and turn text into numbers
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)split() breaks one String into an array of parts, and String.join() puts parts back together. Integer.parseInt() turns text like "85" into the number 85.
split(): one String into an array
A sheet gives the class list as one line: Ravi,Anu,Meena. split(",") cuts the text at every comma and gives a String[] of the parts; the commas themselves are dropped.
Example 1 · Split a list of names
public class SplitNames { public static void main(String[] args) { String line = "Ravi,Anu,Meena"; String[] names = line.split(","); System.out.println("Count: " + names.length); for (String n : names) { System.out.println(n); } }}Count: 3 Ravi Anu Meena
What this code does
split(",")cuts the line at every comma and drops the commas.namesis a normalString[], sonames.lengthcounts the parts and a for-each loop prints them.
Extra spaces between words
split(" ") counts words well only when there is exactly one space between them. For typed text with extra spaces, use s.trim().split("\\s+"): \\s+ means "one or more spaces".
public class SplitSpaces { public static void main(String[] args) { String sentence = " Java is fun "; System.out.println(sentence.split(" ").length); System.out.println(sentence.trim().split("\\s+").length); }}8 3
split(" ") makes an empty part for each extra space, so it finds 8 parts. trim() first removes the outer spaces, then \\s+ treats each group of spaces as one separator: 3 words.
. and |, have a special meaning in split(). To split on a dot, write split("\\.").Join parts: String.join()
After cleaning the words of a sentence, you want them back as one String with _ between them. String.join(sep, parts) puts sep between the parts of an array, or of a list of Strings.
Example 2 · Count words and join them again
public class Words { public static void main(String[] args) { String sentence = " I love cricket "; String[] words = sentence.trim().split("\\s+"); System.out.println("Words: " + words.length); System.out.println(String.join("_", words)); }}Words: 3 I_love_cricket
What this code does
trim().split("\\s+")gives the three clean words, as in the last section.String.join("_", words)puts_between the words, not before the first or after the last.
join is called on the word String, not on a String variable: String.join(", ", "Maths", "Physics") gives Maths, Physics.
Text to number: parseInt()
Marks arrive as the text "78 85 62 90", and "85" + 5 gives 855, not 90. Integer.parseInt("85") gives the int 85; for decimals use Double.parseDouble("9.5").
Example 3 · Add marks given as text
public class MarksTotal { public static void main(String[] args) { String data = "78 85 62 90"; String[] parts = data.split(" "); int total = 0; for (String p : parts) { total += Integer.parseInt(p); } System.out.println("Total: " + total); System.out.println("Average: " + (double) total / parts.length); String totalText = String.valueOf(total); System.out.println("Digits in total: " + totalText.length()); }}Total: 315 Average: 78.75 Digits in total: 3
What this code does
- Each part is text like
"78";Integer.parseInt(p)turns it into a number that+=can add. (double) totalmakes the division keep its decimals (Type conversion and casting (opens in a new tab)).String.valueOf(total)goes the other way: the number 315 becomes the text"315", so String methods likelength()work on it.
The other way, number to text, is String.valueOf(x): String.valueOf(315) gives "315". For neat text such as "78.75" with exactly 2 decimals, use String.format() from Formatting output (opens in a new tab).
Text that is not a number, like "85a", stops the program with NumberFormatException. The Exceptions (opens in a new tab) module shows how to handle it.
Find the bug
Find the bug
Select the line that is wrong.
This should print 90.
Next step
Check your understanding
9 quick questions