Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39
  1. #21
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hi JohnnyR,

    Firstly - thank you so much for helping. I can now see how this code works! Your a star! :-)

    The only problem now, is that when I compile I get cannot find symbol - variable length. I thought this was built in with java?

    I'm confused! Feel free to shout at me!

    All the best - FMJ

  2. #22
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Oh Sorry!

    It needs to be variable.length() - not .length

    This should fix it :)

    I highly recommend the use of an IDE like eclipse or netbeans.

    It tells you these errors pre-compile and makes them easy to figure out :)

  3. #23
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hehe! Thanks Johnny you are a star! Your going to scream big time!

    Now I'm getting an error cannot find symbol class Char? Relating to:

    this line:

    Char temp = args.charAt(i);

    Its been a long day, maybe my typo?

    Cheers - FMJ.

  4. #24
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Yeah..

    This is actually my fault... my first post this morning I wrote it from my phone and it auto put caps locks on first letters etc etc...

    Char temp needs to be char temp ( no caps )

  5. #25
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    I changed Char to char (lowercase) this seems to work :-)

    Hopefully thatz the problem.

    Cheers - FMJ.

  6. #26
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Johnny - Thank you so much for your help. I have been tearing my hair-out for the past two days trying to solve this. I really am very grateful.

    One final question and I promise I will send you a can of beer in the mail.

    How can I test this code in my workspace?

    I type in:

    new myString("testme"); and I get the error java.lang.nullpointerexception. This is a really stupid n00bie question, but I'm not sure how to execute this.

    All the best - FMJ.

  7. #27
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Seeing as your playing with objects you would need to give it a reference.
    (Unless you start it strait into a location of say an array.

    myString object = new myString("testme");

    Assuming that myString is your class name - I don't have all your code so I can't really see. If your using an IDE try debugging to see where the pointer exception comes from.

  8. #28
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey JohnnyR - i'm still getting a nullpointerexception! Dang!

    The code is very simple. I have not been taught debugging yet.

    My whole code:

    /**
    * myString class
    */
    public class myString
    {
    /* instance variables */
    private char[] test;


    /**
    *Constructor
    */
    public myString(String args)
    {
    for (int i = 0; i < args.length(); i++)
    {
    char temp = args.charAt(i);
    temp[i] = temp;
    }
    }
    }

    [/code]

    Can you see anything that looks wrong? Cheers - FMJ.

  9. #29
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    I looked on the net, and two possible causes of getting a nullpointerexception using arrays is:

    1. You've indexed past the array bounds
    2. You haven't initialized the array correctly

    Maybe I have not initialized the array correctly?

    Cheers - FMJ.

    PS - I'm sorry for all this hassle.

  10. #30
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    most likely it's that you haven't initialized the array. otherwise, you would probably get an array out of bounds exception.

  11. #31
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    It's option 2.

    In this example I have initialized it with in the constructor.

    Arrays need to be pre-defined (size set at start unlike Lists which can change).

    You also had a typo anyway :) temp[i] = temp; should of been test[i] = temp;

    Java Code:
    public class myString {
        /* instance variables */
        private char[] test;
    
        /**
         * Constructor
         */
        public myString(String args) {
            test = new char[args.length()];
            for (int i = 0; i < args.length(); i++) {
                char temp = args.charAt(i);
                test[i] = temp;
            }
        }
    }

  12. #32
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Thanks Guys - my brain is fried today, so I will look into this further tomorrow.

    Once again! Respect for all your help today. I'll keep you all posted as to my progress.

    Cheers - FMJ.

  13. #33
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    That works! Yippppieeee!

    I have created an array of type char , attribute test... but the Inspector in the value field says "inspect array".... i guess this is correct. I was expecting an array that would actually list each character in "testme" string.

    Does this sound normal?

    Cheers - FMJ.

  14. #34
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Sorry to clarify - For some reason, I thought the array would actually show each character in the string as char in order:

    e.g testme = 0t 1.e 2 s 3 t 4 m 5 e instead the inspector just shows inspect array. I guess this is correct... am I making sense? Very little I guess :-)

    Cheers - FMJ.

  15. #35
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    It will show them in order built as a string.

    If you want them broken up you would need to do something like
    Java Code:
    for (int i = 0; i < test.length(); i++) {
           System.out.print(test[i] + ", ");
    }

  16. #36
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Johnny R - It works a treat. I would like to take this opportunity to thank you all for your kind help over the past couple of days. I have moved leaps and bounds over the past couple of days in terms of java knowledge.

    (but I'm sure you still wouldn't give me a job as a Java programmer! Hehe)

    Thanks everyone! Thread solved! :-)

    Cheers - FMJ.

  17. #37
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Ahaha :) I only learnt java this year.

    It was a pathway from my old job into programming job, but I had the benefit of 40+ J2EE techos teaching my on the job + i live with 2 who are very helpful so concepts etc were really easy to learn.

    At the moment I'm trying to get my head around how we incorporate Struts/Spring XML JSPs etc with our work.

    The project I'm on is so freakin big and horrible and I find redundant horrible stuff every day :(

  18. #38
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Only learnt Java this year! Your a java genuis to me mate:-)

    What I would give to have a programmer or two living at my house. They would just be getting here is $300 dollars and please sort my assignment! Hehe.

    I'm quite nervous on these Java forums, I keep expecting to get flammed big time!

    I'm 100% I will be in touch again shortly, as I run into further problems... there must be a way of sending some beers over to you for your help! LOL.

    All the best - FMJ.

  19. #39
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    As a note... we only flame ppl who ask for help when they don't do anything :D And learning core java only takes a year or so :P Look at me, 10 months and I can do some pretty crazy programs :P (READ THE SIGNATURE!!!)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 06:01 PM
  2. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  4. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 10:08 AM
  5. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 02:50 PM

Posting Permissions

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