对从类中的方法中获取变量并在 mutator 中使用它的逻辑感到困惑。
编辑:这是我的代码转储
我的代码:
package tests;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class LoadingBox extends JPanel {
String[] inFiles = new String[0];
public void loadIt(){
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT FILES", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getCurrentDirectory();
String path = file.getPath();
String filename = chooser.getSelectedFile().getName();
String fullpath = path + "/" + filename;
}
}
public String[] getFiles() {
return inFiles;
}
public void setFiles(String[] inFiles) {
this.inFiles = inFiles;
}
}
这是它的去向
package tests;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileScan {
ArrayList<String> stringArray = new ArrayList<String>();
ArrayList<Integer> numArray = new ArrayList<Integer>();
LoadingBox LoadFile = new LoadingBox();
String[] files = {LoadFile.getFiles()[0]};
//ITS GOING RIGHT HERE^^^^^^^^^^^^^^^
public void scan(String[] args) throws IOException {
Scanner input = null;
new FileScan();
try {
input = new Scanner(new File(files[0]));
//add strings and integers from file to different arrays
while (input.hasNext()) {
String token = input.nextLine();
try{
int o = Integer.parseInt(token);
numArray.add(o);
}
catch(NumberFormatException nfe){
stringArray.add(token);
}
}
}
finally {
if (input != null) {
input.close();
}
}
}
//Some more getters and setters down here
堆栈溢出使我打字更多,所以我将在这里粗略地说出一些单词,以便我可以发布。