codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Help on this program  rsixtyone at 16:18 on Saturday, September 17, 2005
 

I need to write a program that prompts for and accepts a telephone number in the form ddd-ddd-dddd, where d is a digit, and prints it out in the following format: (ddd) ddd-dddd.

thanks =)

  Re: Help on this program  crwood at 19:15 on Monday, September 19, 2005
 

import java.io.*;

class EntryTest
{
public static void main(String[] args)
{
BufferedReader br = null;
try
{
br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("enter a number in this format \"ddd-ddd-dddd\"");
boolean acceptable = false;
while(!acceptable)
{
String in = br.readLine();
if(isAcceptable(in))
{
formatString(in);
acceptable = true;
}
else
System.out.println("illegal entry, try again...");
}
br.close();
}
catch(IOException ioe)
{
System.err.println("io: " + ioe.getMessage());
}
}

/**
* there are many ways to do this
*/
private static boolean isAcceptable(String s)
{
String template = "ddd-ddd-dddd";
// check format
int length = s.length();
int firstIndex = s.indexOf("-");
int lastIndex = s.lastIndexOf("-");
if(length != template.length() ||
firstIndex != template.indexOf("-") ||
lastIndex != template.lastIndexOf("-"))
return false;

// check for digits
for(int j = 0; j < length; j++)
{
if(j == firstIndex || j == lastIndex)
continue;
if(!Character.isDigit(s.charAt(j)))
return false;
}

return true;
}

/**
* in: ddd-ddd-dddd
* out: (ddd) ddd-dddd
*/
private static void formatString(String s)
{
String out = "(";
out += s.substring(0, s.indexOf("-"));
out += ") ";
out += s.substring(4);
System.out.println("formatted entry = " + out);
}
}

  Re: Help on this program  rsixtyone at 23:42 on Monday, September 26, 2005
 

hello sir/mme,

i'm just new to Java and the code that is written is troubling to read... i just need the basics stuff with no checking and just use simple objects. thank you

  Re: Help on this program  crwood at 05:23 on Tuesday, September 27, 2005
 

class SimpleEntry
{
public static void main(String[] args)
{
BufferedReader br = null;
try
{
br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("enter a number in this format " +
"\"ddd-ddd-dddd\" and press Enter");
String in = br.readLine();
String out = "(";
out += in.substring(0, in.indexOf("-"));
out += ") ";
out += in.substring(4);
System.out.println("formatted entry = " + out);
br.close();
}
catch(IOException ioe)
{
System.err.println("io: " + ioe.getMessage());
}
}
}








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Why Use Method?
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help
•  Re: How to make an investment calculator
•  Re: line breaks in GUI
•  Re: Graph in Gui...
•  Graph in Gui...
•  Re: Counting zero values in a string
•  Re: Help!


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005