Search code examples
javasettergettermutators

using method-declared variable in mutators?


Confused on the logic of getting a variable out of a method in a class and using it in a mutator.

Edit: here is my code dump

My Code:

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

Heres where it's going

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

Stack overflow is making me type more so I'm going to ramble out some words down here so that I can post.


Solution

  • I don't know what the error you're getting is, so I'm just going to rewrite your code and hope it solves your problem.

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