← Back to Week 2 Hub

String Conversion

Converting between Strings, Numbers, and Dates in Java

Conversion Pipeline

Type a value in the input, then click a conversion method to see the result.

String value:
Choose a Method
Integer.parseInt()
Returns: int
Double.parseDouble()
Returns: double
Float.parseFloat()
Returns: float
"42"
method()
?
// Click a method above to see the conversion
Common Gotchas

Click each card to try the problematic input and see what happens.

Non-numeric text
"abc" → parseInt()
Letters cannot become numbers
Decimal with parseInt
"42.5" → parseInt()
parseInt cannot handle decimals
Empty string
"" → parseInt()
Nothing to parse
Extra spaces
" 42 " → parseInt()
Use .trim() first!
Remember: Scanner's nextLine() always returns a String. If you need a number, you must parse it!
int age = Integer.parseInt(scanner.nextLine());
Date Parsing with LocalDate

Java uses LocalDate.parse() with a DateTimeFormatter to convert strings into dates.

String Input
"07/15/2025"
Pattern
"MM/dd/yyyy"
LocalDate
2025-07-15
Pattern Breakdown
Formatter Code Reference
Symbol Meaning Example (July 5, 2025)
dDay (no padding)5
ddDay (zero-padded)05
MMonth (no padding)7
MMMonth (zero-padded)07
MMMMonth abbreviationJul
MMMMFull month nameJuly
yyTwo-digit year25
yyyyFour-digit year2025
Tip: ISO format (yyyy-MM-dd) is the default for LocalDate.parse(). For any other format, you must provide a DateTimeFormatter.
Converting Back to Strings

Click each card to see how it works.

Number to String (Option 1)
String.valueOf(42)
int String
Number to String (Option 2)
Integer.toString(42)
int String
Date to ISO String
date.toString()
LocalDate String
Date with Custom Format
date.format(formatter)
LocalDate String
Remember: Scanner's nextLine() always returns a String. If you need a number, you must parse it!
Tip: String concatenation with + also converts numbers to strings automatically:
String message = "Price: $" + 19.99;  // "Price: $19.99"

Click tabs to explore String-to-Number, String-to-Date, and reverse conversions.