To read a string after an integer in Java, you can use the Scanner
class, which is a simple text scanner that can parse primitive types and strings using regular expressions.
Here is an example of how you can use the Scanner
class to read a string after an integer in Java:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); String str = scanner.next(); System.out.println("Num: " + num); System.out.println("Str: " + str); } }
In this example, the main
method creates a Scanner
object that reads from the standard input stream (the console). It then uses the nextInt
method to read an integer and the next
method to read a string.
The values are then printed to the console using the System.out.println
method.
This code will read an integer and a string from the console and print them to the console. You can modify the code to store the data in a data structure or perform other operations with it.
Keep in mind that this is just one way to read a string after an integer in Java. You can use different techniques and libraries to achieve the same result, such as using the BufferedReader
class or the InputStreamReader
class to read from a file or other input stream.