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 12 of 12

Thread: Using a String[] in a Void or INT???

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Using a String[] in a Void or INT???

    I'm new to the forums and I am not too sure this is the correct place to post this thread, but I guess if it's wrong someone will correct me...

    Anyways, I am trying to make a custom texture system for a block in Minecraft, I am not too advanced with Java and am not sure how to make this work the way I want it to.

     
        /** The list of the types of step blocks. */
        public static final String[] blockStepTypes = new String[] {"stone", "sand", "wood", "cobble", "brick", "smoothStoneBrick", "netherBrick", "quartz"};
     
        private Icon missing;
        private Icon icon1;
     
        /**
         * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
         */
        public Icon getIcon(int par1, int par2)
        {
            int var3 = par2 & 7;
     
     
            {
            	if (var3 == 0) return par1 == 0 ? icon1 : missing;
            }
     
     
            return missing;
        }
     
        /**
         * When this method is called, your block should register all the icons it needs with the given IconRegister. This
         * is the only chance you get to register icons.
         */
        public void registerIcons(IconRegister par1IconRegister)
        {    	
        	String[] var4 = blockStepTypes;
     
        	this.missing = par1IconRegister.registerIcon("");
     
        	if (var4 = "stone")
        	{
        		this.icon1 = par1IconRegister.registerIcon("stone_slab_top");
        	}
     
        	if (var4 = "sand")
        	{
        		this.icon1 = par1IconRegister.registerIcon("sand_slab_top");
        	}
        }

    Alright, so basically I figured I could just tell the code to see if the block is made out of Stone, then to set the texture to Stone, or if it's made out of Sand, then set it to Sand.

    What I usually get is Eclipse telling me to "insert '!= null' check", "insert '!= null' check", and then just error out saying "Opperator != is undefined for the argument type(s) boolean null"

    Is there any way I could make this work for me? Maybe some other way to make it do what I am trying to do?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using a String[] in a Void or INT???

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    I assume you're talking about this statement and others like it:

    if (var4 = "stone")

    '=' is an assignment operator. The value to the right is assigned to or stored in the variable on the left.

    In the if statement, if ( condition ), condition must resolve to a boolean, either true or false. Since the condition var4 = "stone" does not resolve to true or false, the statement

    if (var4 = "stone")

    is incorrect.

    '==' is an equality operator, but it is not used to determine if the value or contents of one object (a String object, for example) is equal to another object's value or contents. Instead, the equals() method is used which is (or can be) customized to be specific for each Java type. Thus,

    if (var4 = "stone")

    is correctly written:

    if ( var4.equals( "stone" ) )

    as long as var4 is a String object.

    That's more about Java than you probably wanted to know. If you decide to continue your use of Java to enhance your Minecraft experience, please pick up a book and study Java basics a bit.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Sidonius (April 24th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using a String[] in a Void or INT???

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    I assume you're talking about this statement and others like it:

    if (var4 = "stone")

    '=' is an assignment operator. The value to the right is assigned to or stored in the variable on the left.

    In the if statement, if ( condition ), condition must resolve to a boolean, either true or false. Since the condition var4 = "stone" does not resolve to true or false, the statement

    if (var4 = "stone")

    is incorrect.

    '==' is an equality operator, but it is not used to determine if the value or contents of one object (a String object, for example) is equal to another object's value or contents. Instead, the equals() method is used which is (or can be) customized to be specific for each Java type. Thus,

    if (var4 = "stone")

    is correctly written:

    if ( var4.equals( "stone" ) )

    as long as var4 is a String object.

    That's more about Java than you probably wanted to know. If you decide to continue your use of Java to enhance your Minecraft experience, please pick up a book and study Java basics a bit.


    Thank you, your tip really helped, and I am interested in learning Java, I just don't know enough resources. Any books/E-books or sites you'd suggest?

    I actually ended up using it in a different way after some testing.

    What I have so far I actually got stuck on because I don't yet know how to do what I'm trying. I've seen it done in other codes but I'm not sure how to implement it.

    Basically here's what I have,

    /** The list of the types of step blocks. */
        public static final String[] stoneType = new String[] {"stone", "sand", "wood", "cobble", "brick", "smoothStoneBrick", "netherBrick", "quartz"};
     
    private Icon missing;
        private Icon icon1;
        private Icon icon4;
     
        /**This checks stoneType for the material and registers a number to each type for other codes to use.*/
        public int textureType()
        {
        	int var1 = 0;
     
        	if (stoneType.equals("stone")) return var1 = 0;
        	if (stoneType.equals("sand")) return var1 = 1;
        	if (stoneType.equals("wood")) return var1 = 2;
        	if (stoneType.equals("cobble")) return var1 = 3;
        	if (stoneType.equals("brick")) return var1 = 4;
        	if (stoneType.equals("smoothStoneBrick")) return var1 = 5;
        	if (stoneType.equals("netherBrick")) return var1 = 6;
        	if (stoneType.equals("quartz")) return var1 = 7;
     
        	else return var1;
        }
         /**
         * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
         */
        public Icon getIcon(int par1, int par2)
        {
            int var3 = par2 & 7;
            int var4 = this.textureType();
     
            if (this.isDoubleSlab)
            {
                if (var4 == 0) return icon1;
                if (var4 == 1) return icon4;
                if (var4 == 2) return Block.planks.getBlockTextureFromSide(par1);
                if (var4 == 3) return Block.cobblestone.getBlockTextureFromSide(par1);
                if (var4 == 4) return Block.brick.getBlockTextureFromSide(par1);
                if (var4 == 5) return Block.stoneBrick.getBlockTextureFromSide(par1);
                if (var4 == 6) return Block.netherBrick.getBlockTextureFromSide(par1);
                if (var4 == 7) return Block.blockNetherQuartz.getBlockTextureFromSide(par1);
            }
            return missing;
    }
     
    /**
         * When this method is called, your block should register all the icons it needs with the given IconRegister. This
         * is the only chance you get to register icons.
         */
        public void registerIcons(IconRegister par1IconRegister)
        {
        	//if (stoneType.equals(""))
     
        	/**Used to test for missing textures*/
        	this.missing = Block.lavaStill.getBlockTextureFromSide(1);
     
        	/**Stone*/
        	this.icon1 = par1IconRegister.registerIcon("stone_slab_top");
        	/**Sand*/
            this.icon4 = par1IconRegister.registerIcon("sand_slab_top");
     
        }

    I know naming the variables to things like var1, or par1 is not usually what people like, it's just easier for me at the moment...

    But yeah, where I am stuck might be a bit obvious, but its at "var1" in "textureType". I'm not sure how to set up something to do what I'm wanting her, but I am trying to get it to assign a number to each type based off the types in "stoneType".

    Setting "var1 = 0" to "var1 = 1" does work as I wanted when I test it, and changes the texture of the block from Stone to Sand as intended, so I know at least that works. So I think it just needs some math... I don't really know since I'm still a beginner with Java.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using a String[] in a Void or INT???

    You have a couple "lists" of if statements that could be simplified by using loops and combining actions. There's no reason that I can see to separately assign values to the variables varX and then do another action based on the value of varX. Why not do it all at once? You can also make better use of arrays to simplify the design.

    These optimizations will occur to you as you gain more experience and confidence, ask questions, and review how others do similar things.

    I'm not sure what you mean by "needs some math." Each type in the array stoneType[] already has a value - the item's index in the array.

    Keep coding!

  6. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using a String[] in a Void or INT???

    Quote Originally Posted by GregBrannon View Post
    You have a couple "lists" of if statements that could be simplified by using loops and combining actions. There's no reason that I can see to separately assign values to the variables varX and then do another action based on the value of varX. Why not do it all at once? You can also make better use of arrays to simplify the design.

    These optimizations will occur to you as you gain more experience and confidence, ask questions, and review how others do similar things.

    I'm not sure what you mean by "needs some math." Each type in the array stoneType[] already has a value - the item's index in the array.

    Keep coding!
    I'm not sure really... I just know that this seems to really be the only thing that works for the codes I am using it for, and that if this loop worked out as I wanted then I'd be able to fix other parts of my code, as "stoneType.equals("stone")" doesn't seem to work in some applications I am trying to use it for, but if it's set up as an Int then it works better.


    What I mean by math is something like this,

            int var9 = par1World.getIndirectPowerLevelTo(var7, par3, var8, Direction.directionToFacing[var6]);
            return var9 >= 15 ? var9 : Math.max(var9, par1World.getBlockId(var7, par3, var8) == Block.redstoneWire.blockID ? par1World.getBlockMetadata(var7, par3, var8) : 0);

    To give an example of one of the things I've tested, maybe I did something wrong, but it didn't seem to work unless I used the loop...


    When using "stoneType.equals("stone")" the block does not register in the menus.
    /**
         * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
         */
        public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
        {
        	{
        		if (par1 != Block.stoneVerticalDoubleSlab.blockID)
                {
        			if (stoneType.equals("stone")) par3List.add(new ItemStack(par1, 1, 0));
                }
        	}
        }

    But, when I use the loop then it works.
    /**
         * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
         */
        public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
        {
        	int var3 = this.textureType();
     
        	{
        		if (par1 != Block.stoneVerticalDoubleSlab.blockID)
                {
        			if (var3 == 0) par3List.add(new ItemStack(par1, 1, 0));
                }
        	}
        }

    So I'm not really sure what else to do at the moment.

    Should I just Pastebin my entire code?

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using a String[] in a Void or INT???

    it didn't seem to work unless I used the loop
    I don't see any loops. The loops in Java are for, while, and do/while. Did you mean there are loops in the code you didn't post?
    When using "stoneType.equals("stone")" the block does not register in the menus . . . So I'm not really sure what else to do at the moment.
    I don't know what problem you're trying to solve. I don't know what "the block does not register in the menus" means. Describe what "it works" and "it doesn't work" mean. Show a sample run if possible, and describe how the sample run would look if it did "work."

    Rather than posting ALL of your code, post a short runnable example that demonstrates the problem you're trying to solve.

  8. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using a String[] in a Void or INT???

    Quote Originally Posted by GregBrannon View Post
    I don't see any loops. The loops in Java are for, while, and do/while. Did you mean there are loops in the code you didn't post?

    I don't know what problem you're trying to solve. I don't know what "the block does not register in the menus" means. Describe what "it works" and "it doesn't work" mean. Show a sample run if possible, and describe how the sample run would look if it did "work."

    Rather than posting ALL of your code, post a short runnable example that demonstrates the problem you're trying to solve.
    Well I mean like, without the "textureType" then the things don't correctly and that it's making it a little easier to handle the "stoneTypes" data.

    getSubBlocks, Is what registers the blocks into menus in Minecraft.

    Colored codes are referenced from earlier post.

    int var3 = this.textureType();

    if (par1 != Block.stoneVerticalDoubleSlab.blockID)
    Tells the game to not register a block.

    if (var3 == 0) par3List.add(new ItemStack(par1, 1, 0));
    Tells the game to register a block. And it's getting the data from "textureType"

    if (stoneType.equals("stone")) par3List.add(new ItemStack(par1, 1, 0));
    This is using the code you tipped me off to, but it's not taking the data from stoneTypes, so I used the texureType that I setup as an attempt to a loop hole, which I think shall help me register that data for other codes when I add them.

    In this image it displays what I'm trying to explain... http://fc06.deviantart.net/fs71/f/20...oi-d7ftlxo.png

    if (var3 == 0) par3List.add(new ItemStack(par1, 1, 0));
    Can be registered as many times as desired and it tells the game to add the block to the list.

    And, if (stoneType.equals("stone")) par3List.add(new ItemStack(par1, 1, 0));
    Is not getting data from the stoneType as I thought it would. Which is why I made the whole textureType as a loop hole for my code.
    Attached Images Attached Images
    Last edited by Sidonius; April 26th, 2014 at 08:38 PM.

  9. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using a String[] in a Void or INT???

    Don't understand what I am trying to explain?

    --- Update ---

    Idk if I was explaining it right before,

    /**This checks slabType for the material and registers a number to each type for other codes to use.*/
        public int textureType()
        {
        	int var1 = 0;
        	{
        		if (slabType.equals("stone")) return 0;
        		else if (slabType.equals("sand")) return 1;
        		else if (slabType.equals("cobble")) return 2;
        		else if (slabType.equals("brick")) return 3;
        		else if (slabType.equals("smoothStoneBrick")) return 4;
        		else if (slabType.equals("netherBrick")) return 5;
        		else if (slabType.equals("quartz")) return 6;
        	}
            return var1;
        }


    What I am trying to do is get either var1, or the return to get data from the parts with the codes with slabType.equals, so then I can use the data from slabTypes

    {
        /** The list of the types of step blocks. */
        public static final String[] slabType = new String[] {"stone", "sand", "cobble", "brick", "smoothStoneBrick", "netherBrick", "quartz"};

    If I can do that then everything should work out as I need it to. I think.

  10. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using a String[] in a Void or INT???

    This is a loop that collapses your string of 'if' statements into a more compact form:
        // returns the index number for the argument
        protected int getElementNumber( String slabType )
        {
            // STONE_TYPES is an array that contains the possible stone types
            for ( int i = 0 ; i < STONE_TYPES.length ; i++ )
            {
                if ( slabType.equals( STONE_TYPES[i] ) )
                {
                    return i;
                }
            }
     
            // default to indicate the specified value was not found
            return -1;
     
        } // end method get ElementNumber()
    Loops and arrays are basic programming tools common to most every programming language. If you're interested in programming, you should pick up a book and learn the basic tools.

    I'll help you learn Java, but I don't do Minecraft.

  11. #10
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using a String[] in a Void or INT???

    I'm not really sure what to do for the STONE_TYPES as the stoneTypes referenced before was changed to slabType, just a name change with the same codes.

    But yeah, I'd love to learn Java, not only for Minecraft but for applications I want to try to make in the future, or even Windows Apps.

    One thing is that I can't really do books to well, I don't really have a way to go to the nearest library, and Idon't know of any near by book stores.
    But any online resources should help just as much, right?

  12. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using a String[] in a Void or INT???

    But any online resources should help just as much, right?
    There are excellent online resources for learning Java, but there is also some crap out there. I wouldn't say ANY online resource will be helpful.

    A good place to start is the Java Tutorials, but there are others.

  13. #12
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using a String[] in a Void or INT???

    Thanks, I will look into that. ^^

    And, what about the STONE_TYPES thing?

    Quote Originally Posted by Sidonius View Post
    I'm not really sure what to do for the STONE_TYPES as the stoneTypes referenced before was changed to slabType, just a name change with the same codes.
    Quote Originally Posted by GregBrannon View Post
        protected int getElementNumber( String slabType )
        {
            // STONE_TYPES is an array that contains the possible stone types
            for ( int i = 0 ; i < STONE_TYPES.length ; i++ )
            {
                if ( slabType.equals( STONE_TYPES[i] ) )
                {
                    return i;
                }
            }
     
            // default to indicate the specified value was not found
            return -1;
     
        } // end method get ElementNumber()[/highlight]

Similar Threads

  1. Public static void (String[] args)
    By Ganjasaur in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2013, 02:06 PM
  2. The operator + is undefined for the argument type(s) String, void
    By Proletariat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 7th, 2012, 12:10 AM
  3. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM
  4. Need a non void method to return the first three characters of a string
    By fallout87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 9th, 2011, 10:00 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM