-4

Here is an example of such input.

A 3 
B 1 
A 2 etc.

As shown above, each input is separated by a line and appears an indeterminate amount of times. How do I only read the numbers next to the 'A' and convert it all into a string using Scanner?

Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
Angela
  • 11
  • 2
  • You mean [`Scanner#nextInt()`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()) method as in [this answer](http://stackoverflow.com/a/3059367/2180785)? – Frakcool Jan 16 '17 at 18:39

1 Answers1

-1

You can write something like:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main29 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            String string = scanner.next();

            int number = scanner.nextInt();

            System.out.println(number);
        }
    }
}

output:

3
1
2

As you can see I just write a loop which works until scanner can read token from STDIN. Inside of loop I read String tokens use next method and then read Integer tokens use nextInt method.

I think now you can add and required logic to the loop i.e. print numbers after A as you wish.

Maxim
  • 8,986
  • 4
  • 52
  • 98