This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi - I am a true programming greenhorn, three weeks into an introductory programming with Java course. I have finished my first homework assignment and am now trying to make it "beautiful" as my instructor likes to say. Currently I am trying to place an input cursor at the end of a text prompt, using the Eclipse IDE and displaying program output in the console.
My effort thus far:
1) Search through my text How to Program 8e by Dietel and Dietel
2) I tried the mighty oracle, Google, and spent some time reading about javax.swing.text.JTextComponent. I think this is only for GUIs, which I am not using.
3) Unsuccessfully searched this forum
Currently there is no cursor or prompt after the colon in either statement, and the users input is displayed two lines underneath the prompt. I am quessing I can fix the display of the users input two lines below by getting rid of the "\n\n". Is there any way I can fix the position of the cursor at the end of my text prompt?
Any help for this tenderfoot would be greatly appreciated. Thank you.
Don't use printf(" . . . \n", . . . ), but printf(" . . . %n", . . . ). That is because line end varies from computer to computer, and you get the correct line end with %n. More details here, here and here.
And why are you using floats? Use doubles.
Do you mean you want a particular character/s to appear at the beginning of the line? Like this
Please enter your input here
Why don't you add $> to the end of the String you are printing?
Thanks Vijitha! I realized that I was forcing the inputted value to appear two lines below because of the \n\n I had at the end of the System.out.printf statement.
Howdy Sheriff. I appreciate your guidance - after reviewing the links you sent and reading about the differences between primitive types float and double, I made the changes you suggested. I used \n because that it was what the textbook uses, but after reading the Oracle website I now understand your suggestion.
I tried the $> as follows:
but Eclipse gave me an error. I tried searching for the $> so I could see the syntax for its usage, but no joy
I would like the user to see:
Please enter the weight of the letter: _ (with the underscore being fixed and replaced by the users input)
Right now when I run this program in Eclipse and view the output in the console, the cursor is in the upper left hand corner.
If you have any suggestions on where to look, I would greatly appreciate it. Thanks again.
Joined: Oct 13, 2005
That is because you have the quote marks before the $>. $> is supposed to be part of the String, so the quote mark should follow it. And put a line break with a %n before the $>
As for a prompt which vanishes: no idea. You could try printing a \b character or even a \r character, but that may not work on all terminal windows. Note there aren't % tags to print \b or \r.
If you are doing that repeated, set the prompt as a final field and use the %s tag to add it to your printouts. The prompt you write would include any \b or \r if that trick ever works.