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!
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!
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
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.
> 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);