Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
rileylolz
Apr 27, 2008
For class, we've been assigned to make an 8x8 board for various pieces to move around on. I've run into a problem with user input, specifically dealing with how to handle tokens. I'm using Scanner to break up the input, but the problem is the largest command will have 5 tokens and the shortest will only have one. The way it's set up now is that it needs a command that will use all 5 tokens or else it crashes.

code:
		while (sc.hasNext())
		{
			cmd = sc.next();
			i = sc.nextInt();
			j = sc.nextInt();
			token4 = sc.next();
			token5 = sc.next();
			break;
		}
How can I break this up so it checks to see if there are any more tokens in the string before crashing? I tried if(sc.hasNext()) after each one, but it didn't work.

I'm still fairly new to java, any help would be appreciated.

And just an example of what the user input would look like:
"create 4 3 fast flexible" with fast and flexible being different types of pieces. It works fine with a command like that, but crashes with a command like "print"

Adbot
ADBOT LOVES YOU

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply