*
The moose likes Beginning Java and the fly likes Problem with new line character !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with new line character !!!" Watch "Problem with new line character !!!" New topic
Author

Problem with new line character !!!

learning Freak
Greenhorn

Joined: Jan 20, 2010
Posts: 5
I have provided a code something like this

string s ="\n";

if(s.startswith("\n")){
s.o.p("new line character.");
}else{
s.o.p("Not a new line character");
}

the problem I encountered is it is always displaying "Not a new line character".
where as I m expecting "new Line Character".

What is happening exactly? can anyone explain me briefly ??

Advance thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. You can change your display name here. Thanks!

Now, as to your question: if I corrected the capitalization and wrote out "System.out.println()", it should behave exactly as you expect it to. Therefore your real code (which obviously is at least a little different from this) behaves differently because of those differences. If UseRealCode in your post, we could tell you exactly what's up with it -- so let's see!


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Ernest meant PostRealCode. For one, your code contains two capitalization errors. Secondly, if I fix those two and change the "s.o.p" to full System.out.println statements, it prints "new line character." for me each time I run the code.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
learning Freak
Greenhorn

Joined: Jan 20, 2010
Posts: 5
@ Ernest & Rob .. Thanks for the reply.

In my real code i was using the System.out.println("") ; instead of s.o.p !! Jzt used them for the purpose of post.

The problem with the execution is some times it is getting inserted with a with a character "\r" before my String .

i mean : while debugging it is showing the value in the String var s as "\r\n".

and i m getting the wrong output.

Why the Escape Return character is getting appended ? what is the reason ?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Your last post simply makes things even more confusing. If you don't post your real code how on earth are we supposed to understand your problem? You may have problems caused by your operating system; older Macs used "\r" for line end, newer Macs and Unix/Linux use "\n" and DOS/Windows uses "\r\n".

You appear to be ignoring Ernest's earlier warning about names. And it's not "jzt"; please look at this.
 
 
subject: Problem with new line character !!!
 
Similar Threads
Storing "new line character " in a string
Problems with newlines in text files
backslash n is being escaped against my will
Need Printwriter help!!!!
StringTokenizer