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

Thread: Using Int to get a value from a string?

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

    Default Using Int to get a value from a string?

    This post retains to the default code given when you decompile Minecraft and... Obfuscate it? I'm not sure the correct terms yet, so please excuse me.
    Anyways, variable names I have gotten from the code I've gotten from Minecraft using MinecraftCoderPack & Eclipse. Please don't hound me about the variable names. I am trying my best based on what I know now. I am still learning Java scripting.

    I believe I posted something similar, or about this a while ago, but I was not able to understand the response fully to the point I could implement it to my needs.



    So... I have a string that I am wanting to get a number from.

    Example:
    public static final String[] exampleLetters = new String[] {"a", "b", "c", "d", "e", "f", "g"};

    And, using a public Int (with no variables in the title), I am looking to take numbers for each string letter, to be able to output the value to other code.
    Example:
    public int letterConversion()

    a = 0, b = 1, and so on to g = 6
    I hope you get the idea.

    How would I be able to get the numerical values of each letters using a public Int without any variables in the title?

    I am not looking for someone to code my Minecraft mod, I am just looking for a point of reference to do what I am trying to for this specific sector I am working on at the moment.

    ---

    Actual Code I have right now. Again, please ignore variable names, it's what I got use to while learning.


    /** The list of the types of step blocks. */
        public static final String[] slabType = new String[] {"stone", "sand", "cobble", "brick", "smoothStoneBrick", "netherBrick", "quartz"};
     
     
        }
     
        /**This checks slabType for the material and registers a number to each type for other codes to use.*/
        public int textureType()
        {
        	int var1 = -1;
        	{
        		for ( int var2 = 0 ; var2 < slabType.length ; var2++ )
        		if (var2 == 0) return var1 = 0;
        		else if (var2 == 1) return var1 = 1;
        		else if (var2 == 2) return var1 = 2;
        		else if (var2 == 3) return var1 = 3;
        		else if (var2 == 4) return var1 = 4;
        		else if (var2 == 5) return var1 = 5;
        		else if (var2 == 6) return var1 = 6;
        	}
            return var1;
        }

    For what I have now, in the Int...

    for ( int var2 = 0 ; var2 < slabType.length ; var2++ )

    Is controlling the output. No mater what the 0 is the output, and I am not sure exactly why due to my still learning.
    Last edited by Sidonius; October 10th, 2014 at 01:00 AM.


  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using Int to get a value from a string?

    Lets start with a basic point: your for loop.

    int var1 = -1;
    {
        for ( int var2 = 0 ; var2 < slabType.length ; var2++ )
            if (var2 == 0) return var1 = 0;
            .....
    }

    Firstly, consider where your brackets are placed, where have you placed them around the outside of the for loop?

    Secondly, let us read the statement above: Instantiate variable 2 to having value 0, while its value is less than the length of slabType, increment it by one, performing the statements enclosed each time.
    So for variable2=0, if variable2 is equal to zero, we return 0, and end execution, do you see the problem here? Consider what you are telling the program when you say 'return xxxx'.

    As a quick question so I might be able to help more after, are you trying to have a set of string values, which each have an integer value attached to them?
    So for instance, rock = 1, paper = 2 and scissors = 3?
    Then another method can say value 1, and you know you have a rock?

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

    Default Re: Using Int to get a value from a string?

    Quote Originally Posted by AGunner View Post
    Lets start with a basic point: your for loop.

    int var1 = -1;
    {
        for ( int var2 = 0 ; var2 < slabType.length ; var2++ )
            if (var2 == 0) return var1 = 0;
            .....
    }

    Firstly, consider where your brackets are placed, where have you placed them around the outside of the for loop?

    Secondly, let us read the statement above: Instantiate variable 2 to having value 0, while its value is less than the length of slabType, increment it by one, performing the statements enclosed each time.
    So for variable2=0, if variable2 is equal to zero, we return 0, and end execution, do you see the problem here? Consider what you are telling the program when you say 'return xxxx'.

    As a quick question so I might be able to help more after, are you trying to have a set of string values, which each have an integer value attached to them?
    So for instance, rock = 1, paper = 2 and scissors = 3?
    Then another method can say value 1, and you know you have a rock?
    I am not sure if this site is considered decent for learning the basics, but it's what I'm using at the moment to learn just Java for now. Learn | Codecademy
    So I don't yet know a lot of the basic stuff I should, but I can usually figure out how to implement stuff when I see it used in other code.

    I am not sure how I'd increment slabType one by one, I don't think I've gotten that far into my learning yet. Could you show me an example?

    Anyways, this is a basic diagram of what I am trying to do with the code:
    Example.jpg
    The blue being what I already have working based on test.

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

    Default Re: Using Int to get a value from a string?

    Bump

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Using Int to get a value from a string?

    What do you mean increment slabType? that is of String[].. which you cannot increment.

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

    Default Re: Using Int to get a value from a string?

    Quote Originally Posted by camel-man View Post
    What do you mean increment slabType? that is of String[].. which you cannot increment.
    Based on @AGunner 's reply to the post. I'm not sure if that's the right term, I think It's supposed to be incrementing the values in the int? I am uncertain.

Similar Threads

  1. 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
  2. string-int-char
    By mdhmdh100 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 25th, 2012, 01:35 PM
  3. String to Int
    By Joy123 in forum Java Theory & Questions
    Replies: 16
    Last Post: June 23rd, 2011, 08:41 AM
  4. String[] and Int[]
    By frozen java in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 17th, 2011, 03:01 PM
  5. String and int problem in swing program
    By duckman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2009, 02:28 AM