Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree2Likes

Thread: Array/list ints contains specific number

  1. #1
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Array/list ints contains specific number

    Hello, I'm very new to java.
    I've been trying to make a list of numbers/ints and later on check that list for a specific number.
    Been trying this for 2 days and still i haven't found anything that works.. found a lot of codes but none of them seemed to work..
    Please help!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Array/list ints contains specific number

    Post your code, we will not post ready made solutions.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    So I finnaly got to find the number in code(4 really..), now i still have figure out how to clean up this mess, cause i need to put in a keyEvent handler in there..

    Java Code:
    package ta.vdlinden.isogame;
    
    import javax.swing.JFrame;
    
    
    
    public class Game extends JFrame {
        
        
        
        public static void main(String[] args){
    
      new Game();
      
      int[] CurrentArea = new int[]{11,12,13,14,15,16,21,22,23,24,25,26,31,32,33,34,35,36,43,44,45,46,53,54,55,56};
      int[] ActionTile = new int[]{3,4,65};
      int lenghtAT = ActionTile.length;
      int lenghtCA = CurrentArea.length;
      int CurrentTile =34;
      int NextTile;
      boolean NextUp= false;
      boolean NextDown= false;
      boolean NextLeft= false;
      boolean NextRight= false;
      boolean NextMoveReady = false;
      
      
       while(NextMoveReady == false){
        //NextTile = CurrentTile+10;
        for(int i=0;i<lenghtCA;i++){
         if(CurrentArea[i]==CurrentTile+10){
          NextDown = true;
          System.out.println("down"+i);}
         if(CurrentArea[i]==CurrentTile-10){
          NextUp = true;
          System.out.println("up"+i);}
         if(CurrentArea[i]==CurrentTile-1){
          NextLeft = true;
          System.out.println("left"+i);}
         if(CurrentArea[i]==CurrentTile+1){
          NextRight = true;
          System.out.println("right"+i);}
         else{}}
        NextMoveReady = true;
       }
      
      
      }
        }

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,910
    Rep Power
    5

    Default Re: Array/list ints contains specific number

    Uhm, you do realize that you make it incredibly unclear what you actually want now?

    Have you solved your problem or not, and if so is there still something you need help with or are you now going to hammer it out yourself again?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Array/list ints contains specific number

    Why are you extending JFrame (which you shouldn't do under most situations)? And since you are doing everything in main, there is no reason to instantiate the class. If your game will eventually require graphics, I recommend you get the building blocks working first before you combine them to construct the building.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    I'm extending the JFrame cause I need one later on (please tell me if I'm wrong) for the actual screen. It's going to be a isometric rpg-like game. I got like a month for it and I'm not that good with java yet. Now my next step was to put in a inputhandler, but let me explain the workflow I'm aiming for. the game loads up and checks wich tile you are on, it finds all the tiles next to it and checks if you can actually go there, than I want an userinput to make the player move to the next tile if posible, and than it automaticly checks the tiles again so u can make your next move. But I think I'm gonna need a lot more practice with java, to be clear I dont want you guys to fix anything, but if u got some tips or info for a newbie, please do.. I am wondering about one thing, where should i put the tile checker and where to keep my var's? because i cant seem to put in a inputhandler without screwing up the rest of the code.. Thanks

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Array/list ints contains specific number

    The preferred approach is to extend JPanel, not JFrame. But create an instance of JFrame in your class and add your subclass to it. If you will be doing any painting, override paintComponent in your subclass.

    There are various ways to tackle it. One way (but not necessarily the best) would be to create a tile class where each class maintains references to all of its neighbor tiles. The class could also contain a boolean which indicates whether the class is busy or not. If you update a given tile to busy, then all the other neighbor tiles will see this since they contain references to that tile. Of course the tile class can hold additional state as needed.

    You also need to decide if you are going to use a real Component (via subclassing) for your tiles or create one by painting. Both have their own sets of problems to deal with. For someone new to Java, this is going to be challenging.

    Regards,
    Jim
    Last edited by jim829; 01-25-2014 at 08:18 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Array/list ints contains specific number

    Should you really be making a game if need help to do something like this? I would start by learning all of the basics of Java first before you dive into game development.

  9. #9
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    181
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    To answer the ORIGINAL question:

    If you use an ArrayList, you can use an

    ArrayList.contains() method

    If you really want to stick to arrays, you can cycle through the array

    Java Code:
    public static boolean containsNum( int[] array, int checknum ) {
    for(int i = 0; i < array.length; i++) {
    if(array[i] == checknum) return true;
    }
    return false;
    }
    Either one works. I'd say just use arraylist

  10. #10
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    Quote Originally Posted by pj6444 View Post
    Should you really be making a game if need help to do something like this? I would start by learning all of the basics of Java first before you dive into game development.
    Yeah it's kind of a bad habbit of mine, I like to jump in the deep..
    Alexgraal, Thank you for the reply. I had put in your second option because the first one.. I didn't get it to work.
    Jim, thanks for your reply, I'll be looking into it! Could you maybe explain real quick about the main difference between JPanel and JFrame?

    Might just change it from tile movement to collision detection even though I'd love to get this working first..

  11. #11
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    So I fixed everything up, got my input to work and such but it leaves me with one isseu, how do i tell the program that it must check the tiles again, because it won't not even on startup..

    Java Code:
    package ta.vdlinden.isogame;
    
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    
    
    
    public class Game extends JFrame {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    		int[] CurrentArea = new int[]{11,12,13,14,15,16,21,22,23,24,25,26,31,32,33,34,35,36,43,44,45,46,53,54,55,56};
    		int[] ActionTile = new int[]{3,4,65};
    		int lenghtAT = ActionTile.length;
    		int lenghtCA = CurrentArea.length;
    		int CurrentTile =34;
    		int NextTile;
    		boolean NextUp= false;
    		boolean NextDown= false;
    		boolean NextLeft= false;
    		boolean NextRight= false;
    		boolean NextMoveReady = false;
    	
    		public Game(){
    			addKeyListener(new AL());
    			setTitle("Screen");
    			setSize(200,200);
    			setResizable(false);
    			setVisible(true);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		}
    	
    	
    	
    		public void check(){
    			while(NextMoveReady == false){
    				
    				for(int i=0;i<lenghtCA;i++){
    					if(CurrentArea[i]==CurrentTile+10){
    						NextDown = true;
    						System.out.println("down"+i);}
    					if(CurrentArea[i]==CurrentTile-10){
    						NextUp = true;
    						System.out.println("up"+i);}
    					if(CurrentArea[i]==CurrentTile-1){
    						NextLeft = true;
    						System.out.println("left"+i);}
    					if(CurrentArea[i]==CurrentTile+1){
    						NextRight = true;
    						System.out.println("right"+i);}
    					else{}}
    				NextMoveReady = true;
    			}
    	}
    	
    	public class AL extends KeyAdapter{
    		public void keyPressed(KeyEvent e){
    			int keyCode = e.getKeyCode();
    			if(keyCode == e.VK_A){
    				if(NextLeft == true){CurrentTile = CurrentTile - 1;NextLeft=false;
    				System.out.println(CurrentTile);}
    				
    			}
    			if(keyCode == e.VK_S){
    				if(NextUp == true){CurrentTile = CurrentTile - 10;NextUp=false;
    				System.out.println(CurrentTile);}
    				
    			}
    			if(keyCode == e.VK_X){
    				if(NextRight == true){CurrentTile = CurrentTile + 1;NextRight=false;
    				System.out.println(CurrentTile);}
    				
    			}
    			if(keyCode == e.VK_Z){
    				if(NextDown == true){CurrentTile = CurrentTile + 10;NextDown=false;
    				System.out.println(CurrentTile);}
    				
    			}
    		}
    		public void keyReleased(KeyEvent e){
    			
    		}
    	}
    	
    	public static void main(String[] args){
    
    		new Game();
    	}
    }

  12. #12
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Array/list ints contains specific number

    You're going to need a game loop that calls your check method over and over again.

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,910
    Rep Power
    5

    Default Re: Array/list ints contains specific number

    Well its a Swing application so a traditional game loop is not a good idea.

    Looking at the code, I think it is enough to call check() each time a move is done.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    Quote Originally Posted by gimbal2 View Post
    Well its a Swing application so a traditional game loop is not a good idea.

    Looking at the code, I think it is enough to call check() each time a move is done.
    Thank you very much! I found that if i called the check() nothing really happend, so i went further into it, did some debugging and found that i should have set the NextMoveReady to false after every move.. I'm getting there! :) I can now freely walk one these tiles!
    Array/list ints contains specific number-map.png
    Now have to recreate it so that my area can be bigger than 10x10 tiles(8x8 really.. cause else it might bug out..)
    Last edited by Trisy123; 01-27-2014 at 04:05 PM.
    gimbal2 likes this.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Array/list ints contains specific number

    Quote Originally Posted by Trisy123 View Post
    Jim, thanks for your reply, I'll be looking into it! Could you maybe explain real quick about the main difference between JPanel and JFrame?
    JFrames are usually the top level container for all the other components. So you usually only need one but sometimes applications may create several different frames. JPanel is a component which may contain other components such as textfields, buttons, etc. and may also contain other JPanels. When you extend something you usually do so to add functionality or you need to override one or more methods. In the painting case, overriding paintComponent is the way to go for JPanel and all other components. I suggest you read about JPanels, JFrames, and painting in the tutorials listed in my signature. They provide a much more in depth discussion of the subject.

    Regards,
    Jim
    Trisy123 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,910
    Rep Power
    5

    Default Re: Array/list ints contains specific number

    This is the first time I spot that while(NextMoveReady == false) loop in the check() method. What is the purpose of that loop? My brain is not that awake today, but it looks to me like it would never loop a second time since the NextMoveReady boolean is set to true at the end of it.

    And PS: try to stick to the java naming conventions. variable names and class attribute names start with a lowercase letter. That is how you can easily differentiate them from the name of a class.

    Java Code:
    public class Person {
    
    }
    
    Person Person; // BRAIN MELTS
    Person person; // brain shrugs
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    Quote Originally Posted by jim829 View Post
    JFrames are usually the top level container for all the other components. So you usually only need one but sometimes applications may create several different frames. JPanel is a component which may contain other components such as textfields, buttons, etc. and may also contain other JPanels. When you extend something you usually do so to add functionality or you need to override one or more methods. In the painting case, overriding paintComponent is the way to go for JPanel and all other components. I suggest you read about JPanels, JFrames, and painting in the tutorials listed in my signature. They provide a much more in depth discussion of the subject.

    Regards,
    Jim
    Thank you very much! It's good to know all these things, as a beginner I find it very hard to find the right places to learn. I will take a look at your tutorials and leave some feedback!

  18. #18
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    Quote Originally Posted by gimbal2 View Post
    This is the first time I spot that while(NextMoveReady == false) loop in the check() method. What is the purpose of that loop?
    Ah that, that boolean is to prevent the player to move when the surrounding tiles have not been calculated yet. but you should read my last update of the code since it doesn't do anything in my previous version.. Also thank you about that tip for capital letters and such, I finally understand why people do it with some names and not with others..
    So here you go:
    Java Code:
    package ta.vdlinden.isogame;
    
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    
    
    
    public class Game extends JFrame {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    		int[] CurrentArea = new int[]{11,12,13,14,15,16,21,22,23,24,25,26,31,32,33,34,35,36,43,44,45,46,53,54,55,56};
    		int[] ActionTile = new int[]{3,4,65};
    		int lenghtAT = ActionTile.length;
    		int lenghtCA = CurrentArea.length;
    		int CurrentTile =34;
    		int NextTile;
    		boolean NextUp= false;
    		boolean NextDown= false;
    		boolean NextLeft= false;
    		boolean NextRight= false;
    		boolean NextMoveReady = false;
    		int i;
    	
    		public Game(){
    			check();
    			addKeyListener(new AL());
    			setTitle("Screen");
    			setSize(200,200);
    			setResizable(false);
    			setVisible(true);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		}
    	
    	
    	
    		public void check(){
    			
    			while(NextMoveReady == false){
    				
    				for(i=0;i<lenghtCA;i++){
    					if(CurrentArea[i]==CurrentTile+10){
    						NextDown = true;}
    					if(CurrentArea[i]==CurrentTile-10){
    						NextUp = true;}
    					if(CurrentArea[i]==CurrentTile-1){
    						NextLeft = true;}
    					if(CurrentArea[i]==CurrentTile+1){
    						NextRight = true;}
    					else{}
    					}
    				i=0;
    				NextMoveReady = true;
    				System.out.println("check");
    			}
    		}
    	
    	public class AL extends KeyAdapter{
    		public void keyPressed(KeyEvent e){
    			int keyCode = e.getKeyCode();
    			if(keyCode == e.VK_A){
    				if(NextLeft == true){CurrentTile = CurrentTile - 1;NextLeft=false;
    				System.out.println(CurrentTile);NextMoveReady = false;check();}
    				
    			}
    			if(keyCode == e.VK_S){
    				if(NextUp == true){CurrentTile = CurrentTile - 10;NextUp=false;
    				System.out.println(CurrentTile);NextMoveReady = false;check();}
    				
    			}
    			if(keyCode == e.VK_X){
    				if(NextRight == true){CurrentTile = CurrentTile + 1;NextRight=false;
    				System.out.println(CurrentTile);NextMoveReady = false;check();}
    				
    			}
    			if(keyCode == e.VK_Z){
    				if(NextDown == true){CurrentTile = CurrentTile + 10;NextDown=false;
    				System.out.println(CurrentTile);NextMoveReady = false;check();}
    				
    			}
    		}
    		public void keyReleased(KeyEvent e){
    			
    		}
    	}
    	
    	public static void main(String[] args){
    		new Game();
    	}
    }
    Last edited by Trisy123; 01-27-2014 at 04:21 PM.

  19. #19
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Array/list ints contains specific number

    That is true. I would agree with gimbal that you should just check them each move.

  20. #20
    Trisy123 is offline Member
    Join Date
    Jan 2014
    Posts
    10
    Rep Power
    0

    Default Re: Array/list ints contains specific number

    I have a dificult time seeing what you think is true..? But yes, the plan was to check it every move, wich it now does.

Page 1 of 2 12 LastLast

Similar Threads

  1. Adding random number to array list
    By Michael305rodri in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 09:50 AM
  2. getting specific information from the array list
    By elvis0288 in forum New To Java
    Replies: 7
    Last Post: 03-05-2012, 12:40 PM
  3. Comparing in an array with ints
    By queenz68 in forum New To Java
    Replies: 3
    Last Post: 01-02-2012, 06:27 PM
  4. Replies: 2
    Last Post: 09-14-2010, 01:24 AM
  5. reading in unsigned ints into a 2D array
    By newToIt in forum New To Java
    Replies: 9
    Last Post: 03-06-2009, 12:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •