• Post Reply Bookmark Topic Watch Topic
  • New Topic

nextLine  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Matched expression found by findInLine: 34

ef56


\r\n is new line? right?

The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end.


then 56 is in new line and it must return ef.What is the problem?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
then 56 is in new line and it must return ef. What is the problem?


It works fine for me*. I got "ef" when I ran the code.

Henry

* after fixing the variable name being passed to the scanner
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:\r\n is new line? right?

No. A newline character is specifically '\n'. A "new line" (or line-break) sequence will depend what operating system you're on: for Windows, it's "\r\n"; for practically every other system in the known universe, it's "\n". For most classes, Java will accept either, but not exactly sure how Scanner determines when a "new line" is encountered.

And unlike Henry, I get a compiler error when I run your snippet, because 'stream1' is not defined. However, once that's corrected I, like him, get:

Matched expression found by findInLine: 34

ef


Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\r\n is new line? right?

On Windows, yes. Other OS have other conventions - on Unix-based ones it's "\n".
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . for Windows, it's "\r\n"; for practically every other system in the known universe, it's "\n". . . .
Which is why you are advised to use println or printf("...%n",...) rather than \n.

A long time ago, I tried some timesharing and there I had to push CR = Campbell Ritchie Carriage Return to move the print head to the left of the paper and then LF = Line Feed to move the paper about ⅙″ up.
CR is equal to \r and LF is \n
About the same time my namesake and Kernighan were developing the C language for the Unix system which used only LF as its line separator. A few years later a group of Steves (Jobs and Wozniak) developed the Apple OS which uses CR alone (=\r) as its line end. Or more precisely used to. Macs nowadays use the same line end as Unix.

Look at the Pattern class and use ctrl-F line terminator, and you find there are about six combinations recognised as line ends. And if you go to this Wikipedia page, you find even more weird and wonderful combinations. Don't you wish you had never read this post
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . the paper . . .
Yes, the program was on paper. It was recorded on tape about 1″ wide with seven rows of holes (and one row of smaller holes).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, the program was on paper. It was recorded on tape about 1″ wide with seven rows of holes (and one row of smaller holes).

And if you can still find any, it makes great Christmas decorations. I think I still know how to make a "star"...

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!