Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

beginers question about taking input from console

807588Feb 9 2009 — edited Feb 9 2009
I want to keep taking input until the letter q is typed. If the letter is not q I pass it to an object from a class I created. This object will take input from the user before returning to the calling class (sorry for bad terminology)
Here's what I've tried:
               String in;
		while(true)
		{
			System.out.println("a:");
			System.out.println("b");
			System.out.println("q - quit");
			try{
				in = input.next();
			}catch(NoSuchElementException name)
			{
				//do nothing
			}
			if(in.equals("q"))
				System.exit(0);
			component.inputted(in);
		}
I get this after the first iteration:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at CheckerBoardComponents.inputed(CheckerBoardComponents.java:109)
at CheckerBoardViewer.main(CheckerBoardViewer.java:37)

I'd like to use scanner to take input, so please don't tell me that I should be using something else unless it will be really important
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 9 2009
Added on Feb 9 2009
26 comments
361 views