• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

substring from user input

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
** 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


******************************************************************
import java.io.*;

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(System.in));

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.exit(1);
}

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";

System.out.println(s);
String areaCode = s.substring(0,3);
String exchange = s.substring(3,6);
String number = s.substring(6,10);
System.out.println(areaCode);
System.out.println(exchange);
System.out.println(number);
System.out.println("You have entered the following phone number");
System.out.println( "(" + areaCode + ") " + exchange + "-" + number);
}
} // end of PhoneString class
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Will Revels
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry... I thought this was system generated.
[ February 11, 2007: Message edited by: ]

Will Revels
 
reply
    Bookmark Topic Watch Topic
  • New Topic