英文:
Read an file of int with blank lines and save to a array - JAVA
问题
我尝试读取一个txt文件并将其保存到一个数组中,但在数组的输出中只得到null。我错过了什么?
txt文件的一部分内容,是的,在这些数字之间有空行
306741
581016
783580
529978
772824
54939
797499
235178
675900
365768
561760
986962
242452
621124
555822
80045
914383
634731
18956
创建数组的代码
public class Stack {
protected final int MAX=1000000;
protected Integer[] pilha;
Stack(){
pilha = new Integer[MAX];
}
void add(Integer newElement){
int i;
for(i=0; pilha[i]!=null; i++);
pilha[i] = newElement;
}
主程序:
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("D:\\entradas\\tarefas1000.txt");
Scanner sc = new Scanner(file);
Stack guardar = new Stack();
while (sc.hasNext()){
guardar.add(sc.nextInt());
}
sc.close();
System.out.println(Arrays.toString(guardar.pilha));
}
}
英文:
Im trying to read a txt file and save into a array but im only getting null in the output of my array. What am i missing?
A part of the txt file, and yes it is with these blank lines between the numbers
306741
581016
783580
529978
772824
54939
797499
235178
675900
365768
561760
986962
242452
621124
555822
80045
914383
634731
18956
The code that creates the array
public class Stack {
protected final int MAX=1000000;
protected Integer[]pilha;
Stack(){
pilha = new Integer[MAX];
}
void add(Integer newElement){
int i;
for(i=0;pilha[i]!=null;i++);
pilha[i]= newElement;
}
And the main:
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("D:\\entradas\\tarefas1000.txt");
Scanner sc = new Scanner(file);
Stack guardar=new Stack();
while (sc.hasNext()){
guardar.add(sc.nextInt());
}
sc.close();
System.out.println(Arrays.toString(guardar.pilha));
}
}
答案1
得分: 1
public class Stack {
protected final int MAX = 1_000_000;
protected int[] pilha;
private int numero;
Stack(){
pilha = new int[MAX];
}
void add(int newElement){
pilha[numero] = newElement;
++numero;
}
public int size() {
return numero;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
boolean needsComma = false;
for (int i = 0; i < numero; ++i) {
if (needsComma) {
sb.append(", ");
}
sb.append(pilha[i]);
needsComma = true;
};
sb.append(']');
return sb.toString(); // Arrays.toString(pilha);
}
}
public static void main(String[] args) throws Exception {
Stack guardar = new Stack();
Path file = Paths.get("D:\\entradas\\tarefas1000.txt");
Files.lines(file) //This method needs a parameter to work on
.forEach(line -> {
if (!line.trim().isEmpty()) {
guardar.add(Integer.parseInt(line));
}
});
System.out.println(guardar);
}
英文:
public class Stack {
protected final int MAX = 1_000_000;
protected int[] pilha;
private int numero;
Stack(){
pilha = new int[MAX];
}
void add(int newElement){
pilha[numero] = newElement;
++numero;
}
public int size() {
return numero;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
boolean needsComma = false;
for (int i = 0; i < numero; ++i) {
if (needsComma) {
sb.append(", ");
}
sb.append(pilha[i]);
needsComma = true;
};
sb.append(']');
return sb.toString(); // Arrays.toString(pilha);
}
}
public static void main(String[] args) throws Exception {
Stack guardar = new Stack();
Path file = Paths.get("D:\\entradas\\tarefas1000.txt");
Files.lines(file) //This method needs a parameter to work on
.forEach(line -> {
if (!line.trim().isEmpty()) {
guardar.add(Integer.parseInt(line));
}
});
System.out.println(guardar);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论