Thread: [Array Help] Need help :/

Page 1 of 2 12 LastLast
Results 1 to 10 of 19
  1. #1 [Array Help] Need help :/ 
    Registered Member the a g s's Avatar
    Join Date
    Nov 2008
    Posts
    52
    Thanks given
    0
    Thanks received
    1
    Rep Power
    1
    ok, so, i've learnt to use arrays recently, and find they can come in use alot, so i tried to make a "Shorter" ::talk command, because i have all my staffs names and stuff on it when they talk, so here's code, i can't figure out how to have multiple declarations of an array in one bunch of code, here's my code, including my String array;

    if ((command.startsWith("talk")) && (command.length() > 5) && (playerRights >= 0)) {
    String Titles = "";


    /* Titles */

    if (playerRights == 1) { Titles = Prt[0]; }
    if (playerRights == 2) { Titles = Prt[1]; }
    if (playerRights == 3) { Titles = Prt[2]; }
    if (playerRights == 4) { Titles = Prt[3]; }
    if (donator == 1) { Titles = "[V.I.P] "; }
    if (playerRights == 0) { Titles = "[Player] "; }
    if (playerName.equalsIgnoreCase(NSO[0] + NSO[1] + NSO[2] + NSO[3])) { Titles = "[##### Owner/Co-owner]"; }


    talk(Titles + "" + playerName + ": "
    + command.substring(5));
    }
    so, thats it at the moment, this bit,
    (NSO[0] + NSO[1] + NSO[2] + NSO[3]))
    is where i get stuck, see i use multiple declarations of the array values, but i only wish to use one declaration, is there any way to do that? :/

    The Array;
    public String[] NSO =
    {"#####",
    "#####",
    "#####",
    "#####"};
    as you can see i like clean code

    Changed the values to hash's because it would be advertising,

    I did the same for PlayerRights
    String Prt[] = {"[Moderator] ", "[Admin] ", "[Co-Owner] ", "[Forum Mod] ", "[Global Mod] "};
    Web Developer for Three years, Java coder for 5.

    Known coding languages:
    #1 - Java (Experienced)
    #2 - C++ (Beginner)
    #3 - Web Development (HTML, CSS, JavaScript, PHP and XML)
    #4 - C# (Intermediate)
    #5 - Visual Basic (Master) - Who isn't?
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Sep 2009
    Posts
    201
    Thanks given
    0
    Thanks received
    17
    Rep Power
    147
    String Prt[] = {"[Player]", "[Moderator] ", "[Admin] ", "[Co-Owner] ", "[Forum Mod] ", "[Global Mod] "};
    String NSO[] = {"Fred", "Jenny", "Steven", "Owns"};
    if ((command.startsWith("talk")) && (command.length() > 5) && (playerRights >= 0)) {
    String Titles = "";
    Titles = Prt[playerRights];
    if (donator == 1) { Titles = "[V.I.P] "; }
    for(int i = 0; i < NSO.length;i++){
    if (playerName.equalsIgnoreCase(NSO[i])) { Titles = "[Owner/Co-owner]"; }
    }
    talk(Titles + "" + playerName + ": " + command.substring(5));
    }
    there and i neatend it

    or a 2d array
    String Prt[] = {"[Player]", "[Moderator] ", "[Admin] ", "[Co-Owner] ", "[Forum Mod] ", "[Global Mod] "};
    String NSO[][] = {{"Fred", "Jenny", "Steven", "Owns"}, {"Jewish", "Fat", "Owner", "Co-Owner"}};
    if ((command.startsWith("talk")) && (command.length() > 5) && (playerRights >= 0)) {
    String Titles = "";
    Titles = Prt[playerRights];
    if (donator == 1) { Titles = "[V.I.P] "; }
    for(int i = 0; i < NSO.length;i++){
    if (playerName.equalsIgnoreCase(NSO[i])) { Titles = "[" + NSO[i][i] + "]"; }
    }
    talk(Titles + "" + playerName + ": " + command.substring(5));
    }
    Reply With Quote  
     

  3. #3  
    Banned

    Join Date
    Jul 2008
    Age
    28
    Posts
    5,827
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Heres your code: (Btw, shorter is NOT better)
    Code:
            if (command.startsWith("talk") && command.length() > 5) {
                StringBuilder prefix = new StringBuilder(32);
                prefix.append(staffPrefix.get(playerRights));
                if (donator == 1) {
                    prefix.append("[V.I.P]");
                }
                if (owners.get(playerName) != null) {
                    prefix.append("[Owner/Co-owner]");
                }
                prefix.append(playerName);
                prefix.append(": ");
                prefix.append(command.substring(5));
                talk(prefix.toString());
            }
    Declare this:
    Code:
        private Map<Integer, String> staffPrefix = new HashMap<Integer, String>() {
    
            {
                Object[][] pre = {
                    {"Player", 0},
                    {"Moderator", 1},
                    {"Admin", 2},
                    {"Co-Owner", 2},
                    {"Forum Mod", 2},
                    {"Global Mod", 2}
                };
                for (Object[] o : pre) {
                    put((Integer) o[1], (String) o[0]);
                }
            }
        };
        private Map<String, Object> owners = new HashMap<String, Object>() {
    
            {
                String[] own = {
                    "Fred", "Jenny", "Steven", "Owns"
                };
                for (String s : own) {
                    put(s, new Object());
                }
            }
        };
    You better use that shit
    Reply With Quote  
     

  4. #4  
    Registered Member

    Join Date
    Sep 2009
    Posts
    201
    Thanks given
    0
    Thanks received
    17
    Rep Power
    147
    you better read my 2d array one lol
    i could of done what i posted better but is there much point he wont learn anything
    Reply With Quote  
     

  5. #5  
    Banned

    Join Date
    Jul 2008
    Age
    28
    Posts
    5,827
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by steven031291 View Post
    you better read my 2d array one lol
    i could of done what i posted better but is there much point he wont learn anything
    Yours isn't even indented kid.
    Reply With Quote  
     

  6. #6  
    Registered Member the a g s's Avatar
    Join Date
    Nov 2008
    Posts
    52
    Thanks given
    0
    Thanks received
    1
    Rep Power
    1
    i will to learn from this, thanks for the help, i learnt that, in order for a array to work i have to use i, which i'm guessing means interger, so, yea , i don't just steal, i find what i did wrong , i'm not a leecher , so i have to declare i as a int , i'm learnin by just reading your code , and dude, sweet hash map method, i'm not onto that yet though so... eh..., i'd prefer not to use it at the moment incase i mess it up :L
    Web Developer for Three years, Java coder for 5.

    Known coding languages:
    #1 - Java (Experienced)
    #2 - C++ (Beginner)
    #3 - Web Development (HTML, CSS, JavaScript, PHP and XML)
    #4 - C# (Intermediate)
    #5 - Visual Basic (Master) - Who isn't?
    Reply With Quote  
     

  7. #7  
    Banned

    Join Date
    Jul 2008
    Age
    28
    Posts
    5,827
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by the a g s View Post
    i will to learn from this, thanks for the help, i learnt that, in order for a array to work i have to use i, which i'm guessing means interger, so, yea , i don't just steal, i find what i did wrong , i'm not a leecher
    Mine is better, learn from it. Looping arrays is bad.
    Reply With Quote  
     

  8. #8  
    Registered Member

    Join Date
    Sep 2009
    Posts
    201
    Thanks given
    0
    Thanks received
    17
    Rep Power
    147
    i was just saying because he might want to make custom titles

    if (playerName.equalsIgnoreCase(NSO[i])) { Titles = "[" + NSO[i][i] + "]"; }
    p.s
    "Fred", "Jenny", "Steven", "Owns"
    is from mine lol


    yes his is better

    but the i was a int thats right its stated in the for(int i = 0; i < blahblahblah
    Reply With Quote  
     

  9. #9  
    Registered Member the a g s's Avatar
    Join Date
    Nov 2008
    Posts
    52
    Thanks given
    0
    Thanks received
    1
    Rep Power
    1
    Quote Originally Posted by Colby View Post
    Mine is better, learn from it. Looping arrays is bad.
    ok dude, like can you direct me to a website via pm that i can learn to do those hash maps and stuff cos, i can't buy a book for it atm :/
    btw, what is a map? i'm experienced at java but not that much
    Web Developer for Three years, Java coder for 5.

    Known coding languages:
    #1 - Java (Experienced)
    #2 - C++ (Beginner)
    #3 - Web Development (HTML, CSS, JavaScript, PHP and XML)
    #4 - C# (Intermediate)
    #5 - Visual Basic (Master) - Who isn't?
    Reply With Quote  
     

  10. #10  
    Registered Member

    Join Date
    Sep 2009
    Posts
    201
    Thanks given
    0
    Thanks received
    17
    Rep Power
    147
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

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