Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
A String is a bunch of characters in Java. It’s like having several char values in a row. To read a String value from the keyboard, you can call either next or nextLine:
  • The methodnextreads up to the next blank space.

For example, with the input Barry A. Burd, the statements

String firstName =;

String middleInit =;

String lastName =;

assign Barry to firstName, A. to middleInit, and Burd to lastName.

  • The methodnextLinereads up to the end of the current line.

For example, with input Barry A. Burd, the statement

String fullName = keyboard.nextLine();

assigns Barry A. Burd to the variable fullName.

To display a String value, you can call one of your old friends, System.out.print or System.out.println. A statement like

out.print("Customer's full name: ");

displays the String value "Customer's full name: ".

You can use print and println to write String values to a disk file.

In a Java program, you surround the letters in a String literal with double quote marks.

Adding strings to things

In Java, you can put a plus sign between a String value and a numeric value. When you do, Java turns everything into one big String value. To see how this works, consider the following code.

import java.util.Scanner;

import static java.lang.System.out;

class ProcessMoreData {

public static void main(String args[]) {

Scanner keyboard = new Scanner(;

String fullName;

double amount;

boolean taxable;

double total;

out.print("Customer's full name: ");

fullName = keyboard.nextLine();

out.print("Amount: ");

amount = keyboard.nextDouble();

out.print("Taxable? (true/false) ");

taxable = keyboard.nextBoolean();

if (taxble) {

total = amount * 1.05;

} else {

total = amount;



out.print("The total for ");


out.print(" is ");






Replace the last several lines in of the code below with the following single line:

out.println("The total for " + fullName + " is " + total + ".");

Fun with word order

Write a program that inputs six words from the keyboard. The program outputs six sentences, each with the first word in a different position. For example, the output of one run might look like this:

only I have eyes for you.

I only have eyes for you.

I have only eyes for you.

I have eyes only for you.

I have eyes for only you.

I have eyes for you only.

About This Article

This article is from the book:

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

This article can be found in the category: