Если вы хотите быстро сканировать входные данные, не запутавшись в методе nextLine () класса Scanner, используйте для этого специальный сканер ввода.
class ScanReader {
/**
* @author Nikunj Khokhar
*/
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) return -1;
}
return buf[index++];
}
public char scanChar(){
int c=scan();
while (isWhiteSpace(c))c=scan();
return (char)c;
}
public int scanInt() throws IOException {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public String scanString() throws IOException {
int c = scan();
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
public long scanLong() throws IOException {
long integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public void scanLong(long[] A) throws IOException {
for (int i = 0; i < A.length; i++) A[i] = scanLong();
}
public void scanInt(int[] A) throws IOException {
for (int i = 0; i < A.length; i++) A[i] = scanInt();
}
public double scanDouble() throws IOException {
int c = scan();
while (isWhiteSpace(c)) c = scan();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = scan();
}
double res = 0;
while (!isWhiteSpace(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, scanInt());
}
res *= 10;
res += c - '0';
c = scan();
}
if (c == '.') {
c = scan();
double m = 1;
while (!isWhiteSpace(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, scanInt());
}
m /= 10;
res += (c - '0') * m;
c = scan();
}
}
return res * sgn;
}
}
ScanReader sc = new ScanReader(System.in);
3. Импортировать необходимые классы :
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
4. Выбросить IOException из вашего основного метода для обработки исключения 5. Использовать предоставленные методы. 6. Наслаждайтесь
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
class Main{
public static void main(String... as) throws IOException{
ScanReader sc = new ScanReader(System.in);
int a=sc.scanInt();
System.out.println(a);
}
}
class ScanReader....
Это вернет вам необходимую статистику. Ваш вопрос не указал, какой формат вам нужен, поэтому здесь я использовал именованный список. (Это может быть легко возвращено как data.frame.)
Я взял это из исходного кода tm-пакета, файла Matrix.R, где определен метод печати для объектов TermDocumentMatrix.
getTDMstats <- function(x) {
# where x is a TermDocumentMatrix
list(sparsity = ifelse(!prod(dim(x)), 100, round((1 - length(x$v)/prod(dim(x))) * 100)) / 100,
maxtermlength = max(nchar(Terms(x), type = "chars"), 0),
weightingLong = attr(x, "weighting")[1],
weightingShort = attr(x, "weighting")[2],
nonsparse = length(x$v),
sparse = prod(dim(x)) - length(x$v))
}
data(crude)
tdm2 <- TermDocumentMatrix(crude)
tdm2
## <<TermDocumentMatrix (terms: 1266, documents: 20)>>
## Non-/sparse entries: 2255/23065
## Sparsity : 91%
## Maximal term length: 17
## Weighting : term frequency (tf)
getTDMstats(tdm2)
## $sparsity
## [1] 0.91
##
## $maxtermlength
## [1] 17
##
## $weightingLong
## [1] "term frequency"
##
## $weightingShort
## [1] "tf"
##
## $nonsparse
## [1] 2255
##
## $sparse
## [1] 23065