>>Deborah Trytten: Let's talk about reading invisible characters. The first question you
might have is, "Well what exactly is an invisible character?" Well, let me tell you about that.
When you type at the keyboard, you hit enter to end one line and to start another. You've
probably done this millions of times without thinking about it at all. This really does
put a character in the computer that we have to deal with. Now the character is called
backslash n, and it stands for newline. And this character isn't visible other than indirectly.
So we can see that there's a newline that starts, what we can't see is there's actually
a little hidden character that causes the newline to start. Now this character is the source
of a lot of trouble in programming. There are two other characters that quite similar
to it. One is the tab, backslash t, and the other is the return, which is backslash r.
There are some very subtle differences between newlines and returns that have to do with
whether you're using a PC or a Macintosh. Luckily Java hides those from us so we don't have
to deal with them, but ordinarily in programming you do. When we go to read things in we use
a scanner object. And first we construct it. So we create a scanner to read in things from
the keyboard, which I usually call keyboard, and that equals new scanner of system.in.
System.in is the fancy computer name for the keyboard, and so this sets up a usual keyboard.
Now this scanner object will allow us to read in characters entered at the keyboard and
interpret them as different types. So for example we can read in Strings, int, and doubles.
Those are the ones we'll read in the most.
There are a couple of different methods that cause a lot of confusion. One is next and
the other is nextLine. Now the reason there's confusion here is because the methods behave
differently. So let's talk about exactly what they do. Next reads characters until it finds
a white space. Now when I say white space I mean a space, a tab, a newline, or a return.
These are all white space characters. The thing about next is it doesn't remove the
white space. So it uses it to separate things, but it doesn't actually take it out. nextLine
on the other hand reads characters until a newline is found. So it doesn't separate things
with spaces and tabs and returns, only with newlines. But here's the tricky part: nextLine
actually does get rid of the newline. So let's look at an example, let's read some data.
Suppose we have a line that starts out one of Robert Frost's most famous poems. Whose
woods and then there's an enter. Now remember you're not used to seeing that enter there
and it isn't going to show up when you enter things into your computer but it is there.
That is the newline character. So keyboard.next would read "whose." Now I'm being extremely
precise in how I show this, and you need to be precise in how you think about it. It does
not read in the space after whose, that's a white space character and that one is left.
This leaves space woods backslash n behind. The space is there.
Now, if we call keyboard.next again it will read in "woods." So it removes the space and
then it takes that next characters until the following white space character, which is
a newline character and then it leaved the newline behind. If instead of calling next
we had called nextLine it would've read in "whose woods" and it would've removed the
newline. So that's the difference between the two commands.
Now let's talk about integer and newlines. Suppose you want to read an age and a name.
So maybe 14 enter and then Maggie enter. So when this is in the computer it will be 14
backslash n, Maggie backslash n. So if we call keyboard.next int first it will read
in 14 because next int reads in integers, again using white space separators. So next
int works exactly the same way that next does. This leaves the newline behind. Now if we
read keyboard.nextLine, this removes the newline. If you use keyboard.next to read "Maggie,"
well, guess what? That leaves the newline behind again. So a better choice would've
been to use keyboard.nextLine. That would've given you "Maggie," but also remove the line
characters so it doesn't cause you trouble. Very subtle difference, but one that makes
a big difference when you're doing input. So here are the rules. When you're reading
data form the keyboard, you really need to think about where those newline characters
are all the time. So the best strategy that I know is to read in the newline characters
just as carefully as you do the data. If you think about where they are, you can just read
them in automatically with the nextLine and then they're not going to cause you bugs in
your program. Trust me - it's easier this way. So keep programming!