Formatted input

As known to all that in C++, cin can skip blanks or tabs and get the data in the type exactly as the variable's difinition. Well, in Java, is there a class can provide that.

For example, when I need to get the data below from std. instream:

1 2

2 3

4 9

10 3

12 4

all the data should be got as int type. How can I make it!

My english is very bad, please dot not care, thank you all!

[437 byte] By [wx-ray-1987a] at [2007-11-26 21:17:05]
# 1

Well, maybe my description is not detailed enough. What I want to know is how can I get data in formated way. For example, in C++, I can use the code below to read the data above into an array:

int data[5][2];

for (int i = 0; i < 5; i++)

cin>>data[i][0]>>data[i][1];

then, if I want do it(get the data) in Java, what should I do?

Thank you!

wx-ray-1987a at 2007-7-10 2:55:48 > top of Java-index,Java Essentials,New To Java...
# 2
No one would answer my question?Is it that, what I want to do is impossible in Java. Then, could you tell me what will do when dealing with the data above?
wx-ray-1987a at 2007-7-10 2:55:48 > top of Java-index,Java Essentials,New To Java...
# 3

Well, it seems that no one would like to answer my question.

Then, please tell me, if I want to do as I post in my topic, is it necessary to design a class which can do formated input?

In addtion, if I don not need a class which is as strong as cin, I just want to get data in formated way, such as "scanf()" in C "read(), readln()" in Pascal, is it possible in Java.

Thank you all. Please give me an answer.

Message was edited by:

wx-ray-1987

Message was edited by:

wx-ray-1987

wx-ray-1987a at 2007-7-10 2:55:48 > top of Java-index,Java Essentials,New To Java...
# 4
Read System.in using e.g. class java.util.Scanner. See the API for details.#
duckbilla at 2007-7-10 2:55:48 > top of Java-index,Java Essentials,New To Java...
# 5

Well, if I even do not need a really formated input, I just want to get data in easily way, which is to say do not convert the input string from String or byte[] to the exactly type I need. For example:

the input is:

1 2

//I define two int variable

int a, b;

how could I get 1 for a and 2 for b if I do not want to do string converting in my code.

wx-ray-1987a at 2007-7-10 2:55:48 > top of Java-index,Java Essentials,New To Java...
# 6

> how could I get 1 for a and 2 for b if I do not want

> to do string converting in my code.

As already suggested: by using the java.util.Scanner class.

java.util.Scanner keyboard = new java.util.Scanner(System.in);

System.out.print("Enter a: ");

int a = keyboard.nextInt();

System.out.print("Enter b: ");

int b = keyboard.nextInt();

System.out.println("a="+a+", b="+b);

prometheuzza at 2007-7-10 2:55:48 > top of Java-index,Java Essentials,New To Java...
# 7
> Read System.in using e.g. class java.util.Scanner.> See the API for details.> > #Thank you very much!Since I am new to java(my oringal language is C\C++), forgive me to ask that stupid question.
wx-ray-1987a at 2007-7-10 2:55:49 > top of Java-index,Java Essentials,New To Java...
# 8
> Thank you very much!> Since I am new to java(my oringal language is C\C++),> forgive me to ask that stupid question.No need to apologise!
prometheuzza at 2007-7-10 2:55:49 > top of Java-index,Java Essentials,New To Java...