Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Input copied multiline into console  RSS feed

 
Abdullah Attia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When input copied multi line into console, The scanner reads the first line only and the rest of lines passed to the next input statements
As example if i put this

this is my code



when prompted to enter the first input i enter this text

This is A Good Test
Not A bad Test
If you Are Good , it will be very very good

it's appears like that : This is A Good TestNot A bad TestIf you Are Good , it will be very very good
when i press enter the line "This is A Good Test" only passed to textToChange variable

i want the whole text to be assigned to the first variable
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is how will getInput() distinguish between pressing <enter> as the next line and pressing <enter> meaning that you're finished.
 
Abdullah Attia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nextLine() will always return when it sees a line separator, and that is what pressing the <enter> key sends.

You might try looping while you get each line, then when the user enters a blank line (just pressing the <enter> key), you concatenate all the previous line together.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you copy and paste the lines, or keep typing at the terminal without pushing the enter key?
java LineLengthDemo
This is a short line.
This line will go over the edge of the screen, providing I keep typing until I am over 80 characters.
When input copied multi line into console, The scanner reads the first line only and the rest of lines passed to the next input statements As example if i put this this is my code [code=java]String textToChange = getInput(sc, "Enter the text you want to change : "); String newText = getInput(sc, "Enter the new Text: ");[/code] when prompted to enter the first input i enter this text This is A Good Test Not A bad Test If you Are Good , it will be very very good it's appears like that : This is A Good TestNot A bad TestIf you Are Good , it will be very very good when i press enter the line "This is A Good Test" only passed to textToChange variable i want the whole text to be assigned to the first variable
You have 3 lines
[This is a short line., This line will go over the edge of the screen, providing I keep typing until I am over 80 characters., When input copied multi line into console, The scanner reads the first line only and the rest of lines passed to the next input statements As example if i put this this is my code [code=java]String textToChange = getInput(sc, "Enter the text you want to change : "); String newText = getInput(sc, "Enter the new Text: ");[/code] when prompted to enter the first input i enter this text This is A Good Test Not A bad Test If you Are Good , it will be very very good it's appears like that : This is A Good TestNot A bad TestIf you Are Good , it will be very very good when i press enter the line "This is A Good Test" only passed to textToChange variable i want the whole text to be assigned to the first variable]
You will recognise the 3rd line as your previous post all run together with line ends removed. Note I am getting 3 lines because I had three line ends and pushed ctrl‑D just before the line marked in green.

That is on bash on a Linux box. You may get completely different results on a Windows command line.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!