Results 1 to 8 of 8
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default using if statement

    i am having errors with using this line of code:

    Java Code:
    if(c.equals("\")) {
    i am guessing that the "\" is throwing it off?

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

    Default Re: using if statement

    Yes, you have to escape the \. Try:

    Java Code:
    if(c.equals("\\")) {

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: using if statement

    why does this return both values, when it should really end when it hits the first one?:

    String test = "myName\nhithere\n";

    String newSentence = "";

    for(int i =0; i<test.length(); i++) {
    char c = test.charAt(i);

    if(c!='\\') {
    newSentence = newSentence + test.charAt(i);
    } else {
    i=test.length();
    }

    }

    System.out.println(newSentence);

  4. #4
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: using if statement

    I think here you are not comparing \ , but you are comparing \n
    So instead of writing this '\\' in if-statement write '\n' and then compare.

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

    Default Re: using if statement

    why does this return both values, when it should really end when it hits the first one
    please show what it does return.
    What are "both values"?

    The String you posted does not have the character: "\" in it. The compiler converts "\n" to a single line end character. Print the String: test and you will see.

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: using if statement

    i get:
    myName
    hiThere

    and it wouldn't read it as a new line, would it, if it read each character at a time?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: using if statement

    it wouldn't read it as a new line, would it, if it read each character at a time?
    The new line character would be there if you read the String character by character. '\n' is a character.

  8. #8
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: using if statement

    oh, ok! that's good to know... thanks!

Similar Threads

  1. If Statement in SQL
    By Steffi1013 in forum JDBC
    Replies: 6
    Last Post: 04-10-2010, 03:19 PM
  2. need help in sql statement
    By chyeeqi in forum JDBC
    Replies: 6
    Last Post: 03-28-2010, 07:49 PM
  3. add an If Else statement and......uh????
    By sonny in forum New To Java
    Replies: 6
    Last Post: 03-04-2010, 06:57 PM
  4. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 02:58 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •