Results 1 to 3 of 3
  1. #1
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default Compare substring to char

    Hey.

    As the title says, I'm trying to make it possible to compare whether a substring (of one character) is a certain char.

    Something like this - note, this code isn't valid.
    Java Code:
    public static boolean isValidDate(String date) {
    	//TODO: 
    	if (isLength(date, 10)) {
    		if (checkIfInt(date.substring(0,2)) &&
    			[B]date.charAt(2).charValue() == 45[/B]) {
    			return true;
    		}
    	}
    	return false;
    }
    It's not at all finished and the isLength and checkIfInt are other methods I created.
    Just focus on the bold line.
    I want to be able to check if the char at index 2 is a '-' - and instead of trying to invent my own way through it I figured I'd ask here and see if anyone had suggestions to this relatively simple question.

    Oh by the way - The main issue is that charAt() doesn't return a char-value that I can call methods on.

    The compiler tells me that it "Cannot invoke charAt(int) on the primitive type char" - and that makes no sense to me since "date" is a String, not a char.

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Listen to the compiler and don't call a method on a primitive. Instead, what's stopping you from simply doing...

    Java Code:
    if (date.charAt(2) == '-') {
      //....
    Having said that, I wonder if you're trying to re-invent the wheel and whether using a SimpleDateFormat object would be easier and much more thorough and bullet proof than what you're doing.

  3. #3
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Listen to the compiler and don't call a method on a primitive. Instead, what's stopping you from simply doing...

    Java Code:
    if (date.charAt(2) == '-') {
      //....
    Haha, thanks, I can't believe I forgot that chars are also integer values themselves.

    Quote Originally Posted by Fubarable View Post
    I wonder if you're trying to re-invent the wheel and whether using a SimpleDateFormat object would be easier and much more thorough and bullet proof than what you're doing.
    I know - but it's all just for practice.
    I've considered implementing the Joda.org Time - since SimpleDateFormat has it's disadvantages as far as I heard.

    What I need to be able to do later on, is simply calculate the amount of days between two dates - and as far as I recall, I was advised against using SimpleDateFormat in that matter.
    I'll have a look on SimpleDateFormat when I get to implementing a date-calculator on my TODO-list.
    Last edited by Muskar; 11-21-2010 at 08:51 PM.

Similar Threads

  1. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  2. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  3. How to compare char
    By scottpush in forum New To Java
    Replies: 2
    Last Post: 02-27-2010, 11:51 PM
  4. Compare a char
    By Torgero in forum New To Java
    Replies: 3
    Last Post: 02-01-2009, 03:24 PM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 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
  •