In this Instructable, you will be creating a computer program written in the Java programming language. This Instructable will offer information on how to code in Java and will offer insight as to what strings and arrays are in Java. This program will demonstrate how to create a program that reads what you type into it as a string. After giving the program your input, it will ask for a number and tell you the character at that value in terms of an array.

To clarify more on the Java programming language; an array is basically a container that can hold a certain number of data. An array can only hold primitive data types, such as an integer or a single character. An integer is exactly what an integer is in mathematics, a positive or negative number, including 0. A character in Java is any single letter or symbol on the keyboard, such as an exclamation point ‘!’. From this explanation, a string in Java is an array of characters. So, a line of text could be considered a string. For example, “This is a string” would be a string in quotations, and would be an array of characters. To move through an array and access its data, we would select where in the array we want to access based on a numbering system. An array stores its values starting at 0 and counting up to the number of values stored in it minus 1. To continue my previous example, “This is a string” would have ‘T’ at its 0 position. It would also have ‘h’ at its 1 position, ‘i’ at its 2 position, and so on.

This Instructable is organized into warnings, equipment and materials required for the project, the steps necessary to complete the program, and the conclusion.

Warnings: Possible concerns regarding this project include: -the program crashing if the user enters a value outside of the range specified in step 25 -the program crashing if the user enters a value that is not an integer in step 25

Equipment/Materials: The equipment required for this project includes: a computer that is compatible with the Java Eclipse IDE, and Eclipse itself.

Step 1:

Open Java Eclipse IDE (Integrated Development Environment).

Step 2:

Click File⇒ New⇒ Java Project.

Step 3:

Name your project and click Finish.

Step 4:

Right click on your program⇒ New ⇒Class

Step 5:

Name your new class⇒ Check box that says “public static void main (String[] args)" ⇒ Click Finish.

Step 6:

Type “import java.util.Scanner;” on the line above “public class”.

Step 7:

Type “int position;” within the brackets under “public static void main(String[] args)”.

Step 8:

Type “String data;” under the step 7 code.

Step 9:

Type “Scanner input = new Scanner(System.in);” under the step 8 code.

Step 10:

Create a line of space under “Scanner input” for code clarity.

Step 11:

Type “System.out.println(“Please enter whatever you want: “);” after the step 10 space.

Step 12:

Type “data = input.nextLine();” under the code from step 11.

Step 13:

Type “System.out.println(“You have entered: “ + data);” under the code from step 12.

Step 14:

Add another line of space for code clarity.

Step 15:

Type “System.out.print(“Select a value to grab a character from within the range “);” under the space from step 14.

Step 16:

Type “System.out.println(“of the string given (0 - “ + (data.length()-1) + “): “);” under the code from step 15.

Step 17:

Type “position = input.nextInt();” under the code from step 16.

Step 18:

Add another line of space for code clarity.

Step 19:

Type “System.out.print(“The character at position “ + position + “ is “);” under the space from step 18.

Step 20:

Type “System.out.println(data.charAt(position));” under the code from step 19.

Step 21:

Add a final line of space for code clarity.

Step 22:

Type “input.close();” under the space from step 21.

Step 23:

Click on the green arrow on the toolbar at the top of your screen to run the program.

Step 24:

Respond to the prompt at the bottom of your screen by typing keyboard input of your choice and pressing enter.

Step 25:

Respond to the next prompt you receive with a value that is within the range that is given to you and press enter. WARNING: Do NOT enter a value outside the accepted range or character type, as it will crash the program.

Step 26:

View the final output to make sure the position you entered is the character that is returned.

Step 27: Conclusion

After creating this program, it is important to note that it will only continue to function as long as it is open, and only stays open until the user decides to close out of it manually. Otherwise, the program will close itself upon completion, and will not lead to any performance degradation of the computer as a result of this. As previously mentioned regarding running the program, make sure to only enter a value within the range specified by the program, or the program will crash and not function properly.

<p>thanks for sharing! </p>
You should add checking so if the user does enter a number out of range it informs the user.
<p>Good info, thank you for writing this up to share!</p>

About This Instructable



More by GoldTeam:Understanding String Arrays in Java 
Add instructable to: