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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 37

Thread: Trouble Updating an Array

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

    Default Trouble Updating an Array

    Hello, Gravity Games here, and I'm having trouble updating an array so my level's size isn't hardcoded.

    Here's the code:
    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 = "13";
    	private String stringlevelsizey = "13";
    	private String stringmusic = "1";
    	private String stringtileset = "1";
     
    	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();
     
    		levelsizex = Integer.parseInt(stringlevelsizex);
    		levelsizey = Integer.parseInt(stringlevelsizey);
    		music = Integer.parseInt(stringmusic);
    		tileset = Integer.parseInt(stringtileset);
    		private String Level[] = new String[(levelsizey)];
     
    		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 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();
     
    	}
     
     
    	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();
     
    }
     
    }

    For some reason, updating the array gives me an error saying that final is the only valid modifier. When I change it to final, it gives me an error saying Strings can't be converted to finals.
    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Love too help ya.
    But my lazy side has kicked in.
    Perhaps a smaller sample to work with?

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

    Default Re: Trouble Updating an Array

    Okay, since you asked, here's a modified version of the simple program I used to trace the problem to the array (originally it didn't have the array parts):
    package stringtest.main;
     
     
    import java.awt.*;
    import java.io.File;
    import java.util.Scanner;
     
    import javax.swing.JPanel;
     
     
    public class Main extends JPanel{
     
    	private Scanner lh;
    	private Scanner l;
    	private String s1 = "13";
    	private String s2 = "13";
    	private String s3 = "0";
    	private String s4 = "0";
    	private int i1;
    	private int i2;
    	private int i3;
    	private int i4;
    	private String a[] = new String[(i1)];
     
    public Main(){
     
    		openHeader();
    		readHeader();
    		closeHeader();
     
    		i1 = Integer.parseInt(s1);
    		i2=Integer.parseInt(s2);
    		i3=Integer.parseInt(s3);
    		i4=Integer.parseInt(s4);
     
    		openFile();
    		readFile();
    		closeFile();
     
     
    	}
     
    	public void paintComponent(Graphics g) {
        	super.paintComponent(g);
     
        	g.setColor(Color.BLACK);
        	g.drawString(s1,10,10);
        	g.drawString(s2,10,30);
        	g.drawString(s3,10,50);
        	g.drawString(s4,10,70);
     
        	if (i1==14 && i2==14 && i3==1 &&i4==1){
        		g.drawString(s1,10,90);
            	g.drawString(s2,10,110);
            	g.drawString(s3,10,130);
            	g.drawString(s4,10,150);
        	}
     
    	}
     
     
    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++){
    			s1=lh.next();
    		}
    		for(int i=1; i < 2; i++){
    			s2=lh.next();
    		}
    		for(int i=2; i < 3; i++){
    			s3=lh.next();
    		}
    		for(int i=3; i < 4; i++){
    			s4=lh.next();
    		}
    	}
     
    	}
     
    	public void closeHeader(){
     
    	lh.close();
     
    	}
    	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 < ((i1)); i++){
    			a[i]=l.next();
    		}
    	}
     
    	}
     
    public void closeFile(){
     
    	l.close();
     
    }
     
    }
    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Just a quick note as I am looking over your code:
    public Main() {
     
    		openHeader();
    		readHeader();
    ...and then:
    public void openHeader() {
     
    		try {
    			lh = new Scanner(new File("C://CWWLEVELS//world1stage1cfg.txt"));
    		} catch (Exception e) {
    			System.out.println("error loading configuration file");
    		}
     
    	}
    assume that fails and you catch the exception. What happens next when you call readHeader(); ...

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    I looked over your (ahem) smaller sample... and it does not contain the same error you mentioned in your original post. So I took a gaze at your first setup and I see you declared a private variable inside the scope of a public constructor as follows:
    private String Level[] = new String[(levelsizey)];
    I looked up top and you have also declared this variable within the class, outside of the constructor as follows:
    private String Level[] = new String[(levelsizey)];

    Did you mean to re-declare a variable with the same name, or did you intend to use the variable previously defined?

    and finally, this duplicated variable is exactly your class name.

    Take a peek at what you did, compared to what you meant to do. Hope that helps.

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

    Default Re: Trouble Updating an Array

    Well, I was trying to use the previously defined array and change its size. I named it the same as the class for simplicity reasons, but I guess I could rename it "stage" or something similar if you think that is whats causing the problem.
    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)

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Quote Originally Posted by Gravity Games View Post
    ...I guess I could rename it...
    Give it a shot and let me know how it works out.

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

    Default Re: Trouble Updating an Array

    No dice. It still gives me an error. Any other ideas?
    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)

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    But of course. Lets start with a fresh post of your current code

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

    Default Re: Trouble Updating an Array

    Quote Originally Posted by jps View Post
    But of course. Lets start with a fresh post of your current code
    Okay:

    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 = "13";
    	private String stringlevelsizey = "13";
    	private String stringmusic = "1";
    	private String stringtileset = "1";
     
    	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 Level1[] = 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();
     
    		levelsizex = Integer.parseInt(stringlevelsizex);
    		levelsizey = Integer.parseInt(stringlevelsizey);
    		music = Integer.parseInt(stringmusic);
    		tileset = Integer.parseInt(stringtileset);
    		final String Level1[] = new String[(levelsizey)];
     
    		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 = Level1[y].substring(x,x+1);
    		return index;
    	}
    	public int getlevelsizex(){
    		return levelsizex;
    	}
    	public int getlevelsizey(){
    		return levelsizey;
    	}
     
    	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();
     
    	}
     
     
    	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++){
    			Level1[i]=l.next();
    		}
    	}
     
    	}
     
    public void closeFile(){
     
    	l.close();
     
    }
     
    }
    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)

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    I threw your code in my IDE and the only complaint was your variable you say has the error is not used. Miss something somewhere?

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

    Default Re: Trouble Updating an Array

    Not that I'm aware, I remembered to make the File methods read from "Level1", my only guess is that they're reading it from when its first initialized.
    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)

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Quote Originally Posted by Gravity Games View Post
    ... they're reading it from when its first initialized.
    exactly my point.
    You have this variable initialized when declared.
    Then you initialize a DIFFERENT variable of the same name.
    Later when you try to use one of the two, the compiler takes the one it can see first at the time it looks, the inner-most scope. So instead of changing the value of the first variable you declared, you are creating a new one, and never using it.

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

    Default Re: Trouble Updating an Array

    Quote Originally Posted by jps View Post
    exactly my point.
    You have this variable initialized when declared.
    Then you initialize a DIFFERENT variable of the same name.
    Later when you try to use one of the two, the compiler takes the one it can see first at the time it looks, the inner-most scope. So instead of changing the value of the first variable you declared, you are creating a new one, and never using it.
    So how exactly would I go about changing it then? The normal method I use for integers clearly isn't working.
    What I normally do:
    public int integer = 1;

    blah blah blah code

    integer = 2;
    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)

  15. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    The difference in what you normally do and what you are currently doing is the type.
    int is a primitive type, and your variable Level1[] is a String array. When you have String Level1[] = new String[(some_size_variable_of_type_int)] you get memory locations to store Strings. Arrays are very picky about their size once created. The simple fix is to not use an array, but rather an ArrayList where size is adjusted on the fly, which is what I think you are wanting to do. Your desired results are not clear to me. My question to you is what is it you mean to do?

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

    Default Re: Trouble Updating an Array

    It sounds like an ArrayList is exactly what I need, because I need the String to be able to change size on the fly, as its for the level format of my game (it would get boring real quick if all the levels were the same size...). So how would I change it to an ArrayList, just replace all instances of "String" with "ArrayList"?
    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)

  17. #17
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Well the ArrayList class will be implemented much in the same way as you did the Array. It would be an ArrayList of type String. But you also need to keep in mind not to declare it twice. It is the same effect as saying int a = 3; and later saying int b = 4; and finally saying why doesn't a = 4? ...well because you set b to 4, not a. Just because the name is the same, doesn't mean it will use the one you wanted. The compiler starts looking for variables in the current scope the variable is seen in. If it does not find a value for that variable in that scope, it jumps up a bock. and up and up. If it never finds one, you get a compile error on syntax. However in your specific case, it is finding the variable defined at the start of your class Level. Hence no complaint by the compiler. The side effect of this type of error, is that it pops its head up later as a bug that is much harder to find.
    My advice on variables is to not use the same name. Even if you totally get what is going on and can do it correctly, and make it even work, your code is less readable to humans. Unfortunately for us humans, you in this case, debugging gets complicated.
    Hope that works out for you. Let me know

  18. The Following User Says Thank You to jps For This Useful Post:

    Gravity Games (July 22nd, 2012)

  19. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Quote Originally Posted by Gravity Games View Post
    ...just replace all instances of "String" with "ArrayList"?
    Just for clarity on this point, you are replacing your instances of Array with ArrayList, not replacing all instances of String...

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

    Default Re: Trouble Updating an Array

    Searching through my code, I found that "array" isn't actually used in the code anywhere. So how do I implement the ArrayList? (Sorry if this is a stupid question, I'm relatively new to Java)

    EDIT: NVM, I saw the post above, saying that ArrayList is a type of String. I'm gonna see if a quick Google search can help me out.

    EDIT2: I successfully converted the arrays to arraylists (I think) but now I'm having a problem getting the code that read the arrays to work:
    for(int y = 0; y < (l.getlevelsizey()); y++){
    				for(int x = 0; x < l.getlevelsizex(); x++){
    					if(l.getLevel(x,y).equals("g")){
    						g.drawImage(l.getGrassEK(),x*32,y*32,null);
    					}
    					if(l.getLevel(x,y).equals("t")){
    						g.drawImage(l.getGrassEKTop(),x*32,y*32,null);
    					}
    					if(l.getLevel(x,y).equals("w")){
    						g.drawImage(l.getWaterEK(),x*32,y*32,null);
    					}
    				}
    			}
    Last edited by Gravity Games; July 22nd, 2012 at 05:16 PM.
    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)

  21. #20
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Yes you never did say Array with the characters 'a', 'r', and 'y'. It was implied by [].
    Array class
    ArrayList class
    I am sure you can find them easy enough, but there they are. There are many tutorials on their use available.
    Since I have never gone to school for programming, that is how I learned.
    Good luck amigo

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

    Default Re: Trouble Updating an Array

    Alright, using the links you posted, I changed the Array completely to an Array List. The problem is that now, although the program runs, it no longer will show the tiles in the level.
    Here's the updated code:
    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 = "13";
    	private String stringlevelsizey = "13";
    	private String stringmusic = "1";
    	private String stringtileset = "1";
     
    	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 ArrayList<String> Level1 = new ArrayList<String>();
     
    	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();
     
    		levelsizex = Integer.parseInt(stringlevelsizex);
    		levelsizey = Integer.parseInt(stringlevelsizey);
    		music = Integer.parseInt(stringmusic);
    		tileset = Integer.parseInt(stringtileset);
    		final String Level1[] = new String[(levelsizey)];
     
    		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 = Level1.toString();
    		return index;
    	}
    	public int getlevelsizex(){
    		return levelsizex;
    	}
    	public int getlevelsizey(){
    		return levelsizey;
    	}
     
    	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();
     
    	}
     
     
    	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++){
    			Level1.add(l.next());
    		}
    	}
     
    	}
     
    public void closeFile(){
     
    	l.close();
     
    }
     
    }

    The code for the part of the class that reads the array is posted in my post edit above.
    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)

  23. #22
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    //
    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 = "13";
    	private String stringlevelsizey = "13";
    	private String stringmusic = "1";
    	private String stringtileset = "1";
    	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 ArrayList<String> Level1 = new ArrayList<String>();//NOTE 1 ****
     
    	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();
     
    		levelsizex = Integer.parseInt(stringlevelsizex);
    		levelsizey = Integer.parseInt(stringlevelsizey);
    		music = Integer.parseInt(stringmusic);
    		tileset = Integer.parseInt(stringtileset);
    		final String Level1[] = new String[(levelsizey)];//NOTE 2 ****
     
    		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 = Level1.toString();//NOTE 3 ****
    		return index;
    	}
     
    	public int getlevelsizex() {
    		return levelsizex;
    	}
     
    	public int getlevelsizey() {
    		return levelsizey;
    	}
     
    	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();
    	}
     
    	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++) {
    				Level1.add(l.next());
    			}
    		}
    	}
     
    	public void closeFile() {
    		l.close();
    	}
    }//

    //@the 3 comments added to your code:
    //NOTE 1 On this line you declare a variable named Level1, which is an ArrayList of Strings
    //NOTE 2 On this line you declare a new variable named Level1, wihch is an Array of Strings, and is never used!
    //NOTE 3 On this line you use the first variable you declared named Level1, but it is an EMPTY ArrayList

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

    Default Re: Trouble Updating an Array

    I just removed the second Level1, but why is the readFile() method not writing to the arraylist?
    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)

  25. #24
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Somehow my last post did not go through and all was lost. There are many other things in your code to go over, but time runs short for me today. So for now a quick list:
    • There is repeated code which could be turned into a method. Namely the open read and close methods.
    • The for loop inside the while loop
    • The read methods should be inside a try block incase something should happen to the file, if it was successfully opened.
    • The close methods should be a finally block
    • A suggestion on the reworking of your try block to include the open/read/close as an all in one method, read about try block with resources


    so the basic idea I propose is:
    all_in_one_method(String file...)
        try (resources) {
            open...
            read...
        }catch{
            ...
        }catch{
            ...
        }finally {
            close...
        }
    }

  26. #25
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Trouble Updating an Array

    lh = new Scanner(new File("C://CWWLEVELS//world1stage1cfg.txt"));
    Gravity Games,
    question: Is your OS Window or Linux (Unix, SunOS, Ubuntu or similar)?
    If Linux the literal "C://CWWLEVELS//world1stage1cfg.txt" can't have double slash //
    If Window the path is UN-window.

Page 1 of 2 12 LastLast

Similar Threads

  1. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  2. Trouble passing array in parameters
    By AngryCrow in forum Collections and Generics
    Replies: 2
    Last Post: September 12th, 2011, 09:49 AM
  3. .add(j, i); not updating array
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 08:30 AM
  4. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  5. Grade Array Trouble
    By kite98765 in forum Collections and Generics
    Replies: 3
    Last Post: January 7th, 2010, 08:14 PM