String
char
next
nextLine
For example, with the input Barry A. Burd, the statements
String firstName = keyboard.next();
String middleInit = keyboard.next();
String lastName = keyboard.next();
assign Barry to firstName, A. to middleInit, and Burd to lastName.
Barry
firstName
A.
middleInit
Burd
lastName
nextLinereads up to the end of the current line.
For example, with input Barry A. Burd, the statement
Barry A. Burd
String fullName = keyboard.nextLine();
assigns Barry A. Burd to the variable fullName.
fullName
To display a String value, you can call one of your old friends, System.out.print or System.out.println. A statement like
System.out.print or System.out.println. A statement like
or System.out.println. A statement like
System.out.println. A statement like
. A statement like
out.print("Customer's full name: ");
displays the String value "Customer's full name: ".
"Customer's full name: "
You can use print and println to write String values to a disk file.
print
println
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(System.in);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.println();out.print("The total for ");out.print(fullName);out.print(" is ");out.print(total);out.println("."); keyboard.close();}}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.
import java.util.Scanner;
import static java.lang.System.out;
class ProcessMoreData {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
String fullName;
double amount;
boolean taxable;
double total;
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.println();
out.print("The total for ");
out.print(fullName);
out.print(" is ");
out.print(total);
out.println(".");
keyboard.close();
Replace the last several lines in of the code below with the following single line:
out.println("The total for " + fullName + " is " + total + ".");
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:
Beginning Programming with Java For Dummies
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:
Java
Trending
Creating Custom Excel Templates
How to Play Rummy: All You Need to Know
Generative AI For Dummies Cheat Sheet
Marvel Comics For Dummies Cheat Sheet
How a New Pope Is Elected in the Conclave
From Category
Important Features of the Java Language
How to Download and Install TextPad
How to Install JavaFX and Scene Builder
Getting a Value from a Method in Java
A Few Things about Java GUIs