View Single Post
Trim the spaces from this program:
Posts: n/a
Default Trim the spaces from this program: - 03-25-2010, 10:16 PM

* @param args

import java.util.Scanner;
public class MonthTranslator {
public static void main(String[] args) {
// get the month number
System.out.println("To see the month that matches a number 1 through 12");
System.out.print("enter a month number (1-12): ");
Scanner keyboard = new Scanner(;
int monthNumber = keyboard.nextInt();
// compute the month abbreviation
final String MONTH_TABLE = "January February March April May June " + "July August SeptemberOctober November December ";
int start = (monthNumber - 1) * 9;
int stop = start + 9;
String monthAbbrev = MONTH_TABLE.substring(start, stop);
// display the month
System.out.println("\nMonth #" + monthNumber + " is '" + monthAbbrev + "'.");


This is an example straight out of the Alice in Action with Java book (page 250). It was originally a program created to translate a number (1 through 12) into the corresponding month abbreviation. For instance, 1 = Jan, 2 = Feb, etc...

My assignment is to modify the program so that the entire month is displayed (instead of just the abbreviation as was the original function), then use the String method "trim" to remove leading or trailing spaces (in this case only trailing) from the extracted month. I've done the first part and the program works fine, but I can't figure out how to use trim correctly. The textbook doesn't seem to offer much help and I'm not having much luck googling it or from the eclipse website.

This is only my second program I've done with eclipse and I'm a complete newbie. What is the trim command that is appropriate for this program, and where do I put it? I thought it should go on the line right above the final System.out.println("nMonth etc....) command.

Thanks in advance as always.
Reply With Quote