• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return Carriage Help

 
Ashley Perry
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me just say, this is a quiz question from a week ago. I would never ask for help or look for help on homework or assignments online. I am not doing too well in java class right now. It feels like when I finally understand somethng... i don't understand it all. I think I am going to end up failing the course, but I am trying very hard not to.

So this question was on a quiz:

Consider the following statement:
System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");
This statment will output _____ lines of text.

A) 1 B) 2 C) 3 D)4 E)5

I chose B, and I got it wrong. Why would it be 3 lines of text?

1 big bad wolf (tab) 8 the 3 little pigs
(new line) (return carriage) 2 night 4 dinner

My teacher said the correct answer is 3. However, when I execute the program I only get two lines. I don't understand. I thought the return carriage was supposed to return to the start of the same line? Why would it create three lines? What's creating the third line? I emailed my teacher, and the only thing she would say is it might be because of the OS I am using. But what does that have to do with it when there is not anything to start a third line? The return carriage is meant to go to the start of the same line, right?

Thank you for any help. I am sorry, but I just can't afford to keep doing badly. I need to understand everything we're learning. It's going to show up on a final exam.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Perry wrote:Let me just say, this is a quiz question from a week ago. I would never ask for help or look for help on homework or assignments online. I am not doing too well in java class right now. It feels like when I finally understand somethng... i don't understand it all. I think I am going to end up failing the course, but I am trying very hard not to.

So this question was on a quiz:

Consider the following statement:
System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");
This statment will output _____ lines of text.

A) 1 B) 2 C) 3 D)4 E)5

I chose B, and I got it wrong. Why would it be 3 lines of text?

1 big bad wolf (tab) 8 the 3 little pigs
(new line) (return carriage) 2 night 4 dinner

My teacher said the correct answer is 3. However, when I execute the program I only get two lines. I don't understand. I thought the return carriage was supposed to return to the start of the same line? Why would it create three lines? What's creating the third line? I emailed my teacher, and the only thing she would say is it might be because of the OS I am using. But what does that have to do with it when there is not anything to start a third line? The return carriage is meant to go to the start of the same line, right?

Thank you for any help. I am sorry, but I just can't afford to keep doing badly. I need to understand everything we're learning. It's going to show up on a final exam.


Well, when I run the program I only get 2 lines of output. There might be a reason that I am unaware of, but my question to your professor would be: If it is going to output differently on different OS's, shouldn't an OS be specified in the question?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the prof to tell you what OS he's run it on where it produces three lines; he will not be able to, as there is no such OS.
 
Pooja Prakash
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I ran the code through command prompt and the output had 2 lines as you say.

But....
When i ran the same code on Eclipse IDE i'm getting 3 lines of output.I'm enclosing a screenshot for clarity...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would do well to try it on different computers. I tried it on Linux and got two lines. Try an old Mac (before OS X) which used \r for line end.
 
Ashley Perry
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. So is the correct answer 2 or 3? I guess I am really just trying to understand the \r escape in case it's on another test. I know I might be getting on my professor nerves with my emails, but I am sliding on thin ice here. I need every point I can get. I love java, and it seems I am good at it when I am in front of the computer. The tests and everything have to be done without a computer. I am having trouble taking my coding from the computer to the paper. Any ideas to help with that, too?

 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer 2 lines is perfectly Correct,No questions there and it wont matter the kind of OS it is run on Cause JVM is expected to behave the same on all Operating Systems.Your Prof definitely used eclipse to run the program otherwise 2 lines is correct
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WinXP: command line:
A:\>edit ReturnDemo.java

A:\>javac ReturnDemo.java

A:\>java ReturnDemo
1 big bad wolf 8 the 3 little pigs
2nighter

A:\>
And it duly prints 3 lines
  • 1 big bad wolf 8 the 3 little pigs
  • 2nighter
  • A blank line
  • The blank line arises from the use of System.out.println(). I shall go back to Linux later and check for the blank line.

    That is a confusing piece of code; in recent (post-September 2004) practice, you ought to use the printf method and the %n tag, which finds the OS-specific line terminator (there is a list in the Pattern class documentation); in older code you would find the line terminator using the System.getProperties() method or similar.
    As far as I remember there are three commonly used combinations
  • Older Macs used the CR character which is obviously correct since it stands for Campbell Ritchie , and is (char)0x0d. It comes from the days of the old Teletype, which I remember using and it had a key marked CR which people told me meant carriage return. It took the head which types the writing to the left side of the paper.
  • When using a Teletype, I had to push CR, then LF which means line feed; LF took the paper up far enough to print a new line. DOS/Windows uses CR then LF as a line terminator. LF is (char)0x0a.
  • Unix, Linux and newer Macs use LF on its own as a line terminator.
  • Remembering what happened when I was writing code on paper: CR=go to the left, LF=move paper one line, I could use LF on its own and move the paper and start in the middle of the line, or CR and overtype the line previously written. You can see that happening here, where 2night (6 characters) obscures 4 dinner (8 characters) leaving only the "er" visible.

    The LF is often called "newline" since it took the print head to a new line, and is usually written in the languages with a C-based syntax (including Java) as \n and the CR character is usually written as \r. Beware: you can get strange problems if you try using \u000a or \u000d as escape sequences. If you write \u000a or \u000d in a String literal you get the code on a second line, and the compiler won't like that.

    People like using confusing code in quizzes, I am afraid.

    When I tried this code on Eclipse on Windows, I got the same 3 lines as Pooja Prakash, then it went to the next line. This demonstrates different behaviour of Eclipse.

    And Pooja Prakash's screenshot demonstrates that the correct answer was 4 lines
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic