本文目录一览:
如何在java中读入整数
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个整数");
int x = scan.nextInt();
System.out.println("输入的证书是:"+x);
}
Java怎么读取txt文件中的整数,并转化为数组
File file = new File("txt路径");
BufferedReader br = new BufferedReader(new FileReader(file));
ListInteger list = new ArrayListInteger();
String tmp;
while((tmp = br.readLine()) != null){
list.add(Integer.parseInt(tmp));
}
br.close();
java从文件中读取int整数
代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class App {
public static void main(String[] args) throws IOException {
try (FileReader fileReader = new FileReader(new File("OutFile.txt"));
BufferedReader reader = new BufferedReader(fileReader);) {
String line = "";
Pattern pattern = Pattern.compile("Value at: \\d+ = (\\d+)");
while ((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
int val = Integer.parseInt(matcher.group(1));
System.out.println(val);
}
}
}
}
}