读取一个带有空行的整型文件,并保存到一个数组中 – JAVA

huangapple go评论77阅读模式
英文:

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(&#39;[&#39;);
boolean needsComma = false;
for (int i = 0; i &lt; numero; ++i) {
if (needsComma) {
sb.append(&quot;, &quot;);
}
sb.append(pilha[i]);
needsComma = true;
};
sb.append(&#39;]&#39;);
return sb.toString(); // Arrays.toString(pilha);
}
}
public static void main(String[] args) throws Exception {
Stack guardar = new Stack();
Path file = Paths.get(&quot;D:\\entradas\\tarefas1000.txt&quot;);
Files.lines(file) //This method needs a parameter to work on
.forEach(line -&gt; {
if (!line.trim().isEmpty()) {
guardar.add(Integer.parseInt(line));
}
});
System.out.println(guardar);
}

huangapple
  • 本文由 发表于 2020年10月7日 23:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64247440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定