File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes substring from user input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "substring from user input" Watch "substring from user input" New topic

substring from user input

Will Revels

Joined: Jan 19, 2007
Posts: 7
** I cant seem to figure out how to incorporate splitting a string from user input. Any help would be appreciated.

Next write a Java program that enters a 10-digit string as a typical U.S. telephone number, extracts the 3-digit area code, the 3-digit �exchange,� and the remaining 4-digit number as separate strings, prints them and then prints the complete telephone number in the usual formatting. A sample might look like this:
Enter 10-digit telephone number: 1234567890
You entered 1234567890
The area code is 123
The exchange is 456
The number is 7890
The complete telephone number is (123) 456-7890


public class ReadString {

public static void main (String[] args) {

// prompt the user to enter their phone
System.out.print("Enter 10-digit telephone number: ");

// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(;

String phone = null;

// read the input from the command-line; need to use try/catch with the
// readLine() method
try {
phone = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your phone number!");

System.out.println("The number you entered is: " + phone);
System.out.println("The area code is: " + phone);
System.out.println("The exchange is: " + phone);
System.out.println("The number is: " + phone);


} // end of ReadString class


public class PhoneString
public static void main(String[] args){

String s = "9721234567";

String areaCode = s.substring(0,3);
String exchange = s.substring(3,6);
String number = s.substring(6,10);
System.out.println("You have entered the following phone number");
System.out.println( "(" + areaCode + ") " + exchange + "-" + number);
} // end of PhoneString class
marc weber

Joined: Aug 31, 2004
Posts: 11343

All of the pieces are here, so I think you're asking how to put these together, right?

You have 2 classes defined, but everything happens in the main methods. My suggestion is to create one class with at least two methods in addition to main.

For example, you might have a method called getUserInput (that basically does what ReadString's main method does now, except that it would return a String), and you might have another method called formatPhoneNumber (that takes a String as an argument, then basically does what PhoneString's main method does now). Use main only as an entry point: To create an instance of the class, and call the methods.

Try this approach, and see how it works.

PS: Please use Code Tags to keep your code indentation showing.
[ February 11, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Will Revels

Joined: Jan 19, 2007
Posts: 7
Thanks for the tip, Ill give it a shot. I am new to Java so still learning the rules on posting. I assume the code tags are at start and end of code ?
Will Revels

Joined: Jan 19, 2007
Posts: 7
sorry... I thought this was system generated.
[ February 11, 2007: Message edited by: ]

Will Revels
I agree. Here's the link:
subject: substring from user input
It's not a secret anymore!