In Java, you can use the Scanner
class of the java.util
package to parse formatted input from a string.
Here's an example of how to use the Scanner
class to parse formatted input from a string:
import java.util.Scanner; public class Main { public static void main(String[] args) { String input = "123 456 789"; // Create a Scanner object to parse the input string Scanner scanner = new Scanner(input); // Parse the input string and store the values in variables int value1 = scanner.nextInt(); int value2 = scanner.nextInt(); int value3 = scanner.nextInt(); System.out.println(value1); // prints "123" System.out.println(value2); // prints "456" System.out.println(value3); // prints "789" } }Soecru:www.lautturi.com
This code creates a Scanner
object with the input string as the source. The nextInt()
method of the Scanner
object is then used to parse the input string and extract the integer values. The parsed values are stored in variables and can be used as needed.
You can use a similar approach to parse other types of formatted input from a string, by using the appropriate methods of the Scanner
class. For example, to parse a double
value from a string, you can use the nextDouble()
method.
You can find more information about the Scanner
class and the available methods for parsing formatted input in the Java documentation.