CPE 102
Winter 2016
Lab 03
Objectives
Resources
Ground Rules
Orientation
You will be developing a
simple Java program that makes use
of the ArrayList and Scanner classes from the Java Standard Library to
read user
input from the command line and separate the responses into different
collections based on the type of response.
You can (and should) write this program in one source file
containing
one class and with only a main
method. No instance variables,
constructors,
or other methods are required.
Specification
main
for a Java program. Remember,
all methods must be defined within a class and you should know what the
class name is based on the given file name.
main
method. Use only local
variables (no instance variables). The
program should prompt for user input and then determine if the response
was an integer, double, or some other string of characters. Depending on the type of the response, it
should be placed in one of three different ArrayList objects. The program should repeatedly prompt and
filter the input until the user types quit. The program then prints out all of the integer
values, double values, and other values in a specified format. Sample runs of the program are provided below
(the user responses are bold for clarity only). Note
that your program must work for any data in any order:
Sample Run 1
Enter an int,
double, any random text, or type
"quit" to end: Here is some
text
Enter an int,
double, any random text, or type
"quit" to end: 95
Enter an int,
double, any random text, or type
"quit" to end: more text
Enter an int,
double, any random text, or type
"quit" to end: 0.765
Enter an int,
double, any random text, or type
"quit" to end: -6.7
Enter an int,
double, any random text, or type
"quit" to end: 2
Enter an int,
double, any random text, or type
"quit" to end: abc
Enter an int,
double, any random text, or type
"quit" to end: 4.5.6
Enter an int,
double, any random text, or type
"quit" to end: 789
Enter an int,
double, any random text, or type
"quit" to end: quit
Integers:
Integer[0]
is 95
Integer[1]
is 2
Integer[2]
is 789
Doubles:
Double[0]
is 0.765
Double[1]
is -6.7
Other:
Other[0]
is Here is some text
Other[1]
is more text
Other[2]
is abc
Other[3]
is 4.5.6
Sample Run 2
Enter an int,
double, any random text, or type
"quit" to end: quit
Integers:
Doubles:
Other:
Hints
& Suggestions
ArrayList<Integer>
intList = new
ArrayList<Integer>();
Notice that the type to
be collected in an ArrayList, in
this case Integer, is specified immediately after the ArrayList class
by
putting it inside the <>-brackets.
To create an ArrayList to collect different types simply
substitute the
appropriate class name for Integer in the example above.
intList.add(new Integer(7));
Notice that the example above shows adding the literal int 7 to the
ArrayList. You will be constructing Integer objects from ints scanned in using the
Scanner class.
Scanner scanner =
new Scanner(System.in);
You would then call the
appropriate methods to read in
different types of data. The Scanner class has several methods to determine
the type of the next data to read without actually reading the data. Read the
javadocs for the class to see what methods are available and some nice
examples
on how to parse text for various types of data using this class.
Lab courtesy of Julie Workman.