Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Trim the spaces from this program:
Old
jamieirl
Guest
 
Status:
Posts: n/a
Default Trim the spaces from this program: - 03-25-2010, 11: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(System.in);
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
Old
dragnet
Member
 
Status: Offline
Posts: 38
Join Date: Jan 2010
Default 03-26-2010, 01:21 AM

Not sure I follow exactly what it is you are trying to accomplish, but the trim function is a built in function of the String class. You call it by using string_name.trim(). So from your example above it would be something like monthAbbrev.trim(). That will trim leading and trailing white spaces from the string.

Is that what you wanted to know?
   
Reply With Quote
Old
jamieirl
Guest
 
Status:
Posts: n/a
Default 03-26-2010, 01:33 AM

Quote:
Originally Posted by dragnet View Post
Not sure I follow exactly what it is you are trying to accomplish, but the trim function is a built in function of the String class. You call it by using string_name.trim(). So from your example above it would be something like monthAbbrev.trim(). That will trim leading and trailing white spaces from the string.

Is that what you wanted to know?
That is exactly what I wanted to know, thank you. I inserted .trim() after monthAbbrev in this line :

System.out.println("\nMonth #" + monthNumber + " is '" + monthAbbrev + "'.");

and that's all it took. As long as I've been messing with this it seems like it should have been more difficult, but I'm not complaining

Thanks again.
   
Reply With Quote
Old
dragnet
Member
 
Status: Offline
Posts: 38
Join Date: Jan 2010
Default 03-26-2010, 01:55 AM

Trust me, from my experience (Which admittedly is not a ton), it's the easiest stuff that scares me. Complex issues are usually easier to see/find, at least for myself.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2017, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.