<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code With Design &#187; input</title>
	<atom:link href="http://codewithdesign.com/tag/input/feed/" rel="self" type="application/rss+xml" />
	<link>http://codewithdesign.com</link>
	<description>web application development blog by Caleb Jonasson</description>
	<lastBuildDate>Fri, 11 May 2012 12:28:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Creating user input with menus in java</title>
		<link>http://codewithdesign.com/2010/01/15/creating-user-input-with-menus-in-java/</link>
		<comments>http://codewithdesign.com/2010/01/15/creating-user-input-with-menus-in-java/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 03:40:45 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[scanner]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=245</guid>
		<description><![CDATA[TweetWhen working with console command lines in java I found myself needing a solution that works when in certain situations. The console was a java based game and the need was user input because the game was being built in a text style format. In this article I will be using &#8216;equalsIgnoreCase();&#8217; to match a [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/01/17/create-a-press-enter-to-continue-with-java/' rel='bookmark' title='Create a press enter to continue with java'>Create a press enter to continue with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/28/java-pass-as-many-strings-as-you-want-through-a-method/' rel='bookmark' title='Java: Pass as many strings as you want through a method'>Java: Pass as many strings as you want through a method</a></li>
<li><a href='http://codewithdesign.com/2010/05/22/creating-multiple-sidebar-menus-in-wordpress/' rel='bookmark' title='Creating multiple sidebar menus in wordpress'>Creating multiple sidebar menus in wordpress</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Creating user input with menus in java" data-via="" data-url="http://codewithdesign.com/2010/01/15/creating-user-input-with-menus-in-java/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2010/01/15/creating-user-input-with-menus-in-java/"></g:plusone></div><p>When working with console command lines in java I found myself needing a solution that works when in certain situations. The console was a java based game and the need was user input because the game was being built in a text style format.</p>
<p>In this article I will be using &#8216;equalsIgnoreCase();&#8217; to match a string with what I would like it to be. I will also be using the &#8216;Scanner&#8217; class, keyboard inputs, and String objects. Here is the code that I will be working with&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Scanner</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> userInputWithMenus <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
Scanner keyboard <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1. Menu.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2. Item.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3. Close.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">String</span> inp <span style="color: #339933;">=</span> keyboard.<span style="color: #006633;">nextLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;main&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//execute what happens on main.</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//execute what happens on item</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;exit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//execute what happens on close</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The first thing we do is import a class that is built into java. Here we can access the scanner and assign a variable to it. Once we have our main class and main method setup we will display our menu and request the next line of text from the user and load this into the string &#8216;inp.&#8217; The next block of code is the If else statements normally you would execute another method or run the program from this point on however that was not what I intended on covering here.</p>
<p>Inside of the if else blocks..</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I placed code that matched the string contents with what I needed it to; in this case the menu items. I didn&#8217;t want the user to be forced into typing menu, item or exit so I made an or statement using &#8216;||&#8217; to introduce the option of matching the string with the numbers. This is all done with a line that looks as follows.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;main&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> inp.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can just use &#8216;imp.equals();&#8217; however I in creating this some copying and pasting was done. To get your menu to work even better you can try putting it into a loop and breaking the loop when the user enters a valid command. This can be easily done with a boolean and a while loop.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=245&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/01/17/create-a-press-enter-to-continue-with-java/' rel='bookmark' title='Create a press enter to continue with java'>Create a press enter to continue with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/28/java-pass-as-many-strings-as-you-want-through-a-method/' rel='bookmark' title='Java: Pass as many strings as you want through a method'>Java: Pass as many strings as you want through a method</a></li>
<li><a href='http://codewithdesign.com/2010/05/22/creating-multiple-sidebar-menus-in-wordpress/' rel='bookmark' title='Creating multiple sidebar menus in wordpress'>Creating multiple sidebar menus in wordpress</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/01/15/creating-user-input-with-menus-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

