Results 1 to 6 of 6
  1. #1
    Chuklol is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Comparing a char to " ' "

    So my code takes a user input such as

    setq x '(yellow)

    The first part removes 'setq ', and initializes the program to find the variable name (which will be x).

    What i am working on now is to find the variable name so in this case 'x'.

    Java Code:
    public String var (String s){
        	if (s.length () == 0){
        		return "";
        	}else if (s.charAt(0) == ' '){
        		return "";
        	}else if (s.charAt (0) != ' '){
        		return s.charAt (0) + var (s.substring (1,s.length ()));    		
        	}else    		
        		return "";    	
        }
    So this returns the variables name so far.

    Recall the input
    setq x '(yellow)

    My program removes setq , now it is
    x '(yellow)
    Now i want it to remove x ' and keep (yellow).

    How can i do this using the same recursive technique as above?

    Confused since im not sure how i can compare a character to a ' when
    the actual compare statement in java uses 'a' to indicate when i want to compare to an a.

    Thanks

    Chuklol
    Last edited by Chuklol; 09-26-2011 at 02:10 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Comparing a char to " ' "

    how i can compare a character to a '
    Have you tried using the escape character: \ when you define the character literal?

    What is the (yellow)?

  3. #3
    Chuklol is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Comparing a char to " ' "

    thats just the next part of my code, it defines the objects in nodes in a linked list.

    for now i have changed it to when it finds a |, so if i put \' it should work?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Comparing a char to " ' "

    What have you tried? Try escaping ', or look up the ascii number and use that instead.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Comparing a char to " ' "

    The problem with using an integer value for a character is that it reduces readability. Not everyone knows the ASCII codes.

  6. #6
    Chuklol is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Comparing a char to " ' "

    That worked, thanks Norm, for anyone else with similar problems
    Java Code:
    public String var (String s){
        	if (s.length () == 0){
        		return "";
        	}else if (s.charAt(0) == ' '){
        		return "";
        	}else if (s.charAt (0) == '\''){
        		return "";
        	}else if (s.charAt (0) != ' '){
        		return s.charAt (0) + var (s.substring (1,s.length ()));    		
        	}else   		
        		return "";    	
        }

Similar Threads

  1. using a "char" sentinel value for "int" input
    By jh7468 in forum New To Java
    Replies: 8
    Last Post: 03-02-2011, 11:42 PM
  2. "boolean isDigit(char ch)" not working...please help
    By cc11rocks in forum New To Java
    Replies: 7
    Last Post: 02-06-2011, 01:40 AM
  3. Equivalent of "char *" in JAVA
    By ABHIJEEEEEEET in forum New To Java
    Replies: 3
    Last Post: 08-11-2010, 11:37 AM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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