-3

This is the input of the program.

3

1 45 5 3 5 Fizz Buzz FizzBuzz Nil

4 13 10 2 7 Ba Bi Be Bu

49 23 5 5 10 Oong Greeng Kattu Eswah

I want to get all these lines as input using Scanner and separate them into Integers and Strings. It is not compulsory to use the scanner. Some other method is also accepted.

Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29
Salitha10
  • 9
  • 4
  • 1
    What have you tried so far? – Stephan Hogenboom May 17 '19 at 08:13
  • 1
    So what is the expected output? – Mureinik May 17 '19 at 08:14
  • 4
    Welcome to SO. I advise you read the [How to ask](https://stackoverflow.com/help/how-to-ask) article as it provides very useful information for newcomers on how to write questions. Quality questions help us provide you quality answers - Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your issue. – Esteban Garcia May 17 '19 at 08:20

2 Answers2

1
Scanner scan = new Scanner("3\n" +
        "\n" +
        "1 45 5 3 5 Fizz Buzz FizzBuzz Nil\n" +
        "\n" +
        "4 13 10 2 7 Ba Bi Be Bu\n" +
        "\n" +
        "49 23 5 5 10 Oong Greeng Kattu Eswah");

ArrayList<String> strings = new ArrayList<>();
ArrayList<Integer> ints = new ArrayList<>();
while(scan.hasNext()){
    String word=scan.next();
    try {
        ints.add(Integer.parseInt(word));
    } catch(NumberFormatException e){
        strings.add(word);
    }
}

scan.close();

System.out.println(ints);
System.out.println(strings);

If you want Scanner scan input from console with System.in then you need some trigger word which will end loop, for example if("exit".equals(word)) break;.

Hexronimo
  • 122
  • 1
  • 10
0

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 
Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29