• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Input copied multiline into console

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic