Monday 20 November 2017

java.util.scanner - Java Scanner Troubles: how to skip to the next line?

itemprop="text">

I'm making a text-based, menu driven
program which uses the scanner class to take in both ints and strings. The ints
correspond to the menu options, while the strings are used to take in
user-input.




private
static Scanner userInput = new Scanner(System.in);

public static
void main(String[] args)
{
//Will be used to initiate the
while-loop
int start = 1;

while(start == 1)

{

System.out.print(Messages.printMenu());
**int choice
= new Integer(userInput.nextLine());**

switch(choice)

{
case 1:


System.out.println(Messages.askForAuthor());
String author =
userInput.nextLine();



System.out.println(Messages.askForRecepName());
String recepName =
userInput.nextLine();


System.out.println(Messages.askForEmailAdd());
String recepEmail =
userInput.nextLine();


System.out.println(Messages.askForSubject());
String subject =
userInput.nextLine();


System.out.println(Messages.askForTextBody());

String textBody =
"";
***while(!userInput.hasNext("end") &&
!userInput.hasNext("END"))***
{
textBody += userInput.nextLine() +
"\n";
}

System.out.println(author);

System.out.println(recepName);
System.out.println(recepEmail);

System.out.println(subject);


System.out.println(textBody);

break;


The parts
surrounded by "**" is where the problem occurs. The program runs just fine during the
first run, but when it goes into the while loop again for the second time, it causes a
type mismatch error because "end"/"END" is still in the Scanner's stack(I'm guessing
it's a stack) and choice looks for an
int.



Here's the
output:



Document Storage System
Menu
============================

1 - Create and store an
e-mail
2 - Create and store a memo
3 - Create and store a
report
4 - Display a document
5 - List all active
documents
6 - List all archived documents
7 - Locate documents
containing a specific word or phrase
8 - Archive a document
9 -
Retrieve a document from the archive
10 - Clear the
archive

99 - Quit

Enter your choice:
1
Please enter author:
Agent Smith
Please enter the
recipient's name:
Neo
Please enter the recipient's e-mail address:

Neo@zion.net
Please enter the subject:


Notification for Eviction!
Please enter Enter text body
(type END on separate line to stop):
All your base are belong to
us
end
Agent
Smith
Neo
Neo@zion.net
Notification for
Eviction!
All your base are belong to
us


Document Storage System
Menu
============================
1 - Create and store an
e-mail
2 - Create and store a memo
3 - Create and store a
report
4 - Display a document
5 - List all active
documents
6 - List all archived documents
7 - Locate documents
containing a specific word or phrase
8 - Archive a
document

9 - Retrieve a document from the archive
10 -
Clear the archive
99 - Quit

Enter your choice: Exception
in thread "main" java.lang.NumberFormatException: For input string: "end"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:449)
at
java.lang.Integer.(Integer.java:660)
at
proj4.Project4.main(Project4.java:20)***



Answer




You should be able to do
this:



while(!userInput.hasNext("end")
&& !userInput.hasNext("END"))
{
textBody +=
userInput.nextLine() +
"\n";
}
userInput.nextLine();



Since
at this point you know that either "end" or "END" is still waiting in the
Scanner, you can just read the next line and do nothing with
it. There are still issues about failing more gracefully if the wrong input is given,
but this should solve the given problem.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...