dummies logo
Articles

Article Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Collections

Collections

Explore all collections
close
BYOB (Be Your Own Boss)Be a Rad DadCareer ShiftingContemplating the CosmosFor Those Seeking Peace of MindFor the Aspiring AficionadoFor the Budding Cannabis EnthusiastFor the College BoundFor the Exam-Season CrammerFor the Game Day Prepper
Custom Solutions
Dummies AI
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
forward arrow
  • Custom Solutions
  • Dummies AI
    • Log In
    Explore Book
    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 = keyboard.next();

    String middleInit = keyboard.next();

    String lastName = keyboard.next();

    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(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.

    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
    Java
    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

    Quick Links

    About For Dummies
     Terms of Use
    Copyright © 2000-2025 by John Wiley & Sons, Inc., or related companies. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
    © 2025 MARVEL

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant