If it the input is in a file i would recommend using a BufferedReader or Files.lines()
, for a scanner example look at the other answer. Below is example how you can use a BufferedReader to read the input of file.
I would recommend using this regex to check if the input is an int or String
public static void main(String[] args) {
List<Integer> ints = new ArrayList<>();
List<String> strings = new ArrayList<>();
try (BufferedReader br = new BufferedReader(
new FileReader(new File("path/to/input/file"))
)) {
String line;
while((line = br.readLine()) != null) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.matches("(?<=\\s|^)\\d+(?=\\s|$)")) { // regex matching int
ints.add(Integer.parseInt(part));
} else {
strings.add(part);
}
}
}
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println("content of string = ");
strings.forEach(string -> System.out.printf("%s ", string));
System.out.println();
System.out.println("content of ints = ");
ints.forEach(string -> System.out.printf("%d ", string));
}
output
content of string =
Fizz Buzz FizzBuzz Nil Ba Bi Be Bu Oong Greeng Kattu Eswah
content of ints =
3 1 45 5 3 5 4 13 10 2 7 49 23 5 5 10