Question:
What is a new line as a char value in Java?
Noobtaculus
2009-01-28 07:45:40 UTC
Like as a String it's "\n", "\r" or System.getProperty("line.separator") is a new line; but if I have a character value instead of a String, what should I be looking for?
Nine answers:
Icarus Rising
2009-01-28 09:54:01 UTC
Calanth is correct to tell you that there simply is no new line character. Depending on your operating system, "\n" is silently translated to {'\c','\r'}, {'\r','\c'}, {'\r'}, or {'\c'} , So, it's some combination of "carriage return" (usually 0x0D) and "line feed" (usually 0x0A). For us lucky stiffs that grew up with Microsoft, \n has always meant "\0x0D\0x0A", but it really did depend on the OS (Mac and Unix were different).



What I do to solve this issue if I must look a character at a time, then I will look for either 0x0D or 0x0A. And if you do find either, you must then look at the next character for the other and skip over it. (If 0x0D, then check for 0x)A, or vice versa). Two 0x0D in a row or two 0x0A in a row usually does mean two lines, one of them empty.



Yeah yeah... it blows. This is just a case where something invented 20 years ago is still biting us. And if you are really interested in who thought this crazy stuff up -- CR literally used to tell an electric typewriter to send its print head back to the far left of the current line. LF told the roller to scroll the paper one line. You needed this seperation because to underline a word, you would send a carriage return, then a bunch of spaces and underscore marks before you sent a line feed. To do a bold, you would send carriage return and retype the line over again on top of the original line before doing a line feed. Fun times.
janta
2016-12-14 09:41:45 UTC
Java Newline Character
pahulick
2016-09-28 16:29:10 UTC
Java New Line Character
anonymous
2009-01-28 08:18:51 UTC
Java is system-independent, newline characters aren't, so there's no "new line character" in Java. The runtime environment translates \n into what the particular computer needs. If you're looking in some non-Java data, you need x0d, x0a or x0d and x0a as a pair, depending on the operating system it came from (or anything else the OS uses - EBCDIC, for example, uses x0d as a carriage return, but x15 as a line feed).
anonymous
2015-08-12 08:18:50 UTC
This Site Might Help You.



RE:

What is a new line as a char value in Java?

Like as a String it's "\n", "\r" or System.getProperty("line.separator") is a new line; but if I have a character value instead of a String, what should I be looking for?
anonymous
2016-03-29 12:53:29 UTC
For the best answers, search on this site https://shorturl.im/Ur9Jm



Java initializes primitive types to 0, non-primitive types to null. That's part of the VM specification. But I think sometime along the line (maybe JDK 1.5) it became an official compiler error to not initialize a variable, and use it despite that.
anonymous
2016-03-17 11:13:45 UTC
Java only gives default values to variables that are fields. In your code, c is a local variable, not a field, so it has no default value.
Nick A
2009-01-28 11:40:37 UTC
if your reading form a file, the file object has the method nextLine() which reads the whole line including spaces and moves on to the next line.



for example

while(file1.hasNext())

{

System.out.print(file1.nextLine());

}
Fudge
2009-01-28 08:01:53 UTC
'\n'


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...