Minecraft Blogs / Tutorial

Java Programming Tutorial #5 - Strings

  • 862 views, 2 today
  • 2
  • 1
  • 1
SourC00kie's Avatar SourC00kie
Retired Moderator
Level 45 : Master Blob
201
Click here to see Java Tutorial #4 - Data Inputs/Outputs

Strings


Introduction to Strings

A string is one or more characters considered as a single value. Everything entered in a program, requested from the user, or displayed as a series of characters is primarily a string. To support strings, Java is equipped with the String class. To declare a variable that would hold a string, use the String data type.

To retrieve a string, you can apply next() to your Scanner class. Here is an example:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String firstName;

System.out.print("Enter the First Name: ");
firstName = scnr.next();

System.out.print("First Name: ");
System.out.print(firstName);
}
}

Here is an example of running the program:

Enter the First Name: James
First Name: James

next() is used to get only one word that is considered terminated by an empty space. To retrieve a string made of different words, you must assume that it would be terminated by a new line. To get such a string, use nextLine().

To initialize a String variable, assign it a double-quoted value. Here is an example:

public class Exercise {
public static void main(String[] args) {
String firstName = "Derp";
}
}

To display the value of a string, you can pass the name of variable to the System.out.print() method. Here is an example:

public class Exercise {

public static void main(String[] args) {
String firstName = "Derp";

System.out.print("First Name: ");
System.out.print(firstName);
}
}

As an alternative, to display a string using System.out.printf(), add a %s expression in the double-quotes ass the placeholder of the variable. Here is an example:

public class Exercise {
public static void main(String[] args) {
String fullName = "Derp Derp";

System.out.printf("Full Name: %s", fullName);
}
}

This would produce:

Full Name: Derp Derp

Escape Sequences

An escape sequence is a special character that displays non-visibly. For example, you can use this type of character to indicate the end of line, that is, to ask the program to continue on the next line. An escape sequence is represented by a backslash character, , followed by another character or symbol. For example, the escape sequence that moves to the next line is n.

An escape can be included in single-quotes as in 'n', in double-quotes as "n", or as part of a string.

The Java language recognizes other escape sequences:
Escape Sequence Name Description
a - Bell (alert) - Makes a sound from the computer
b - Backspace - Takes the cursor back
t - Horizontal Tab - Takes the cursor to the next tab stop
n - New line - Takes the cursor to the beginning of the next line
v - Vertical Tab - Performs a vertical tab
f - Form - feed
r - Carriage return - Causes a carriage return
" - Double Quote - Displays a quotation mark (")
' - Apostrophe - Displays an apostrophe (')
? - Question mark - Displays a question mark
- Backslash - Displays a backslash ()
- Null - Displays a null character

String Concatenation

We mentioned that any character or group of characters is a string. To make it easy to manage strings, you can "concatenate" one string with another to create a new string. The concatenation is done by adding one string to another, which is done using the addition operator. Here is an example:

public class Exercise {

public static void main(String[] args) {
String firstName = "Derp ";
String lastName = "Derp";
String fullName = firstName + lastName;

System.out.print("Full Name: ");
System.out.print(fullName);
}
}

In the same way, you can concatenate as many strings as you want, using the addition operator the same way you would in algebra. When writing System.out.print("First Name: ");, the First Name value is a string. This allows you to also add strings locally where they are used, such as in the print() method. Here is an example:

public class Exercise {

public static void main(String[] args) {
String firstName = "Derp";
String strSeparator = ", ";
String lastName = "Derp";

System.out.print("Full Name: " + lastName + strSeparator + firstName);
}
}
Tags

Create an account or sign in to comment.

1
11/12/2011 6:50 pm
Level 27 : Expert Scribe
JustaFleshWound
JustaFleshWound's Avatar
That went right over my head and hit the wall behind me.
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome