Java: Pass as many strings as you want through a method

When working on a text based role playing game I wanted a simple way to check for user input. An example of this is as follows:

You enter a small clearing, there is a path north, south and north-west. In the tree line you can also see something shimmering.

user: go north-west

In this case the user wants to go north-west but I would also accept other commands to go in that direction such as: “walk north-west”, “northwest”, “north west”, “go north west”, “go north-west.” The easiest way to properly do this is to pass multiple strings through a method. Naturally you can only have as many strings as declared or you would have to use the alternative of passing an array.

The code to solve this problem goes as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public boolean check(String... x){
//normally when passing a string it is required to do the following:
// String x, but with arguments we can pass it something like this:
//check("go north","head north","travel north","north")
 
for(String words : x){
//creates a loop declaring the variable words and loading it with
//the value of x
 
if(words.equalsIgnoreCase(input)){
//this checks to see if the word is equal to the users input.
 
return true;
// if so then return true
 
}
 
}
 
return false;
//after it loops through all of the possibilities it will return false.
 
}

In order to use this in the way that I have you must know that the variable “input” is loaded from the keyboard. As a matter of fact this method is placed inside of a class called “Input.” This class has a static variable called string, a way to prompt for a string, number, etc and checks the input for errors. An example of this class in action would be something like this:

1
2
3
4
5
6
7
8
inp1.promptInput();
if(inp1.check("attack","fight","1")){
attack();
}else if(inp1.check("flee","run","2")){
run();
}else{
System.out.println("Please enter a valid command.");
}

The code first asks the user to prompt one of the menu items. calls a method if check returns true or displays an error if there is nothing there.

Going through website creation part 2: Backend

This step mainly involves breaking the site up into sections of PHP. This will allow us to use various parts and pieces to make the site dynamic.

We may want to create a database and for this article I will be creating a very simple design that is just going to be used to display the information on the page. We will do this by adding a simple connect.php page that will contain the connection information.

1
2
3
4
5
6
7
8
<?php
 
$con = mysql_connect('database.website.com','username','password');
if (!$con) {
die('Could not connect: ' . mysql_error());
} mysql_select_db('databaseName');
 
?>

Now that we have successfully created our connect.php file we can move on to create a small piece of code we will include in the body of the page we created in the first tutorial. In this file we will be selecting information from our database.

1
2
3
4
5
6
7
8
9
10
11
<?php
 
include('connect.php');
$loader = mysql_query("SELECT * FROM table ORDER BY title") or die(mysql_error());
while($row = mysql_fetch_array($loader)) {
 
echo .$row[title].'<br>'.$row['description'].'<br>'.$row['link'].'<br>'.$row['quality'];
 
}
 
?>

Now we have successfully pulled information from our database, created a connection to the database and loaded the connection dynamically. Hopefully this will have helped you in your journey to create a dynamic website.

Ways to make you work more effectively from home

Working from home as a freelancer can have its ups and downs. A lot of people find that it is hard to keep working steadily when there are so many things that can be a distraction whether that is Facebook, Twitter, Youtube or anything else you spend time on doing when you should be working. There are also other things that can be done to increase efficiency such as adjust your work environment.

In this article I will be showing you ways to increase work flow when working from home that will make you more efficient with your job.

Cutting off the outside world

The first step to making yourself more efficient is to cutting yourself off from the outside world. By not taking personal calls, and emails you will change the way you think about what you are doing and thus making you more efficient in the process.

Telling your family to not interrupt you while working is also something that is very necessary and will help a large amount. This will change how you think about work and will reduce the likeliness of being distracted.

The final step of cutting yourself off involves not using “Youtube”, ”Twitter” or “Facebook” when working. These will just pull your brain away from the tasks that it should be working on and are generally a sheer distraction.

Working in a clean environment

Working in a clean environment can change everything. If your desk is clutter free it will allow you to focus on your work with less worries. Even though you may not notice this, your mind may become a little more depressed when working in a cluttered area.

Comfort in this environment is also very important whether that means changing the temperature or splurging for the chair with the added lumbar support this is a very important thing to have when working from home.

