我有一个程序从文件中读取内容并将其打印在屏幕上.但程序打印每隔一行,即跳过其他所有行.包InputOutput;
import java.io.*;
public class CharacterFileReaderAndFileWriter{
private BufferedReader br = null;
private PrintWriter pw = new PrintWriter(System.out, true);
/* Read from file and print to console */
public void readFromFile() throws IOException{
try{
br = new BufferedReader(new FileReader("E:\\Programming\\Class files\\practice\\src\\InputOutput\\test.txt"));
}
catch(FileNotFoundException ex){
ex.printStackTrace();
}
String s = null;
do{
s = br.readLine();
pw.println(s);
}
while((s = br.readLine())!=null);
br.close();
}
/* Main method */
public static void main(String[] args) throws IOException{
CharacterFileReaderAndFileWriter cfr = new CharacterFileReaderAndFileWriter();
cfr.readFromFile();
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下功课问题:
给定一系列分数,如果阵列中彼此相邻的分数为100,则返回true.数组长度至少为2.
这个问题是否意味着数组中的数字应该可以被100整除?所以如果它在数组中,1也会使程序返回true?
我试图为长变量赋值,但eclipse显示编译错误.任何人都可以解决这个问题吗?我已检查并确信该值在很长的范围内.
public static void main(String[] args) {
**long num = 600851475143;**
for(long i = num/2; i<1; i--) {
if(num%i == 0 && isPrime(i) == true) {
System.out.println(i);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想从文件中读取数字行.代码如下,但IDE显示NullPointerException运行时异常.不确定我做错了什么.
//reading the contents of the file into an array
public static void readAndStoreNumbers() {
//initialising the new object
arr = new int[15][];
try{
//create file reader
File f = new File("E:\\Eclipse Projects\\triangle.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
//read from file
String nums;
int index = 0;
while((nums = br.readLine()) != null) {
String[] numbers = nums.split(" ");
//store the numbers into 'arr' after converting into integers
for(int i=0; i<arr[index].length; i++) {
arr[index][i] = Integer.parseInt(numbers[i]);
}
index++;
}
} …Run Code Online (Sandbox Code Playgroud) 是否可以执行以下操作?
List<String> list = new ArrayList<String>();
Iterator<String> iterator = list.listIterator();
populateList(list);
Run Code Online (Sandbox Code Playgroud)
我在使用数据填充列表之前创建一个列表并获取其迭代器.我能使用迭代器获取列表的元素吗?获取迭代器和填充数组的顺序是否会有所不同?
任何帮助将不胜感激.