I'm trying to get this loop to put each line from a file in an object, then store each object in an arrayList. For some reason the loop stores the first three lines of the input correctly, but when it gets to the last line every item in the array list becomes the values in te last line. I pasted the relevant part of my code below, any suggestions?
while (lineIteration != null) {
//store polynomial objects from Polynomial class in an array list.
ta.setText(ta.getText() + lineIteration + "\n"); //prints the input to
// the text area
Polynomial objLine = new Polynomial(lineIteration); //store a line in the
// objLine object
System.out.println(objLine);
polyArrayList.add(objLine); //add the object to the arrayList
lineIteration = inBR.readLine(); //read the next lineIteration.
}
The full code is below. /***** CLASS Main.java *****/
import java.util.Iterator;
public class Polynomial implements Iterable, Comparable{
private static String poly;
public Polynomial (String x){ // constructor that accepts the input and stores it in a polynomial.
poly = x;
return;
}
@Override
public String toString() {
return poly;
}
private static class Node<String>{
private String data;
private Node<String> next;
public Node (String data, Node<String> next){
this.data = data;
this.next = next;
}
}
@Override
public Iterator iterator() {
return null;
}
@Override
public int compareTo(Object o) {
return 0;
}
}
/****Polynomial.java****/
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.logging.Logger;
public class Main extends JFrame implements ActionListener {
JMenuBar mb;
JMenu file;
JMenuItem open;
JTextArea ta;
static ArrayList<Polynomial> polyArrayList = new ArrayList<>(); //ArrayList of objects Polynomial
Main(){
open = new JMenuItem("Open File");
open.addActionListener(this);
file = new JMenu("File");
file.add(open);
mb = new JMenuBar();
mb.setBounds(0,0,800,20);
mb.add(file);
ta = new JTextArea(800,800);
ta.setBounds(0,20,800,800);
add(mb);
add(ta);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == open) {
JFileChooser fc = new JFileChooser();
int i = fc.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File selectedFile = fc.getSelectedFile();
String filepath = selectedFile.getPath();
try {
BufferedReader inBR;
inBR = new BufferedReader(new FileReader(selectedFile));
String lineIteration = inBR.readLine();
while (lineIteration != null) {
//store polynomial objects from Polynomial class in an array list.
ta.setText(ta.getText() + lineIteration + "\n"); //prints the input to the text area
Polynomial objLine = new Polynomial(lineIteration); //store a line in the objLine object
System.out.println(objLine);
polyArrayList.add(objLine); //add the object to the arrayList
lineIteration = inBR.readLine(); //read the next lineIteration.
}
} catch (FileNotFoundException fileNotFoundException) {
System.out.println("File not Found");
} catch (IOException ioException) {
System.out.println("IO Exception");
}
System.out.println(polyArrayList.get(0));
System.out.println(polyArrayList.get(1));
System.out.println(polyArrayList.get(2));
System.out.println(polyArrayList.get(3));
}
}
}
public static void main(String[] args){
System.out.println("Start");
Main om = new Main();
om.setSize(500,500);
om.setLayout(null);
om.setVisible(true);
om.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
The poly
variable has the static
modifier, meaning it's shared between all instances of that class. When the variable is set to a new value in another instance of the Polynomial
object, that will reflect in all other Polynomial
objects as well.
Since the last line of your file is the most recent value set on the variable, it'll be the value set across all Polynomial
objects. Removing the static
on the variable should be sufficient.
You have to remove the static from poly
in Polynomial
:
public class Polynomial implements Iterable, Comparable {
private String poly;
public Polynomial(String poly) { // constructor that accepts the input and stores it in a polynomial.
this.poly = poly;
}
static
keyword means all instances will share exactly the same value across them. Even though you were not seeing that, in every iteration you were setting the poly
value in all instances to the same value. You can check a bit more about the static
keyword here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.