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

Thread: character problem

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default character problem

    I am writing a lexical analyzer for a fictional language in java. I have a for loop which prints out the type and the tokens read. The thing is it prints one character at a time and there is a reserved word 'print'. I am having trouble with making it recognize that it is a reserved word and not just a normal one (Identifier).
    Here is my loop:
    private static void readLetters(int i) {
            for (int j=0;j<36;j++)
            {
     
                if (shitarray[i]==letters[j])
                {
                    String abc ="" +shitarray[i];
                    d=abc;
                    if (c != "Identifier ") {
                        c = "Identifier ";
                        System.out.print(c);
                        System.out.print(abc);
     
                    } else {
                        c = "Identifier ";
                        System.out.print(abc);
                    }
     
     
                } 
    }
     
                }
    Thx in advance for any help.
    Last edited by prettynew; November 4th, 2011 at 10:39 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: character problem

    Don't use == for testing String equality. Use the equals() method instead. == tests whether the references point to the same instance, .equals() checks whether they contain the same String value.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: character problem

    shitarray[] (sry for the name) and letters[] are both char arrays. Any thoughts on how to overcome my problem?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: character problem

    Take a look at the String API. It has several constructors that might be useful to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Illegal character
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2011, 11:59 AM
  2. Character Array
    By p_dibb in forum Collections and Generics
    Replies: 1
    Last Post: February 16th, 2011, 10:08 AM
  3. Help with Character arrays
    By shanklove in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 6th, 2010, 12:56 PM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM
  5. Vietnamese Character Problem in JSP Report. Chinese and Thai Char OK
    By mrvora in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: October 23rd, 2009, 02:35 AM