Results 1 to 2 of 2
  1. #1
    whooplaah is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default String Method returning... odd things

    This may be a simple thing. I'm new to Java, and I don't even know how to describe this anomaly to Google...

    Part of a game that I am writting uses a random name generator. Rather than having and array of names, I have an array of syllables that I piece together to make names. When I run the nameGen class by itself it spits out a name, and I've pasted the same code into another class and it returns names there as well (I couldn't get the return to work, so I just pasted the same method in the class that needed it).

    However, I am now trying to pull these returns into a TextField so that I can generate new names with the push of a button. When I do this the nameGen class is returning values such as "nameGen@23478923" instead of a name like "Rowidi."

    The code for the name generator is:

    Java Code:
    import java.util.*;
    
    public class nameGen
    {
        /* The following is based on Wood Elf language*/
        /* 105 entries*/ String[] syl_2={"b","d","f","h","j","l","m","n","r","s","w","y","z","zh","v","b","d","f","h","h","h","j","l","l","l","l","l","l","m","m","n","n","r","s","s","s","s","w","w","y","y","y","z","z","zh","v","v","v","v","bw","dw","hw","lw","sw","yw","zw","hs","br","dr","fr","hr","lr","mr","nr","rr","sr","zr","dd","ll","mm","nn","rr","ss","zz","dm","dn","zhr","zhl","v","vl","vr","sv","a","e","i","o","u","y","ae","ai","ao","ay","ea","ei","eo","eu","ey","ia","ie","io","iu","oa","oe","oi","ou","oy"};
        /* 6  entries*/  String[] syl_sv={"a","e","i","o","u","y"};
        /* 18 entries*/  String[] syl_dv={"ae","ai","ao","ay","ea","ei","eo","eu","ey","ia","ie","io","iu","oa","oe","oi","ou","oy"};
        /* 44 entries*/  String[] syl_sc={"b","d","f","h","j","l","m","n","r","s","w","y","z","v","b","d","f","h","h","h","j","l","l","l","l","l","l","m","m","n","n","r","s","s","s","s","w","w","v","y","y","y","z","z","zh","v","v","v","v"};
        /* 34 entries*/  String[] syl_dc={"zh","bw","dw","hw","lw","sw","yw","zw","hs","br","dr","fr","hr","lr","mr","nr","rr","sr","zr","dd","ll","mm","nn","rr","ss","zz","dm","dn","zhr","zhl","vl","vr","sv"};
       String first()
        {
            dice d = new dice();
            int b = d.d(4);
            String name="";
            if (b==1)
            {
                name+=syl_sc[d.d(44)-1]+syl_dv[d.d(18)-1]+syl_dc[d.d(34)-1]+syl_sv[d.d(6)-1];
                name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
                //System.out.println("Name: "+name);
            }
            else if (b==2)
            {
                name+=syl_sc[d.d(44)-1]+syl_dv[d.d(18)-1]+syl_dc[d.d(34)-1]+syl_sv[d.d(6)-1];
                name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
                //System.out.println("Name: "+name);
            }
            else if (b==3)
            {
                name+=syl_sc[d.d(44)-1]+syl_dv[d.d(18)-1]+syl_dc[d.d(34)-1]+syl_sv[d.d(6)-1];
                name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
                //System.out.println("Name: "+name);
            }
            else if (b==4)
            {
                name+=syl_sc[d.d(44)-1]+syl_dv[d.d(18)-1]+syl_dc[d.d(34)-1]+syl_sv[d.d(6)-1];
                name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
                //System.out.println("Name: "+name);
            }
            else
            {
                System.out.println("There might have been an error just then...\n Want to try again?");
            }
            return name;
        }
    }
    The code for the TextField where the result is being displayed is:

    Java Code:
      public void actionPerformed(ActionEvent e){
          nameGen name = new nameGen();
          String n=""+name;
          numClicks++;
          text.setText(numClicks+" "+n);
        }
    and a screenshot of the result is:

    String Method returning... odd things-untitled.png

    Any suggestions will be appreciated, though I really don't want to loose nameGen to an array of prefabricated names.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: String Method returning... odd things

    Java Code:
    String n=""+name;
    The + being used here is called the "string concatenation operator". What is does is run two strings together. In this case the first string is empty and the second one is the string form of name. The string forms of instances of classes can look pretty weird: like nameGen@5e4b2b75. The first part is the name of the class and the second is some form of identifier.

    This is not what you want of course! Either call the first() method of name and use that in the text field. Or give the nameGen class a toString() method. Where a toString() method exists it will beused instead of the default one which is returning the weird String form.

    Java Code:
    public String toString() {
        return first();
    }
    -----

    It would be a good idea to follow Java coding conventions and start classes with an upper case letter: NameGen, Dice etc.

Similar Threads

  1. Returning A String Using A Method
    By Extraho Vinco in forum New To Java
    Replies: 15
    Last Post: 05-07-2012, 10:23 AM
  2. Replies: 1
    Last Post: 12-15-2011, 04:02 AM
  3. My method keeps returning 0
    By ToolJob in forum New To Java
    Replies: 11
    Last Post: 03-27-2011, 05:22 PM
  4. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  5. Returning Value from a method
    By Mirix in forum New To Java
    Replies: 12
    Last Post: 06-01-2010, 09:48 PM

Tags for this Thread

Posting Permissions

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