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

Thread: String Help Needed

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String Help Needed

    I'm extremely new to Java. I'm in a very basic Java programming class that's really boring and tedious, and I highly doubt we're going to learn anything like this all year, but what I was wondering is...

    Can a code be written to define both a string's contents as well as the string's name based on an array of integers--which will be referred back to by a constructor method--perhaps by "text" + .toString + "text" to make up the name of a certain file (an icon)?

    I want to make it so that I can have an icon for north-, south-, east- and west-facing characters, direction being defined by one integer in the constructor, while using another integer in the constructor to change the set of icons used by that object. What I have written looks like this, which I built off of the existing code in the jar file our class uses as a classpath:

    static ImageIcon getCharacterIcon(int direction, int app){
    switch (direction) {
    case NORTH: {
    if (app == 1) {
    if (nbm == null)
    nbm = new ImageIcon(Display.class.getResource(nbmLocation));
    return nbm;}
    else {
    if (nkarel == null)
    nkarel = new ImageIcon(Display.class.getResource(nkarelLocation ));
    return nkarel;}
    }
    case EAST: {
    if (app == 1) {
    if (ebm == null)
    ebm = new ImageIcon(Display.class.getResource(ebmLocation));
    return nbm;}
    else {
    if (ekarel == null)
    ekarel = new ImageIcon(Display.class.getResource(ekarelLocation ));
    return ekarel;}
    }
    case SOUTH: {
    if (app == 1) {
    if (sbm == null)
    sbm = new ImageIcon(Display.class.getResource(sbmLocation));
    return sbm;}
    else {
    if (skarel == null)
    skarel = new ImageIcon(Display.class.getResource(skarelLocation ));
    return skarel;}
    }
    case WEST: {
    if (app == 1) {
    if (wbm == null)
    wbm = new ImageIcon(Display.class.getResource(wbmLocation));
    return wbm;}
    else {
    if (wkarel == null)
    wkarel = new ImageIcon(Display.class.getResource(wkarelLocation ));
    return wkarel;}
    }
    default:
    Debug.printError("Karel image or character for direction " + direction + " or " + app + " not found! Aborting...");
    System.exit(7);
    return null;
    }
    }

    I don't know if it works yet, but I think it does. Fairly sure it compiles and everything, or would if I had code identifying the bm strings. I was hoping to be able to use an integer array with both karel and bm--or, at this point, just bm so far, with karel as the default--to make strings and whatnot, but I have no idea if that's possible or anything, and no one in my class (or the teacher, because he only really knows what he has to teach) would know either. I didn't want to have more of:

    private static final String nkarelLocation = "/icons/kareln.gif";

    private static ImageIcon nkarel = null;

    for each new icon I add, but based on the information at my disposal, I have no other way. Please help?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    define both a string's contents as well as the string's name
    No. The name of the String variable is defined when you type it into the source file.

    You can build Strings that are filenames by concatenating Strings as you are currently doing.

    I'm not sure what questions you are asking. Can you be more specific if you are asking a question?
    Like start with "How do I" and add ? at the end

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

    Well the way I have the program written (It works for sure now :3), I have to change the hunk of program I just showed to include all my new icons, as well as add a bunch more strings. I was hoping I could write an ImageIcon method or String method or whatever that would be able to access/create the correct ImageIcon based on only the direction and appearance integers. I mean, I have .replace("karel", "newcharacter");, which I could write as .replace("karel", appearance[app].toString());, I think? But that would still leave me with the same String name, regardless of what file I got it to identify...so is there some way I could use the appearance integer array to dictate which set of icons to use without having to write 20 odd new lines of code for each one? (I also set this.app = Display.appearance[app]; in the constructor method, so that it returns bm or karel or whatever)

    Is my question unclear, or am I just trying to do something impossible out of laziness?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    is there some way I could use the appearance integer array to dictate which set of icons to use without having to write 20 odd new lines of code for each one?
    Can you show the code you are talking about? What is the "appearance integer array"?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    When you post code, wrap your code with
    [code=java]<YOUR CODE HERE>
    [/code]
    to get highlighting

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

          public static final int karel = 0;
     
          public static final int bm = 1;
     
     static final int[] appearance = { karel,
             				  bm
             					 };
    It was relevant in the original draft, but now I think it's mostly vestigial, and only used in one or two spots for things that don't actually affect how the program runs.
    I'm not entirely sure how to use it. I only just independently started messing with these codes last night. All the language is way beyond what we've learned in class so far.

    :c

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    Can you explain what filenames you are trying to generate and how the contents of the array is to be used.
    The int array you posted has two values in it: 0 & 1

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

    I think I was using it before to make a
    private static String stringname = "/icons/" + appearance[app].toString() + "w.gif";
    or something like that. int app is an argument used in the constructor method for the various little characters.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    The variable: app can only have two values because that is the size of the array that you posted earlier.

  10. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

    This is true, but the values represent sets of icons. The specific icons are defined by another integer in the constructor that also aligns with an array. Those are directions. The one I just posted is case WEST: I suppose in a switch(directions){ where directions is the other array I mentioned.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    appearance[app].toString()
    Does this compile? You can't use an method with an int variable.

  12. #12
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

    Not sure, actually. But I had something sort of similar to that. I have not actually found a way to use it, as I said. >:

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    I'm losing track of what your problem is.
    Can you explain what you have now and what your problems are?

  14. #14
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

    What I have now is a really inefficient method of adding new sprites to be accessed by constructor methods.
    Which image is displayed at any given point in time is dictated by two integers/arguments in the constructor. One says which set of four icons to pull from. The other says which direction the object is facing, which indicates which of the four icons in the indicated set should be displayed by the object made by the constructor.

    In order to add new icons to be used, I have to either write a new class and override the command that renders icons from the set containing bm and karel, or, I could add an

    else if ( app == whatevernumber )

    into that first block of program I posted that creates ImageIcons for each new set of 4 icons I wish do use. I also have to go back and write a string for that code to call up for each icon I add.

    What I've been trying to ask is whether there is a way I can abridge the process of adding new sets of icons/sprites. I don't know much about programming, but I was hoping, since I can put the integers into an array, and since I can make the array identify the characters that would need to be changed to make the proper filename (I could even rewrite the directions to identify the e (for EAST) in like karele.gif or bme.gif. However. If I told it to identify ImageIcons that way, which I think I could do, I still don't know how to make it so that each time it needed to use a new icon, it didn't use the same ImageIcon name.

    However, if you know of any way I could speed up the process of adding new icons, I would be very grateful. As I said. Each new set of four icons added would currently take me a good twenty lines of code or so (because I have to write in a separate String and ImageIcon for each).

  15. #15
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

      /**
        * Defined direction for east (right).
        */
          public static final int EAST = 0;
       /**
        * Defined direction for north (up).
        */
          public static final int NORTH = 1;
       /**
        * Defined direction for west (left).
        */
          public static final int WEST = 2;
       /**
        * Defined direction for south (down).
        */
          public static final int SOUTH = 3;
     
     
          private static final int[] directions = { NORTH,
                                               EAST,
                                               SOUTH,
                                               WEST
                                               };
     
    /**
        * Location of the image of karel facing north.
        */
          private static final String nkarelLocation = "/icons/kareln.gif";
       /**
        * Location of the image of karel facing east.
        */
          private static final String ekarelLocation = "/icons/karele.gif";
       /**
        * Location of the image of karel facing south.
        */
          private static final String skarelLocation = "/icons/karels.gif";
       /**
        * Location of the image of karel facing west.
        */
          private static final String wkarelLocation = "/icons/karelw.gif";
     
     
       /**
        * Image icon where the north-facing karel is loaed.
        */
          private static ImageIcon nkarel = null;
       /**
        * Image icon where the east-facing karel is loaded.
        */
          private static ImageIcon ekarel = null;
       /**
        * Image icon where the south-facing karel is loaded.
        */
          private static ImageIcon skarel = null;
       /**
        * Image icon where the west-facing karel is loaded.
        */
          private static ImageIcon wkarel = null;
     
       	/**
        * Location of the image of karel facing north.
        */
          private static final String nbmLocation = "/icons/bmn.gif";
       /**
        * Location of the image of karel facing east.
        */
          private static final String ebmLocation = "/icons/bme.gif";
       /**
        * Location of the image of karel facing south.
        */
          private static final String sbmLocation = "/icons/bms.gif";
       /**
        * Location of the image of karel facing west.
        */
          private static final String wbmLocation = "/icons/bmw.gif";
     
     
       /**
        * Image icon where the north-facing karel is loaed.
        */
          private static ImageIcon nbm = null;
       /**
        * Image icon where the east-facing karel is loaded.
        */
          private static ImageIcon ebm = null;
       /**
        * Image icon where the south-facing karel is loaded.
        */
          private static ImageIcon sbm = null;
       /**
        * Image icon where the west-facing karel is loaded.
        */
          private static ImageIcon wbm = null;
     
          public static int validateDirection(int dir) {
             for (int i = 0; i < directions.length; i++)
                if (dir == directions[i])
                   return dir;
     
             return ((dir % 4) + 4) % 4; //This is in case it's negative
          }
     
          static ImageIcon getCharacterIcon(int direction, int app){
             switch (direction) {
                case NORTH: 
                   {
                      if (app == 1) {
                         if (nbm == null)
                            nbm = new ImageIcon(Display.class.getResource(nbmLocation));
                         return nbm;}
                      else {
                         if (nkarel == null)
                            nkarel = new ImageIcon(Display.class.getResource(nkarelLocation));
                         return nkarel;}
                   }
                case EAST: 
                   {
                      if (app == 1) {
                         if (ebm == null)
                            ebm = new ImageIcon(Display.class.getResource(ebmLocation));
                         return ebm;}
                      else {
                         if (ekarel == null)
                            ekarel = new ImageIcon(Display.class.getResource(ekarelLocation));
                         return ekarel;}
                   }
                case SOUTH: 
                   {
                      if (app == 1) {
                         if (sbm == null)
                            sbm = new ImageIcon(Display.class.getResource(sbmLocation));
                         return sbm;}
                      else {
                         if (skarel == null)
                            skarel = new ImageIcon(Display.class.getResource(skarelLocation));
                         return skarel;}
                   }
                case WEST: 
                   {
                      if (app == 1) {
                         if (wbm == null)
                            wbm = new ImageIcon(Display.class.getResource(wbmLocation));
                         return wbm;}
                      else {
                         if (wkarel == null)
                            wkarel = new ImageIcon(Display.class.getResource(wkarelLocation));
                         return wkarel;}
                   }
                default:
                   Debug.printError("Karel image or character for direction " + direction + " or " + appearance[app] + " not found!  Aborting...");
                   System.exit(7);
                   return null;
             }
          }

    This is essentially everything behind the scenes of the constructor method that uses

    direction = validateDirection(int dir);

    and basically just an integer named app as arguments.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    Using arrays could help simplify the code.
    Its hard to figure out what the code is supposed to do.
     if (app == 1) {
    This test has an else with it implying that the value of app could be anything.
    Can app have more than 2 values? If its 1 do the first part, if its not one do something else.
    If there are only every 2 values, then use a boolean variable which can only have two values.

  17. #17
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

    Well app is an integer that can have absolutely any value. At first I set karel as 0, but as an afterthought, I wrote it as a default instead, which makes more sense in my opinion. If I added four more icons, I'd add the

     else if ( app == 2 ) {

    and write new strings. I was hoping there was a way I could just add the changes that would need to be made to the default icon filenames as the various integers in an array so that I would not have to manually write new strings and else if conditions for each one.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    You should also look at using an enumeration to define the directions, instead of using an int.
    That would keep the value of the direction variable in the fixed range that you need.

  19. #19
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Help Needed

    I honestly don't even have any idea what that means.
    ...I have a lot to learn. ^^'

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Help Needed

    Go to this site and Find Enum:
    The Really Big Index

Similar Threads

  1. Replies: 7
    Last Post: December 11th, 2011, 11:58 PM
  2. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  3. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  4. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  5. Read URL into string - Cookies needed?
    By bhanness in forum Java Networking
    Replies: 0
    Last Post: March 12th, 2010, 05:03 AM