0

My RPG is doing just fine in the compiler. It inputs a file and reads it with Scanner, but when I export it into a ".jar" file it throws the FileNotFoundException.

I have tried putting the file in different locations. I have tried using different ways to call up the file. Nothing seems to be working.

package worldStuff;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class World {

    public static String[][] initWorld(String[][]array,String name) throws FileNotFoundException {
        int j = 0;
        String string;
        Scanner input = new Scanner(new File(name+".map"));
        while(j!=30) {
            string = input.nextLine();
            array[j] = string.split(",");
            j++;
        }
        input.close();
        return array;
    }

}

That is the method for inputting the file. I need a way for it to not error after compiling. I am using the Eclipse IDE with the export on this configuration:Eclipse Configuraiton

E_net4
  • 27,810
  • 13
  • 101
  • 139
WeaponGod243
  • 19
  • 10

1 Answers1

1

If you've got the option to use Java8, how about this:

public static void main( String[] args ) {
        try {
            String[][] output = initWorld( "E:\\Workspaces\\Production\\Test\\src\\test\\test" );
            for ( String[] o : output ) {
                if ( o == null || o.length == 0 ) { break; }
                System.out.println( "- " + o[0] );
            }
        } catch ( FileNotFoundException ex ) {
            ex.printStackTrace();
        }
    }

    public static String[][] initWorld( String name ) throws FileNotFoundException {

        String array[][] = new String[30][];
        try (Stream<String> stream = Files.lines(Paths.get(name))) {
            List<String> inputList = stream.collect(Collectors.toList());
            for ( int i = 0; i < 30 && i < inputList.size(); i++ ) {
                array[i] = inputList.get( i ).split( "," );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return array;
    }

the main method is jsut for testing purposes here (prints the first element of each initialized array).

Also, no need to pass String[][] as an argument.

Just replace the initWorld argument with whatever your path to the target file is (make sure to use \ if you're on windows, \ by itself is an escape character ).

Hope that helps.

Ivan Tomić
  • 171
  • 6
  • When I compile, I still get problems. Is this a bad file path? `spawnWorld = World.initWorld("Resources/world/spawn.map");` – WeaponGod243 Aug 29 '19 at 19:18
  • The path may be valid, but it's impossible for anyone without access to your pc to know if there's anything on it or not. Try using an absolute path you absolutely know exists, e.g. "C:\\whatever\\folder\\path\\spawn.map", or in case you're on linux e.g. "/home/yourhomefolder/dev/spawn.map", just make sure it's an absolute path that points to a file that 100% exists. – Ivan Tomić Aug 29 '19 at 21:47
  • But, I would be sharing it with others, so that wouldn't work on their computers. – WeaponGod243 Aug 29 '19 at 21:50
  • Dude... don't share it, just use it to test your code... see if it works with an absolute path, and if it does take it from there – Ivan Tomić Aug 29 '19 at 23:52