Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Poetry Question Midterm 15/16

+11 votes
In the poetry Question from midterm 2015/16 i was writing my solution when I had a peculiar problem. I wrote a for loop to gather the information with a print line followed by a console.nextLine. The code seems to be working fine except it is not letting me input my first line. I know it is getting run because it asks for the first line, it just won't let me put it in. Any ideas on a possible cause?

example: console displays Enter line 1 /n Enter line 2 and then allows me to enter for lines 2,3,4 and 5.
asked Oct 11, 2016 in CSC 211 (F16) by Jack Cannell (437 points)

1 Answer

+5 votes
 
Best answer
I had the same problem the first time.

However, just like in the owlRacing project, you just need to put the statement "console.nextLine();" right before your for loop, then it should work fine.

Because when you use System.out.print to get the haiku thing, you need to move the cursor to the next line in order to get your first line String.
answered Oct 11, 2016 by Son Nguyen (100 points)
selected Oct 11, 2016 by Jack Cannell
Alternatively, you could use console.nextLine() (instead of console.next()) to read in the poetry type (haiku or limerick) from the user.  If you use ALL nextLine() or ALL next()/nextInt(), then you don't run into trouble.  The trouble tends to arise if you mix the use of next() and nextLine() in your program...
P.S.  It doesn't matter whether you use System.out.print or System.out.println for this.  The issue is where the Scanner object's input-cursor is.  The printed output makes no difference to the Scanner actually...
...