Ice08 Input and Output: Printing Messages & Input from Keyboard

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

ICE ICE 8.1: Input/Output (I/O): For Reference Only (Not an Exercise)

ICE 8.1 IS TO BE USED FOR REFERENCE ONLY: Download and open the file in jGrasp.

The basic steps for getting (and using) user input were explained in the lecture and are listed below.

Compile and run the program to see how the input/output portions of it run, and how to enter data in the Run I/O console section of jGrasp

HINTRemember: you will have to make sure that the console screen has the focus in order to actually enter data from the keyboard. Click on/in the console screen and then type. If you are working with Robots in a City, you will also have to hit the Start button (either after you enter your data/response in the console screen or before).

TO REPEAT: The file is to be used as a reference point as you work through the remaining parts of this ICE, and is itself not an exercise.

HINTNOTE: While doing a trace table is not required, but if you find yourself getting confused, then working through a input trace table is may help you put your thoughts in order and build the necessary logic and decision-making elements. 

Program Run

Input Provided


2 <Enter key>


-1 <Enter key>


100 <Enter key>


"Rex Winkus" <Enter key>



When you get done with this part, go on to the next section. Go Next

ICE ICE 8.2: Input/Output: Finding and 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).  There aren't that many errors, only three or four, so don't waste a lot of time looking for more.

You might record all the errors you find in a Program Debug Table although this is not required.

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 is not a number the program should politely point this out, then ask them to type in their choice again.

If the user types in a number that is not valid (for example, a 0 or 10) then program should politely point out that only 1, 2, or 3 are valid choices.

HINTHint: 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

ICE ICE 8.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) , once per main command (in the green box), and stuff that you need to do once each time you ask the user for input (in the yellow box).

For this exercise, you should obtain a copy of the program, rename it, and modify it so you present the user with a brief menu of commands to give to the robot. 

HINTNOTE: Please feel free to use code (e.g., copy-and-paste) from other source files, demo files, research, etc.

The user should be able to tell the robot to turn left, or to move, or pick something up, or put it down, etc. 

You should then use the "if" statement to figure out which command the robot should do. 

The following should be done once  per program 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 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(;

    • Note: You only need to do this once per command
    • 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;

    • Note: You can, and should, reuse memory locations (declared and named variable spaces) where it makes sense 

The steps listed below need to be repeated once each time you 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 robot to move 1 space");

  • 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 (mustn't have that!)  
  1. Check if the user typed in a number:


    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 above (as the userChoice)

    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() method:


  3. Now that you’ve got the user’s input, you need to do something with it. You should set up a bunch of if statements that would compare the possible user input then perform some action based on that choice:

    if(userChoice == 1)
    // if userChoice is 2, then tell the robot to move...

    // if userChoice is 3, then tell the robot to pick thing...
    // if userChoice is 4, then tell the robot to put thing, 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 uniquely named memory location (i.e., declare and initialize another variable like int userChoice2 or int newChoice).


When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 8.4: Input/Output: Code That Repeatedly Asks 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 the new file names and class names so everything still compiles & runs).

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 !)

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 8.5: Input/Output: Tracing Code

The three objectives for this exercise are:

1. to familiarize you with the System.out and statements

2. to review some of the topics you may be seeing on the mid-term exam

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

Download the file the file, and use the more informal Output Trace 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. 

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 8.6: Input/Output: Finding and Fixing Errors

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

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 8.7: Inout/Output: Writing Code

Download 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 and avenue format (e.g., Street: 3, Avenue: 5) using the getStreet() and getAvenue() methods that are built into the Robot class.

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 8.8: Input/Output: A More Detailed Menu with Choices (If Time)

Download the file, and then add 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 up a Thing
    4. (Try to) put down a Thing
    5. Quit by saying "Goodbye Human Task Master!"

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 whole number (an integer) and within the proper range (and don't forget to use the nextLine() method at the appropriate spot to clear any lingering 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 statements.

When you get done with this exercise, show your Instructor.