Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Having trouble loading variables from a file

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Having trouble loading variables from a file

    Hi, Gravity Games here, and I need to be able to load variables from a file so I can make different sized levels for my game. I'd prefer not to have to hard code anything, because I plan to have a level editor later on. So I NEED to be able to load the variables from a file.

    This is the code from the java class that loads the levels:
    package scorpioengine.main;
     
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
     
    import javax.swing.ImageIcon;
     
    public class Level {
     
    	private Scanner l;
    	private Scanner lh;
    	private String Header[] = new String[5];
     
    	private String stringlevelsizex = "14";
    	private String stringlevelsizey = "14";
    	private String stringmusic = "1";
    	private String stringtileset = "1";
     
     
    	public void openHeader(){
     
    		try{
    		lh = new Scanner(new File("C://CWWLEVELS//world1stage1cfg.txt"));
    		}catch(Exception e){
    			System.out.println("error loading configuration file");
    		}
     
    	}
     
    	public void readHeader(){
     
    	while(lh.hasNext()){
    		for(int i=0; i < 1; i++){
    			stringlevelsizex=lh.next();
    		}
    		for(int i=1; i < 2; i++){
    			stringlevelsizey=lh.next();
    		}
    		for(int i=2; i < 3; i++){
    			stringmusic=lh.next();
    		}
    		for(int i=3; i < 4; i++){
    			stringtileset=lh.next();
    		}
    	}
     
    	}
     
    	public void closeHeader(){
     
    	lh.close();
     
    	}
     
    	private int levelsizex = Integer.parseInt(stringlevelsizex);
    	private int levelsizey = Integer.parseInt(stringlevelsizey);
    	private int music = Integer.parseInt(stringmusic);
    	private int tileset = Integer.parseInt(stringtileset);
     
    	private String Level[] = new String[(levelsizey)];
     
    	private Image grassek,
    	              grassektop,
    	              waterek,
    	              path1,
    	              path2;
     
    	public Level(){
     
    		ImageIcon img = new ImageIcon("C://CWWGFX//GrassmainEKlarge.PNG");
    		grassek=img.getImage();
    		img = new ImageIcon("C://CWWGFX//ChompPortrait.PNG");
    		grassektop=img.getImage();
    		img = new ImageIcon("C://CWWGFX//EKWaterLarge.PNG");
    		waterek=img.getImage();
    		img = new ImageIcon("C://CWWGFX//Path1Large.PNG");
    		path1=img.getImage();
    		img = new ImageIcon("C://CWWGFX//Path2Large.PNG");
    		path2=img.getImage();
     
    		openHeader();
    		readHeader();
    		closeHeader();
     
    		openFile();
    		readFile();
    		closeFile();
     
    	}
     
    	public Image getGrassEK(){
    		return grassek;
    	}
     
    	public Image getGrassEKTop(){
    		return grassektop;
    	}
     
    	public Image getWaterEK(){
    		return waterek;
    	}
     
    	public Image getPath1(){
    		return path1;
    	}
    	public Image getPath2(){
    		return path2;
    	}
     
    	public String getLevel(int x, int y){
    		String index = Level[y].substring(x,x+1);
    		return index;
    	}
    	public int getlevelsizex(){
    		return levelsizex;
    	}
    	public int getlevelsizey(){
    		return levelsizey;
    	}
     
    	public void openFile(){
     
    		try{
    		l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt"));
    		}catch(Exception e){
    			System.out.println("error loading map");
    		}
     
    	}
     
    public void readFile(){
     
    	while(l.hasNext()){
    		for(int i=0; i < ((levelsizey)); i++){
    			Level[i]=l.next();
    		}
    	}
     
    	}
     
    public void closeFile(){
     
    	l.close();
     
    }
     
    }

    ...and here is the text in "world1stage1cfg":
    "14"
    "14"
    "1"
    "1"
    I know its not the file itself, because I've tried the numbers with and without the quotation marks. If you know whats wrong, please help! I've tried everything, but I'm pretty sure the problem is that the open, read, and close Header routines aren't being run properly. Also, it runs fine when the defaults for the variables are the same as in the file, but obviously that won't always be the case.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Having trouble loading variables from a file

    Your catch block does not post informative information. If you instead print a stacktrace, what errors do you see? Also, are you sure that you want to use files? You might consider using resources instead, especially if any of the data will be held inside of a jar.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Having trouble loading variables from a file

    I'm not sure what you mean by stacktrace or resources, unless the stacktrace is the errors the Eclipse SDK shows, or by resources you mean placing the files in my workspace's res folder. Cause I tried replacing "println" with "stacktrace", but Eclipse gave me an error, and I plan to move the images I call to the res folder. If it helps, here is what Eclipse gives as an error when I change the initial values of "stringlevelsizex" and "stringlevelsizey" to "13" instead of "14":

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at scorpioengine.main.Level.readFile(Level.java:137)
    at scorpioengine.main.Level.<init>(Level.java:88)
    at scorpioengine.main.MainJPanel.<init>(MainJPanel.ja va:40)
    at scorpioengine.main.MainWindow.main(MainWindow.java :19)
    Sorry if I'm being a n00b and completely missing something, as I'm about an advanced beginner at best...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Having trouble loading variables from a file

    I mean using printStackTrace method of Exception:

    }catch(Exception e){
       e.printStackTrace();
    }

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Having trouble loading variables from a file

    It still gives me an error, and eclipse still gives me the same errors I quoted earlier.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Having trouble loading variables from a file

    You should simplify your problem to solve it. Just use a simple text file and a very simple Java program that reads in the text file into say a collection of Strings. If and when that works, move to the next step, perhaps parsing the information into numbers if that is necessary, or marshalling it into objects. Continue adding functionality in a step-wise fashion until the problem is solved or until you reach a step that has an error that can't be solved. Then post the code here.

  7. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Having trouble loading variables from a file

    Well, I made a new and simple program and I think I found the problem. The problem has something to do with the array. The array isn't updated even when the variable for "levelsizey" is. What I'm having trouble with is updating the array after the header file has been read. When I try moving the array to underneath where the Header functions are called, it gives me an error saying that the only valid modifier is "final", but when I change it to final it gives me an error saying something along the lines of "can not convert from %missing% to string" and tells me to change the final back to a String. Any suggestions?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

Similar Threads

  1. Trouble loading elements into a queue
    By mael331 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 17th, 2012, 04:04 PM
  2. saving / loading a file
    By Herah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 07:04 AM
  3. loading things from a text file into an array list
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2011, 03:32 PM
  4. [SOLVED] Trouble accessing variables from other classes
    By Elementality in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 5th, 2011, 11:50 AM
  5. Loading a file in a different folder
    By tiemykim in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:53 AM