Lighting, in my opinion, is the most important thing in your place of work. It will change your mood and perception you have while looking out on the world. Natural lighting is the best thing you can have. It will make you more optimistic and comfortable when working inside.

Setting up your computer properly

According to the division of health and safety there are some procedures to follow that will allow you to properly sit at a computer. Some of these are to allow proper blood flow and others are for your eyes. I will only be going over some of the more important things. The first and foremost thing required is a chair that will support your lower back. Next to that you are going to want to be at eye level with the very top of your monitor or looking down on it.

When working to be more efficient it is good practice to have your monitor, mouse and keyboard directly in front of you and not at an angle.

Making your work area away from your living area.

When working from home sometimes it can be hard to find the proper motivation to get things done. Making a small home office are even wearing shoes in the house when working will help you become that much more focused with your job and allow you to get more work done in a days time.

There are some tricks that will make your mind think that you are at work. These little tricks consist of dressing as though you are ready to go and do business, wearing shoes and setting a fixed work schedule that involves being in the home office at a certain time and leaving at a certain time.

Learning HTML in one short night

Introduction

Open up your text editor because it is time to learn HTML in one night. If you are unfamiliar with HTML you should know that it is first and foremost a scripting language. Your server is going to send this file to the person’s computer. This person’s web browser; be it “Chrome”, ”Safari”, ”Opera”, ”Firefox” or “Internet Explorer” is then going to read the information and display it on screen.

This is an in depth look at HTML and how it is structured. In a future article I will include CSS with some CSS3.

HTML stands for hyper text markup language and is written in a standard text editor. (notepad for Windows, text edit for Macintosh) We are going to go ahead and start the tutorial now.

Please make sure that you have a folder, web browser and text editor open on your screen.

Building our first page

Open up your text editor; type in the following code and then save the file as “index.html” Be sure to write the extension. If it saves as “index.html.txt” then open up the folder that the file resides in, write click it go to “properties” on Window or “get info” on a Mac and remove the “.txt” extension.

1
2
3
4
5
6
7
    <html>
    <head>
    </head>
 
    <body>
    </body>
    </html>

The code above doesn’t contain anything special which is why you don’t see anything. In order to add content we are going to add a little more code to demonstrate some content on the screen but first we need to understand what was just added.

The first part of the above code tells the browser that inside is the HTML, in fact everything is until it reaches the closing statement or the bottom of the page. The head tags as you can see “<head></head>” tell the browser where the head information is. This information usually contains the title of the page, some meta data, style tags or links to css files and a little javascrip.

The tags below this are the body tags. Inside of here is where we will be putting all of the page contents which you will see shortly.

The next step is to add a line of code to your website. It is going to be at the very top and will have the type of charset, some meta data and the doctype. For now you do not need to know what it means but know that it is good practice and allows your code to be read properly.

1
2
3
4
5
6
7
8
9
10
    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
    <head>
    </head>
 
    <body>
    </body>
    </html>

Now that we have the basic structure of the page setup we can go ahead and add a title. Like everything else it is set in tags and requires no formatting.

1
2
3
<head>
<title>Code With Design</title>
</head>

Note: The naming of the title is important. It will allow search engines and bots to easily find your content and increase your page rank. Make sure it covers what the page is about but don’t make it to long. Search engines don’t like that.

Testing your page

In order to test your page you just need to find the index.html file’s location on your hard disk and double click it. The page will open up in your default browser. If it does not you are going to need to associate a browser with that type of file. You can also open up your web browser and set the URL to the location of the file on your hard disk.

When it comes to testing to see if you have valid code it is as simple as using the W3C validation service offered here. You can test a page by using the URL, copying and pasting or uploading the file directly.

The body of the page

The next step in our quest for knowledge is getting text onto the screen. In order to accomplish this there are some ways to go about doing so. First there is just plain text which I would advise not doing do to styling complications later on. The next type of text is “paragraph” text. This text is displayed in <p></p> tags and is formatted to have spaces on the top of the first line and the bottom of the bottom line. The third main kind of text is heading tags, once again name these accordingly because it will help your pages chances of getting higher in the search engine.

