Results 1 to 8 of 8
Thread: using if statement
- 09-24-2011, 06:50 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
-
Re: using if statement
Yes, you have to escape the \. Try:
Java Code:if(c.equals("\\")) {
- 09-24-2011, 07:18 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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);
- 09-24-2011, 07:28 PM #4
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
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.
- 09-24-2011, 07:38 PM #5
Re: using if statement
please show what it does return.why does this return both values, when it should really end when it hits the first one
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.
- 09-25-2011, 04:25 AM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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?
- 09-25-2011, 03:14 PM #7
Re: using if statement
The new line character would be there if you read the String character by character. '\n' is a character.it wouldn't read it as a new line, would it, if it read each character at a time?
- 09-25-2011, 04:20 PM #8
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Similar Threads
-
If Statement in SQL
By Steffi1013 in forum JDBCReplies: 6Last Post: 04-10-2010, 03:19 PM -
need help in sql statement
By chyeeqi in forum JDBCReplies: 6Last Post: 03-28-2010, 07:49 PM -
add an If Else statement and......uh????
By sonny in forum New To JavaReplies: 6Last Post: 03-04-2010, 06:57 PM -
If statement, please help??
By soc86 in forum New To JavaReplies: 5Last Post: 11-23-2008, 02:58 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks