Skip navigation

Introductory Programming In Java

Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6 Lab 7

Lab 4

Working with Strings and arrays

Objectives

Get more experience in using String classes and arrays, and learn how to parse a text using StringTokenizer

Exercise One

Given the array:

int num[] = {1,2,3,6,8,10,12,14,15,17,19,21};

Write a program that inspects each element of the array and does the following:

  1. If the number is divisible by 2 (num % 2 == 0) then print out "The number is divisible by 2".

  2. If the number is divisible by 3 (num % 3 == 0) then print out "The number is divisible by 3".

  3. If the number is divisible by 5 (num % 5 == 0) then print out "The number is divisible by 5".

Then modify the program ) so that this time the numbers are read in from the command line, ie, if your program is called ListNonPrimes then you will enter a command like:

java ListNonPrimes 1 2 3 4 5 6 9 12 15 18 19

Exercise Two

Write a program which reads in a string of ten characters or less and copies each character in the string to an array of characters:

char[] charArray = new char[10];

Note: You may find the charAt() method of String objects to be useful. You probably use it as follows:

String s = "hello"; char firstChar=s.charAt(0);

What happens if you enter more than ten characters? Can you guard against this possibility using an if branch? Try to rewrite your program to quit with a warning message if more than ten characters are entered.

Exercise Three

Write a program which will prompt a user to input their full name. Then write this name back out again with the surname first followed by a comma and then the first two names:

For example, the program would respond to the input:

John Stuart Average

by typing

Average, John Stuart
  • You can assume that the user will input 3 names.

  • There are two ways of writing this program.

    • You can just read in a string and then search that string for spaces (a single space," ", or multiple spaces) which separate the names. This will involve a lot of counting and careful referencing to make sure that you split up the string properly. It is very good practice.

    • You can also use the StringTokenizer class. Here you need to create a StringTokenizer object using your string as follows:

      String input = JOptionPane.showInputDialog(......); StringTokenizer tokens = new StringTokenizer(input);

      You can then call the methods of the StringTokenizer by applying them to the tokens object (part of the exercise is to look up the Java API docs to find our which methods to use. You will find this much easier, but you are now beginning to enter the world of objects more fully.

Further programming ideas

Using the StringTokenizer class isn't the only way to handle a formatted input. Scanner class can do this, too. Check the lecture notes (or The Java Tutorial, or the textbook) to find out how, and (if you are not tired yet) re-implement the solution of the Exercise Three using the Scanner class.

Sometimes, the String class method split() provides even better way to handle the text processing task. Learn how to use it (again look up the Java API docs). When split() is better suited than StringTokenizer?

Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6 Lab 7

Updated:  Tue 14 May 2013 01:03:20 EST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:   Course Webmaster