1
2
3
4
5
6
<body>
<h1>This will be shown at an h1 level</h1>
<p>Here is som basic fill text that is only used to display what is needed.</p>
<h2>Difference in size</h2>
<p>As you can tell there is a difference in size between the heading 1 and heading 2 tags. This difference can be changed later on using page styles.</p>
</body>

The bit of code we are going to use is called a div. Div tags can be found throughout websites and are the main structure of formatting. Div tags can be of type class or id and are used with css to change how a website appears on screen. The following is the basic structure of how a div tag works.

1
2
3
4
<div class=”this_is_where_css_is_associated”>
<h1>Article title</h1>
<p>Content goes here.</p>
</div>

Note: The div tag can be formatted with CSS to change the location of this content on the screen along with colors, and size. Also, div tags are often associated with classes over id.

Menus and links

Menus in HTML are commonly created using unordered lists and list items. These tags like as follows <ul>unordered list</ul> and <li>List item</li>. When written these tags are nested meaning that the list item tags inside of the unordered list.

1
2
3
4
<ul>
<li>Menu item 1</li>
<li>Menu item 2</li>
</ul>

It isn’t going to be much of a menu if we cannot click the item and find ourselves on another page. In order to do this we are going to need to add the following code.

1
2
<a href=”http://codewithdesign.com/”>Code With Design</a>
<a href=”some-random-page.php”>Linking internally</a>
1
2
3
4
<ul>
<li><href=”http://codewithdesign.com”>codewithdesign.com</a></li>
<li><href=”http://lirkr.com”>Search engine</a></li>
</ul>

Initially the menu is going to look very bad but there is hope for it yet. The menu is styled this way as a default. Once you add css to the web page it will begin to look better.

Header rules are not as common as they once were do to the increase in proper CSS they were quickly replaced with borders on div tags. If you are looking for a heading rule then you can add the simple code below.

1
<hr></hr>

Note: There is not content inside of this header ruling for a reason. All that is required are the two tags and a line will be created.

Tables

Note: It is best practice to not use tables at all unless you are puling the information form a database. If you choose to make your website layout using tables note that it isn’t considered common practice, it will increase load times, they are messier to work with and all around a bad idea.

Tables are created using the <table></table> tags and consist of nested tags inside. The basic table will look something like this.

1
2
3
4
5
6
7
8
9
10
<table cellspacing=”5px” border=”1px”>
<tr>
<td>Row 1 col 1</td>
<td>Row 1 col 2</td>
</tr>
<tr>
<td>Row 2 col 1</td>
<td>Row 2 col 2</td>
</tr>
</table>

This table was made using cell spacing and a border just to let you see how it is all aligned. Like I have previously stated these are not a good idea unless displaying data and even then there are better ways then tables.

Forms

These are html components that can be used to send information to other pages on the site. These components range in abilities and are often used to pass a username and password to a site. The general way that it is written is as follows:

1
2
3
    <form action=”" method=”" enctype=”multipart/form-data” name=”form-name” id=”form-id”>
 
    </form>

Going through the top line it is obvious that we are going to need to add components within the form tags. The forms id and name should be changed to your choosing are commonly named the same. (ie: name=”form1″ id=”form1″) The action of the form is where you want the information to be posted. This usually means that it will be sent to PHP page.

Components for forms

Components are usually enclosed in label tags which means that you can add generic text that will be grouped with the item itself.

text area

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    <label>
    <input type=”text” name=”text” id=”text”>
    <label>
 
    <label>
    <input type=”textarea” name=”textarea” id=”textarea”>
    <label>
 
    <label>
    <input type=”checkbox” name=”chkbox” id=”chkbox”>
    <label>
 
    <label>
    <input type=”radio” name=”radio” id=”radio”>
    <label>

Using the above format you can create items such as:

file input field “file”
hidden field to pass data without a field appearing on screen “hidden”
A submit button used to post the form “submit”