© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_7

7. Input and Output

Ron Dai1 
(1)
Seattle, WA, USA
 

During runtime of a computer program, the program can ask the user to input data, read the user’s input, and then show the user an output result. Scanner is the tool we use to implement the user interaction feature on the console window.

Importing java.util.Scanner

The Scanner utility class and its methods have been predefined in a package. We use the import statement to integrate the Scanner class with the program we are creating.
  • Approach 1. Add line import java.util.Scanner; at the top of your Java code, and then add the following in your main() class:

Scanner input = new Scanner(System.in);
  • Approach 2. Type code Scanner input = new Scanner(System.in); in your Java code directly, and then use Eclipse’s IntelliSense to choose the right fix. In other words, Eclipse will suggest that you add the import statement because it spots that you might need it.

    As a result, import java.util.Scanner; will be added at the top of the class.

Getting Input

There are several ways to read user input data from a program:
  • nextLine(): read a string input

  • next(): read a string input

  • nextInt(): read an integer input

  • nextFloat(): read a float number input

What is the difference between nextLine() and next()?
  • next() reads the input only until the space.

    It cannot read two words separated by a space. And it places the cursor at the same line after reading the input stream, meaning it doesn’t change the line.

  • nextLine() reads the input until the end of the line (‘ ’).

    It will automatically move the scanner down after returning the current line.

Producing Output

System.out.println is a common way to display text in the console window. Developers often use it to read a user’s input, provide general information to the user, and log information (to the console) during runtime in order to find out what is going on with key variables.

We often use the following special characters (i.e., escape characters) to control the output format:
  • +: concatenates two strings;

  • : a newline character;

  • : a tab key character that aligns text at the tab width;

  • \: a backslash character;

  • : a carriage return character;

  • " and ": double quote characters.

Here is an example:
System.out.println("This demonstrates " + ""how to display a table format". ");
System.out.println("123 45 6789 ab cde f");
This generates the following output:
This demonstrates "how to display a table format".
123    45    6789
ab     cde   f

Lab Work

Practice using the statement System.out.println() to:
  1. 1.

    display the string concatenation between two substrings “I am” and “a developer” using +

     
  2. 2.

    display a new line

     
  3. 3.

    display quotes using " and "

     

Example

Which of the following is the correct syntax to output a message?
  1. 1.

    System.out.println("Hello, world!");

     
  2. 2.

    System.println.out('Hello, world!');

     
  3. 3.

    System.println("Hello, world!");

     
  4. 4.

    System.println(Hello, world!);

     
  5. 5.

    Out.system.println"(Hello, world!)";

     

Answer: 1

Example

What is the output from the following statements?
System.out.println(""Quotes"");
System.out.println("Forward slashes \//");
System.out.println("How '"profound' "\" it is!");
Answer:
"Quotes"
Forward slashes //
How '"profound' "" it is!

Lab Work

What is the output from the following program? What if we replace the next() with nextLine()?
public class TestScanner {
       public static void main(String arg[]) {
               Scanner sc=new Scanner(System.in);
               System.out.println("enter string for c");
               String c=sc.next();
               System.out.println("c is "+c);
               System.out.println("enter string for d");
               String d=sc.next();
               System.out.println("d is "+d);
       }
}

Problems

  1. 1.
    What is the output produced from the following statements?
    System.out.println("name age height");
    System.out.println("Anthony 17 5'9"");
    System.out.println("Belly 17 5'6"");
    System.out.println("Bighead 16 6'");
     
  2. 2.
    What is the output produced from the following statements?
    System.out.println(" a b c");
    System.out.println("\\");
    System.out.println("'");
    System.out.println(""""");
     
  3. 3.
    Write a program in Java to print the following:
    /
    \//
    \///
     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.78.136