4. Scanner

4.1. Objectives

  • Recognize and correct syntax errors in a Java program.

4.2. Time Goal

  • 25 minutes on this section

4.3. Key Terms

symbol
The name for a variable, function, or other object within a program. Also known as “identifier”.

4.4. Exercises

Note

The System class also provides the special value System.in, which is an InputStream that has methods for reading input from the keyboard. These methods are not convenient to use, but fortunately Java provides other classes that make it easy to handle common input tasks.

For example, Scanner is a class that provides methods for inputting words, numbers, and other data. Scanner is provided by java.util, which is a package that contains various “utility classes”. Before you can use Scanner, you have to import it like this:

import java.util.Scanner;

This import statement tells the compiler that when you refer to Scanner, you mean the one defined in java.util. Using an import statement is necessary because there might be another class named Scanner in another package. Import statements can’t be inside a class definition. By convention, they are usually at the beginning of the file.

Next you have to initialize the Scanner. This line declares a Scanner variable named in and creates a Scanner that reads input from System.in:

Scanner in = new Scanner(System.in);

The Scanner class provides a method called nextLine that reads a line of input from the keyboard and returns a String. The following example reads two lines and repeats them back to the user:

If you omit the import statement at the top of the file, you will get a compiler error saying “cannot find symbol”. That means the compiler doesn’t know where to find the definition for Scanner.

Use the Trinket window below (or open in a separate page) to see what the program does. Remember that you use the triangular “Run” button to compile and run the code, and the pencil button to come back and edit the code.

    Q-80: Let’s say you compile a program and get the following error output. What do you think is wrong with your code?

    Powered by
    ScannerTest.java:7: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
      symbol:   class Scanner
      location: class ScannerTest
    ScannerTest.java:7: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
      symbol:   class Scanner
      location: class ScannerTest
    2 errors
    
  • The computer needs to be rebooted
  • Nice try, but it's a problem with the code, not the computer!
  • At line 7, a symbol not used in the Java programming language was used in your program.
  • Technically correct, but try to find an answer that is more helpful.
  • Your program did not include an import statement for the Scanner class
  • Correct!
  • Your program did not properly declare a Scanner variable at line 7
  • Incorrect, the declaration of the Scanner variable is actually being done correctly.
  • The name of your source file should have been Scanner not ScannerTest
  • Incorrect. As long as the class and filename match, they could be named almost anything.

    Q-81: Run the following program

    What is the output if you enter the name Sally at the prompt?

  • Welcome, Sally to CS415!
    
  • Correct
  • Welcome, name to CS415!
    
  • Incorrect
  • Welcome, + Sally +  to CS415!
    
  • Incorrect
  • Welcome,Sallyto CS415!
    
  • Incorrect
    Q-82: At line 12, name = scanner.next();, select all answers that explain what is occuring in that statement
  • The code is checking to see if the value in name is equal to scanner.next()
  • The Scanner class is reading in what is typed at the keyboard
  • The information typed at the keyboard is being assigned to the variable ``name``
  • The value saved in the variable ``name`` is being transferred to the Scanner class
  • The value stored in the variable ``name`` is being printed to the screen
    Q-83: If line 12 is removed, name = scanner.next();, what is the program output when the program is run? (Note: you don’t need to type anything in to see the output.)
  • ScannerTest.java:8: error: unclosed string literal
        String name = ";
                      ^
    1 error
  • Incorrect--there should be no syntax errors if you removed line 12 correctly.
  • ScannerTest.java:15: error: variable name might not have been initialized
        System.out.println("Welcome, " + name + " to CS415!");
                                         ^
    1 error
  • Incorrect--there should be no syntax errors if you removed line 12 correctly.
  • ScannerTest.java:11: error: package ystem does not exist
        ystem.out.print("What is your name?");
             ^
    1 error
  • Incorrect--there should be no syntax errors if you removed line 12 correctly.
  • What is your name?
    Welcome,  to CS415!
    
  • Correct.
  • What is your name?
    Welcome, + name +  to CS415!
    
  • Incorrect.

Note

ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII).

Use the following code window to answer the questions below. (Remember that you can go back to editing the code using the pencil icon next to the run button.)

    Q-84: When you click run, an error occurs.  Which of the following would correct the code so that it can run?
  • Add a Scanner class
  • Remove the public from the Dog class
  • Change the name of the source file to Dog.java
  • Change the name of the source file to Dog.class
  • Change the name of the class to Main

    Q-85: Update the code above to correct the error and run the code. Notice this only prints out the top part of the dog. Which of the following lines of code will correctly complete the picture? (Note: in order to print out a backslash on the screen, you need to actually have two backslashes in a row inside your code.)

    ^..^      /
    /_/\_____/
       /\   /\
      /  \ /  \
    
  •         System.out.println("/_/\_____/");
            System.out.println("   /\   /\");
            System.out.println("  /  \ /  \");
    
  • Incorrect. Remember that backslashes have to be "escaped" by another backslash to be printed normally
  •         System.out.println("/_/"\"_____/");
            System.out.println("   /"\"   /"\"");
            System.out.println("  /  "\ "/  "\"");
    
  • Incorrect. Remember that backslashes have to be "escaped" by another backslash to be printed normally
  •         System.out.println("/_/\\\_____/");
            System.out.println("   /\\\   /\\\");
            System.out.println("  /  \\\ /  \\\");
    
  • Incorrect. Only one extra backslash is needed to properly print out each backslash (no tripling of backslashes)
  •         System.out.println("/_/\\_____/");
            System.out.println("   /\\   /\\");
            System.out.println("  /  \\ /  \\");
    
  • Correct.
  •         /*
    System.out.println("/_/\\_____/"); System.out.println(" /\\ /\\"); System.out.println(" / \ / \\");
    */
  • Incorrect. This is a multi-line comment, so no output would print.
Next Section - Fundamentals II Lab