This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Problem with Scanner class

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is very simple I just simply want to take n strings as input and print same.

INPUT CASE
N
STR1
STR2
.
.
STRN

IOUTPUT CASE
STR1
STR2
.
.
STRN




This loop is running for empty string also I am putting repl link please kindly check.I have no clue what is happening.

REPL link
 
Rancher
Posts: 2143
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's running for an empty string.  It's prompting to enter a string.  Just put a s.o.p before sc.nextLine statement and see what happens.

 
priyanshi bhardwaj
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:I don't think it's running for an empty string.  It's prompting to enter a string.  Just put a s.o.p before sc.nextLine statement and see what happens.



I got this as a output

 
Swastik Dey
Rancher
Posts: 2143
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enter a string
String str=sc.nextLine(); // Here the system is expecting some input, so you need to enter a string.  If this is not the case what is actual requirement.
 
priyanshi bhardwaj
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:Enter a string
String str=sc.nextLine(); // Here the system is expecting some input, so you need to enter a string.  If this is not the case what is actual requirement.



My Requirement is

INPUT CASE
N
STR1
STR2
.
.
STRN

What I am currently getting is
When I am entering N value after pressing enter


N
        <-- Here is a space
STR2
STR3
.
.
STRN -1


4
Enter a string
                      <-- I can't enter value here directly I am compelled to enter 2nd string rather than first string
Enter a string


 
priyanshi bhardwaj
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

4
Enter a string
                      <-- I can't enter value here directly I am compelled to enter 2nd string rather than first string
Enter a string



My requirement is

4//I want to enter this and system is allowing me to do this also.  
Enter a string //System prints it
 abcd //I want to enter this  which I can't do.    System leaves this space empty .
Enter a string . //System is giving me option . to enter this (2nd string not first string)



 
Swastik Dey
Rancher
Posts: 2143
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to consume the \n character after the first nextInt() call

 
priyanshi bhardwaj
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:You have to consume the \n character after the first nextInt() call



This worked like a magic  but can you explain why this problem has occurred.

And Can you please look into my another problem and let me know if you have any solution for this.

https://coderanch.com/t/718918/mobile/CoordinatorLayout-relative-layout

Thanks for Help Really appreciate
 
Swastik Dey
Rancher
Posts: 2143
15
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go through Scanner class documentation you will find the reason.  In brief the input ends with \n.  The nextLine function itself containrs a \n i.e  end of input.  However with nextInt this does not happen, that's why after that call the nextLine function is called to  consume the \n character and let the system know that line has ended.
 
priyanshi bhardwaj
Ranch Hand
Posts: 194
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:If you go through Scanner class documentation you will find the reason.  In brief the input ends with \n.  The nextLine function itself containrs a \n i.e  end of input.  However with nextInt this does not happen, that's why after that call the nextLine function is called to  consume the \n character and let the system know that line has ended.



Thanks
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!