Input and Output: Printing Messages For The User & Reading Information From The Keyboard

Note:  Please keep the programs that you create today, in case you have a dispute about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy

Part 1: Input / Output: Tracing An Example

            The basic steps for getting (and using) user input were both explained in lecture, and are listed below.  For this exercise, you need to get the normal, detail-oriented, laborious-to-fill-out, Program Trace Table (NOT the Output Trace Table) and fill it out for the file.  Because this program relies on user input, you should trace it three times, feeding the program input as indicated below (You may assume that the user presses the 'Start' button as soon as the program appears)

Program Run

Input Provided


2<enter key>


-1<enter key>


YourFirstName<enter key>


Part 2: Input / Output: Finding & Fixing Errors

    For this exercise, you should download the file, and find and fix all the errors in it (both compile-time/syntax AND logical/intent errors).  You should record all the errors you find in a Program Debug Table, and be ready to explain the errors to the rest of the class. 

   The purpose of the program is to allow the user to repeatedly tell the program what they want the robot to do. If the user types in something that's not a number the program should politely point that out, then ask them to type in their choice again. If the user types in a number that isn't valid (for example 0 or 10) then program should politely point out that only 1, 2, or 3 are valid choices.

    Hint: You may want to consult the blue/yellow boxes listed below to help you remember all the different steps that are required for doing I/O.


Part 3: Input / Output: Writing Code

            As per the lecture, the basic steps for getting (and using) user input are listed below.  The steps are broadly divided up into stuff you need to do once (in the blue box) and stuff that you need to do once per time that you want to ask the user for input (in the yellow box).


For this exercise, you should obtain a copy of the program, rename it Basic_I_O, and modify it so that you present the user with a brief menu of commands to give to the robot.  The user should be able to tell the robot to turn left, or to move, or pick something up, or put it down.  You should then use the "if" statement to figure out which command the robot should do.  A brief outline is provided in Figure 2 (below the boxes). 


The following should be done once  per file:

  1. At the very top of the file, right below import becker.robots.*; add the following line

          import java.util.*;


All of the following should be done once  per command/service:

  1. Find the command that should ask the user for input.  Typically, this will be the main command of the program, but it can be any command.
    Inside that command, create a new Scanner object:

         Scanner keyboard = new Scanner(;

    1. Note: You only need to do this once per command
    2. Note: You should only create (at most) ONE Scanner object per command.  Even though the program will compile and run ok with multiple Scanners, you should only create one.

  2. In that same command, you’ll need to create a new variable (aka a new memory location) to store the user’s input. For example:

          int userChoice;
    1. You can, and should, reuse memory locations where it makes sense 



The steps listed below need to be repeated once EACH time that you want to ask the user for input:


  1. Ask the user to type something

    System.out.println("Type 1 if you want the robot to turn right");
    System.out.println("Type 2 if you want it to move 1 intersection");
  1. If you leave out this step, the program will simply wait for the user to type something.  The user won’t understand that they’re supposed to type something, and get confused


  1. Check if the user typed in a number (as the very next thing)

       if( keyboard.hasNextInt() )


a.     Note that we use the hasNextInt method here, and the nextInt method in the next step.


  1. IF THERE IS AN INTEGER AVAILBLE, then get that number.  This number (the user’s input) needs to be stored in the memory location that we set up in step 3:

          userChoice = keyboard.nextInt();

  2. Regardless of whether there was an integer available, we need to get rid of anything else that the user typed in, using the nextLine command:

  3. Now that you’ve got the user’s input, you need to do something with it.
          if ( userChoice == 1)
          // if userChoice is 2, then tell the robot to move, etc

    Keep in mind that a variable (a memory location) can only hold ONE value at a time, so if you go back and do these steps over, you’ll lose the value that’s currently stored in userChoice.  If you don’t need to remember the current choice any longer, this is fine.  If you need to keep this value (for whatever reason), then you’ll need another memory location (i.e., another variable).



Figure 1: How To Get User Input


Figure 1: The Outline Of The Program For This Exercise



Part 5: Input / Output: Writing Code That Repeatedly Asks The User For Input


Take the program that you wrote in the prior exercise, and use Fileà Save As to create another copy that you can keep working on (obviously, you'll need to adjust things so that everything still compiles & runs, etc, etc).  Now that you've got that separate copy, build on what you previously did by setting things up so that the program will REPEATEDLY ask the user what to do.  In order to make this useful, you should print out an additional menu option which allows the user to quit, make sure that your while loop runs while the user's choice is not that number, and then double-check that the nextLine command is properly clearing out any extra input (you should have been doing this all along, but leaving it out is a common error that sometimes doesn't affect the program – until now J  )


Part  6: Tracing Code: I/O


The three objectives for this exercise are

1.      to familiarize you with the System.out.println statement

2.      to review some of the topics that you'll be seeing on the exam,

3.      and to start moving you away from the ultra-detailed program execution traces and towards an equivalent-yet-less-meticulous approach to tracing.


Download the file the file, and use the more informal OUTPUT Tracing Table to trace what the program does.  Once you’ve done that, you should compile and run the program, and check that your trace is correct.  For any part of the trace that you didn’t do correctly, you must do a normal, detailed, program execution trace for just that part of the program that you got wrong.


Part  7: (Optional) Writing Code: I/O: Output


Download the file the file, and use a loop to move the robot forward 10 intersections.  At each intersection, the program should print out the current location (in street, avenue format), using the getAvenue() and getStreet() commands that are built into the robot.


Part  8: Finding and Fixing Errors; Tracing Code: I/O


Download the file named  Based on the lecture, find and fix any and all errors in the file. 


Part  9: Writing Code: I/O: Input and Output


Download the file the file, and then print out the following menu of choices:


You can have the robot do the following:

  1. Turn left
  2. Move forwards 1 intersection
  3. (Try to) pick a Thing up
  4. (Try to) put a Thing down

Please type the number of your choice, then press the 'Enter' key


After you've printed out the above menu, then get the user's input.  Make sure that you check that the user has typed in a number!  (And don't forget to use the nextLine() command to clear any remaining input!)

Once you've got that number, you should use it to make the robot do whatever the user chose, using a series of if/else statements.