在mutators中使用方法声明的变量?

时间:2016-02-25 03:42:57

标签: java setter getter mutators

混淆了从类中的方法中获取变量并在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

堆栈溢出让我输入更多,所以我要在这里饶恕一些单词以便我可以发帖。

1 个答案:

答案 0 :(得分:1)

我不知道你得到的错误是什么,所以我只是要重写你的代码并希望它能解决你的问题。

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();
            }

    }
}