3

I have written some simple code where I am using a StringTokenizer to get input data one line at a time and then parseInt from it.

InputStreamReader in = new InputStreamReader(System.in);
BufferedReader r = new BufferedReader(in);

int T = Integer.parseInt(r.readLine());
int v;
int e;
int testCases;

for(testCases = 1; testCases <= T; testCases++)
{
    StringTokenizer st = new StringTokenizer(r.readLine());
    v = Integer.parseInt(st.nextToken());
    e = Integer.parseInt(st.nextToken());
    ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>(v+1);
}

now i want to read the next line using the same object st but when i try this

 st(r.readLine());

it throws an error

"The method is undefined for main type"

how do I read the next Line. I think i am doing something fundamentally wrong. or please suggest some alternative method. Here is the input:

5   // T
5 6 // v & e
1 2 // error reading this
3 4
5 6
1 4
1 3
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Ankit
  • 394
  • 1
  • 4
  • 16
  • Just out of curiosity: what do you expect `st.()` to do? – SJuan76 Apr 16 '15 at 11:51
  • 2
    From [the documentation for `StringTokenizer`](https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) _`StringTokenizer` is a legacy class that is retained for compatibility reasons although its use is discouraged in new code._ The answer to your question is "don't use it in the first place". – Boris the Spider Apr 16 '15 at 11:52
  • @SJuan76 My mistake. edited it to st(r.readLine()); – Ankit Apr 16 '15 at 11:52
  • @boris How should i do it then?could you suggest some other method – Ankit Apr 16 '15 at 11:53
  • 1
    From the [very same documentation](https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html), which I take it you haven't even bothered to look at, _it is recommended that anyone seeking this functionality use the `split` method of `String` or the `java.util.regex` package instead._ – Boris the Spider Apr 16 '15 at 11:54
  • works for me well ...... where is "st(r.readLine());" exactly ? .... "st" isn't a method, is a object – Jose Ricardo Bustos M. Apr 16 '15 at 11:57

1 Answers1

-1

Please use separate method which has your line as argument and returns integer token and get it on your place.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Santosh Shinde
  • 6,045
  • 7
  • 44
  • 68
  • Do not only provide a URL... quote the important part in your answer, for the content you are referring to can change or even disappear! – Willi Mentzel Apr 17 '15 at 08:36