Computer Science, asked by HARMANSINGHSAINI, 1 year ago

Why in.nextLine() and in.next() is used in scanner class


Plz answer in points

Answers

Answered by Anonymous
2

next() is used to acceptance token as string from scanner object

When a line of text is given as input to the scanner object it will receive first token only

The rest of the token can be obtained by nextLine().

Example good so here if you use next() only "g" will come out....n of u use nectLine() "good" will come out

nextLine() is to include a complete sentence with spaces

Please mark brainliest


rahul200013: where should i upload
rahul200013: you mean brainly inbox
rahul200013: you can provide me your mail if ypu want
rahul200013: or ask some question
HARMANSINGHSAINI: No it’s not like that u may have your understanding way to do
Once again thanks to both of u for helping
HARMANSINGHSAINI: Asopachitrita as u mentioned after my comment that in.next() is also used to enter a string. It is not wrong but sometimes it shows error. So in.nextLine() is used to enter a word or a string whereas in.next() is used to enter only a word
rahul200013: many a times using nextLine() causes problem
rahul200013: especially when you use both of them together
Answered by rahul200013
1

in.nexLine() is used to accept the entire String sentence avialable to the scanner object, before a line break, and stored in the string variable.

in.next() however accepts only the next token....i.e. only the word avialable( the characters avialable to thr scanner object in before a delimiter(by default a whitespace and linebreak are delimiters for scanner class)occurs).

Consider this example:-

If you Enter: Hi I amRahul

using:str = in.nextLine(); the entire sentence is stored in str.

using:str1= in.next();only the first word (here Hi) enters into str1.

Therefore, Output:

str= Hi I am Rahul

str1= Hi

Similar questions