• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is the entire text not getting printed  RSS feed

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
When i create a new note and i enter the String (in.nextLine()) say  "  Red RidingHood"  and when i View it , output is only RidingHood. Why is the entire text "Red RidingHood" not getting printed?

_---------------------------------------------------------



-----------------------------------------------------------------------------------

CLASS NOTE -



______---------------------------------------------------------------------------------------


OUTPUT :

Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
strybook
Enter the description
red ridinghood
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit
2
Id : 1
Title :strybook
Description : ridinghood
Write to the Console
1. Add Note
2. To View Note
3. Exit



 
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please surround your code with code tags. Highlight your code and then click on the "Code" button above the edit window. I've fixed it for you this time.
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the input being read?  Are there any other reads of the data to "eat" part of what the user enters?
 
Sucheta Shrivastava
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Norm Radder
I didn't get you. The input is being stored in a file that is serialized and output is  deserialized .

Why is the output eaten i dn't understnd.
 
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code: can be confusing.  If you lookup the documentation on Scanner, you will see the next() reads the next token and that a token defaults to whitespace.  So if you enter "Red Ridinghood" as your title, the next() will take only "Red" because of the space -- but "Ridinghood" is still in the buffer, so the second next() (for the description) reads this and that's what you are getting.

You may need to use nextLine() which will read the entire line up to the End of Line separator, but this can cause other problems.  You may have to change the default token separator to an End of Line, but I haven't tried this.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of the statement on line 34?  What is done with the data it reads?
 
Knute Snortum
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it looks like changing the token separator (which is really called the delimiter) won't do it.  You will need to change any next() to nextLine() and deal with the problem that brings.  Here's what will happen:

nextInt() will read one token, the next integer it finds.  It will read up to, but not including the line separator, which is in the buffer because you have to press the <enter> key.  Now say you enter 1 to add a note.  Now there is a line separator in the buffer.  Next, you have to use nextLine() to read more than one word for the title, but it will see the line separator in the buffer and think you've pressed <enter> and skip the input.

To get around this, you have to call an extra nextLine() and ignore the input of the first one.  This relies on knowing that you can never get to "Enter a Title" without first getting a menu option.

There are other problems with your code, but we can get to these later.
 
Sucheta Shrivastava
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Norm Radder

If i don't put line no. 34 . i get this output
Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
story
Enter the description
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit
 
Knute Snortum
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my post (above) for why this is.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save the value returned by line 34 in a variable and print it to see what is being read and ignored.
 
Sucheta Shrivastava
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Knute Snortum , I haven't really understood what to do in the code . I changed everything to nextLine() still it doesn't display the output.

@Norm Rancher

When i save the next value i dont get any output

Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
story
Enter the description

red ridinghood
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is there a blank line in the posted console output after: Enter the description ?

Enter the title
story
Enter the description

red ridinghood 


When i save the next value i dont get any output

Where did you print the value that was read on line 34?  Use a print statement like this (assuming value was read into theVar):
 
Sucheta Shrivastava
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
case ADD_NOTE: {

System.out.println("Enter the title");
String title = in.next();
note.setTitle(title);
System.out.println("Enter the description");
String theVar = in.next();  // Save returned value
  System.out.println("theVar="+theVar +"<");
String description = in.nextLine();
System.out.println("description" + description);
note.setDescription(description);
notes.add(note);
service1.noteSerialize(notes);
System.out.println("--------------------");
break;

}


output is :
Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
stories
Enter the description
red ridinghood
theVar=red<
description ridinghood
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit



AND WHEN I WRITE THIS -

case ADD_NOTE: {

System.out.println("Enter the title");
String title = in.next();
note.setTitle(title);
System.out.println("Enter the description");

String description = in.nextLine();
System.out.println("description" + description);
note.setDescription(description);
notes.add(note);
service1.noteSerialize(notes);
System.out.println("--------------------");
break;

}
OUTPUT IS -
Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
stories
Enter the description
description
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isounds like the classic problem of the Scanner class: Mixing the use of the nextLine method with other next... methods is a problem.  The next... methods leave the lineend character from the Enter in the Scanner's buffer.  The next call to nextLine will read that lineend char and return an empty String.

One solution is to call nextLine after every call to a next... method to clear the buffer.
 
Carey Brown
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Knute explained this fairly well. Calling nextInt() leaves a new-line in the scanner's buffer that you need to purge. This is a major gotcha in the Scanner class.

Also, when you post code to go along with your questions, it really helps to post a small compilable piece of code that demonstrates the issue. Something along these lines.
 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace line 22 with



and then use these lines




 
Knute Snortum
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like this!  The only change I would make is I have noticed, though, that the first, and only the first input, you can't just press <enter>.  It doesn't respond the first time.

I'll throw together some code to illustrate this...
 
Knute Snortum
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you execute this code and enter anything text at the prompts and press <enter>, the program behaves as you would expect.  But if you only press <enter> without typing in an text, the first input takes two <enter>s.  The second one does not, though, so you have inconsistant behaviors.  I don't know a way around this yet -- other than setting a flag.
 
Sucheta Shrivastava
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working fine with this code -

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the title always only a single word?  The next() method reads one token/word.

What is the code on line 7 supposed to read?  Two calls to nextLine one after the other will require the user to press Enter two times.
 
Sucheta Shrivastava
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Norm Radder

in place of next() i could use nextLine() - this is by choice that i am using in.next().

And about using two in.nextLine() at line 6 and 7 , s that it doesn't eat the description and the entire description is taken and printed. I figued it out from the earlier posts.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!