Skip Top Navigation Bar
Why Java?
Learner's Corner
Teacher's Corner
Java Playground
Home
Getting Setup
Learn
Practice
Apply
Other Resources
Write Code Segments that Require the String Method split
Your Turn
Write program code to complete the provided tasks.
Run the code in the Java Playground to see if your code generates the desired result.
Make necessary changes until you have satisfied the tasks.
String split Method Tutorial
Practice 1: Shopping List
Given the
String item
, write code to separate the elements in the shopping list into an array named
shoppingList
Try the code in the Java Playground to verify your prediction.
Practice 2: Date Parsing
Given the
String date
, that is in the format "year-month-day", write code to separate the date into an array and print the year.
Try the code in the Java Playground to verify your prediction.
Modify
String date
to be `"2024-06-17-2024-08-07". Use a loop to print all the months stored in the array.
Practice 3: Robot Moves
Given the
String robotMoves
, write code to separate into an array of moves. The first character of each move tells the direction R for right, which moves the robot to the right (add to current distance) and L for left, which moves the robot to the left (subtract from current distance). The number after the direction gives the amount the robot will move either to the left or the right.
Try the code in the Java Playground to verify your prediction.
Given that the robot current location starts at
0
, calculate the final location of the robot. Remember moves that start with
R
add the corresponding value to the current location and moves that start with
L
subtract the corresponding value to the current location.
Run your code to test.