试图弄清楚如何读取文件中的数字并将它们存储在不同的变量中。我理解hasNext如何使用字符串,但不太确定如何读取同一行上的数字。运行它打印出第一个数字,但不打印第二个数字。
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class project1{
public static void main(String args[])throws IOException,FileNotFoundException{
Scanner kb = new Scanner(System.in);
File file = new File("candidates.txt");
Scanner inputFile = new Scanner(file);
String line = inputFile.nextLine();
Display the line.
System.out.println("The first line in the file is: \n" + line);
while(inputFile.hasNext()){
int creds = inputFile.nextInt();
System.out.println("credits : "+ creds);
}
// Close the file.
inputFile.close();
}
}
答案 0 :(得分:0)
每隔一行:
String numbers = inputFile.nextLine().split(" ");
int i = Integer.valueOf(numbers[0]);
double d = Double.valueOf(numbers[1]);