0

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.

Tack
  • 121
  • 1
  • 12
  • What exactly are you trying to do here? You want to have a variable (that is declared inside a method) be accessible to another method within the class? The only way to do this is to either a) declare that variable as a class member, or b) pass that variable as a parameter to the other method – Hen Feb 25 '16 at 03:55
  • I want to take a variable (declared in a metod) and put it in an array outside the method. – Tack Feb 25 '16 at 03:59
  • Okay, so the String[] inFiles is a class variable? And String fullpath is the variable in the method? And you want to initialize fullpath in a method, and put it as the first element of inFiles? – Hen Feb 25 '16 at 04:01
  • You'll need to figure out how many elements you want to declare inFiles to be and do inFiles[1] = fullpath; or make inFiles an ArrayList and call .add(fullpath) – Hen Feb 25 '16 at 04:05
  • yes! Thats exactly it – Tack Feb 25 '16 at 04:05
  • I put fullpath as a parameter for the getter, made String[] inFiles = new String[1]; and then put "inFiles[1] = fullpath;" into the getter above "return inFiles", but my IDE says it is still not using the originally declared fullpath... – Tack Feb 25 '16 at 04:10
  • I really want to help you but I don't understand what you're trying to do. Could you edit your post and dump the code you're using? – Hen Feb 25 '16 at 04:13
  • Thanks for sticking with me, I dumped my code – Tack Feb 25 '16 at 04:17
  • And can you show me the main method (or wherever) you're calling this code from ? – Hen Feb 25 '16 at 04:19
  • Its being called into a scanner class. The idea is to take this file path that I get through the one class and put it into a variable to be used in a scanner class where I actually load the desired file. – Tack Feb 25 '16 at 04:27

1 Answers1

1

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

    }
}
Hen
  • 633
  • 1
  • 9
  • 21
  • 1
    I think this fixed the problem I was having with accessing the method declared variable. Thank you! – Tack Feb 25 '16 at 04:53
  • Your problem earlier was that 1. You weren't calling setFiles anywhere, and even if you did 2. You declared the inFiles array to be of size 0 (In Java, this indicates that it is "Empty") and you wouldn't have been able to add anything to it. – Hen Feb 25 '16 at 04:55
  • Not to be a downer, but I'm now getting a null pointer exception when I try and scan in "file" here: try { scanner = new Scanner(new File(files)); Which would mean the file isn't there when I invoke the scan method... – Tack Feb 25 '16 at 05:10
  • Right, sorry about that. Don't use files[0]. Just use files. I editted my answer. – Hen Feb 25 '16 at 05:11