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

Thread: beginner question--boolean expressions

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default beginner question--boolean expressions

    Okay, say that I am using an if-else statement and i want to basically say that
    if (string.contains("#")
    then do this...
    else if (string.contains ("#") = false)
    then do this...
    else
    then do this...

    so, my question is about the second else if statement. I know that I can't say what I typed up there but I need to figure out a way to write if string doesn't contain a certain substring then do something else... Any suggestions?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: beginner question--boolean expressions

    Any reason a simple else clause won't work? As anything which doesn't evaulate to true when performing String.contains("#") will enter the else clause.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: beginner question--boolean expressions

    You do not want the expression to equal true, so what's the opposite of true? A lot of these basics can be found at the following link:
    Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language)

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: beginner question--boolean expressions

    I tried both of these already and returned
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1931)
    at tweetToLeet.main(tweetToLeet.java:26)

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: beginner question--boolean expressions

    And what code did you use exactly to achieve such an Exception?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: beginner question--boolean expressions

    I figured it out... thanks for all the advise though... had nothing to do with my boolean expressions.... had more to do with placement of {'s.... stupid mistake haha :/

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: beginner question--boolean expressions

    Glad you solved it ninjaBob.
    If you could now mark this thread as solved, I would be grateful.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  2. Beginner question on where to put an array
    By Diplo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 3rd, 2011, 03:56 PM
  3. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 12:56 PM
  4. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM
  5. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM