• Post Reply Bookmark Topic Watch Topic
  • New Topic

String reading an Enter Key?  RSS feed

 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just wondering, why doesn't a string type not takes an enter key..
i.e;
String var_name = " a string variable" ;

Here, if i want a new line between any two consecutive letters, that's not possible.. n any language ( i guess )..
why is it so??
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you saying this doesn't do what you want?

String var_name = "This is printed\non two lines" ;
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uhmm,
that would solve the purpose definitely, but i just wonder, why is the enter key itself not taken as part of an input string...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are talking about console input, because it is interpreted as a terminator, not part of the input.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:If you are talking about console input, because it is interpreted as a terminator, not part of the input.

Exactly. If the enter key was taken as part of the input, how would you tell the program you were done inputting?
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Bear Bibeault wrote:If you are talking about console input, because it is interpreted as a terminator, not part of the input.

Exactly. If the enter key was taken as part of the input, how would you tell the program you were done inputting?


well, we can tell the program that w had done inputting, the " " (quotes) mark the starting and ending of any string.
so that would be valid even when enter key is read as input by the string.. no?
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote:
well, we can tell the program that w had done inputting, the " " (quotes) mark the starting and ending of any string.
so that would be valid even when enter key is read as input by the string.. no?

So, how can you tell the program when you want to enter " " quotes ?
I guess there has to be a terminator, Enter Key is the chosen one because it is trivial in most cases?
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uhmm, well fine. so just because we needed a terminator, and we had to choose one, the enter key has been choosen..?!!
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote:uhmm, well fine. so just because we needed a terminator, and we had to choose one, the enter key has been choosen..?!!

Depends on how you read the String.
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
For Scanner, it is whitespace for next() and carriage return/line feed for nextLine().
And you have overloaded method for next() method for specified pattern or string.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!