在增变器中使用方法声明的变量?
using method-declared variable in mutators?
对从 class 中的方法中获取变量并在增变器中使用它的逻辑感到困惑。
编辑:这是我的代码转储
我的代码:
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
堆栈溢出让我打字越来越多,所以我要在这里说一些话,这样我就可以 post。
我不知道你得到的错误是什么,所以我将重写你的代码并希望它能解决你的问题。
package tests;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class LoadingBox extends JPanel {
private String inFiles;
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;
this.setFiles(fullpath);
}
}
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 {
private ArrayList<String> stringArray;
private ArrayList<Integer> numArray;
private LoadingBox loadFile;
private String files;
public FileScan(){
stringArray = new ArrayList<String>();
numArray = new ArrayList<Integer>();
loadFile = new LoadingBox();
loadFile.loadIt();
files = LoadFile.getFiles();
}
public void scan(String[] args) throws IOException {
Scanner input = null;
new FileScan();
try {
input = new Scanner(new File(files));
//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();
}
}
}
对从 class 中的方法中获取变量并在增变器中使用它的逻辑感到困惑。
编辑:这是我的代码转储
我的代码:
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
堆栈溢出让我打字越来越多,所以我要在这里说一些话,这样我就可以 post。
我不知道你得到的错误是什么,所以我将重写你的代码并希望它能解决你的问题。
package tests;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class LoadingBox extends JPanel {
private String inFiles;
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;
this.setFiles(fullpath);
}
}
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 {
private ArrayList<String> stringArray;
private ArrayList<Integer> numArray;
private LoadingBox loadFile;
private String files;
public FileScan(){
stringArray = new ArrayList<String>();
numArray = new ArrayList<Integer>();
loadFile = new LoadingBox();
loadFile.loadIt();
files = LoadFile.getFiles();
}
public void scan(String[] args) throws IOException {
Scanner input = null;
new FileScan();
try {
input = new Scanner(new File(files));
//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();
}
}
}