hedge243
asked on
why is my method using a Tokenizer hanging up on the second token?
import java.io.*;
import java.util.*;
//************************ ********** ********** ********** ********** **
//Programed designed to break up phone and print area code, exchange
// and extension all separately
//************************ ********** ********** ********** ********** **
class Phone
{
public static void main(String[] args)throws IOException
{
int area, exchange, extension;
String reader;
System.out.print("Please enter phone number: ");
area=token();
System.out.println(area+ " is the area code");
System.out.flush();
exchange=token();
System.out.println(exchang e+ " is the exchange");
System.out.flush();
extension=token();
System.out.println(extensi on+ " is the extension");
} //method main
//************************ ********** ********** ********** ********** *****
// methods used to tokenize areacode, exchange and extension
//************************ ********** ********** ********** ********** *****
static int token()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.i n));
int tokens;
StringTokenizer reader;
reader = new StringTokenizer(in.readLin e());
return Integer.parseInt(reader.ne xtToken("- "));
}// method token
} // class Phone
import java.util.*;
//************************
//Programed designed to break up phone and print area code, exchange
// and extension all separately
//************************
class Phone
{
public static void main(String[] args)throws IOException
{
int area, exchange, extension;
String reader;
System.out.print("Please enter phone number: ");
area=token();
System.out.println(area+ " is the area code");
System.out.flush();
exchange=token();
System.out.println(exchang
System.out.flush();
extension=token();
System.out.println(extensi
} //method main
//************************
// methods used to tokenize areacode, exchange and extension
//************************
static int token()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.i
int tokens;
StringTokenizer reader;
reader = new StringTokenizer(in.readLin
return Integer.parseInt(reader.ne
}// method token
} // class Phone
You're not prompting in the same sequence you're expecting the response in. You only ask for the number and then try to read more than one input (which will block, waiting for more input)
because you (attempt to) read a new line each time you call token.
you need to read all three from one line:
static int[] token()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.i n));
int tokens;
StringTokenizer reader;
reader = new StringTokenizer(in.readLin e(), "-");
return new int[] { Integer.parseInt(reader.ne xtToken()) , Integer.parseInt(reader.ne xtToken()) , Integer.parseInt(reader.ne xtToken()) };
}// method token
you need to read all three from one line:
static int[] token()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.i
int tokens;
StringTokenizer reader;
reader = new StringTokenizer(in.readLin
return new int[] { Integer.parseInt(reader.ne
}// method token
You need something more like this:
import java.io.*;
import java.util.*;
class Phone {
public static void main(String[] args) throws IOException {
System.out.print("Please enter phone number: ");
String[] parts = getNumberParts();
System.out.println(parts[0 ] + " is the area code");
System.out.println(parts[1 ] + " is the exchange");
System.out.println(parts[2 ] + " is the extension");
}
//method main
//************************ ********** ********** ********** ********** *****
// methods used to tokenize areacode, exchange and extension
//************************ ********** ********** ********** ********** *****
/**
* Gets the numberParts attribute of the Phone class
*
* @return The numberParts value
* @exception IOException Description of the Exception
*/
static String[] getNumberParts() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.i n));
return in.readLine().split("-");
}
}
// class Phone
import java.io.*;
import java.util.*;
class Phone {
public static void main(String[] args) throws IOException {
System.out.print("Please enter phone number: ");
String[] parts = getNumberParts();
System.out.println(parts[0
System.out.println(parts[1
System.out.println(parts[2
}
//method main
//************************
// methods used to tokenize areacode, exchange and extension
//************************
/**
* Gets the numberParts attribute of the Phone class
*
* @return The numberParts value
* @exception IOException Description of the Exception
*/
static String[] getNumberParts() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.i
return in.readLine().split("-");
}
}
// class Phone
import java.io.*;
import java.util.*;
//************************
//Programed designed to break up phone and print area code, exchange
// and extension all separately
//************************
class Phone
{
public static void main(String[] args)throws IOException
{
int area, exchange, extension;
String reader;
System.out.print("Please enter phone number: ");
int[] phone = token();
area=phone[0];
System.out.println(area+ " is the area code");
exchange=phone[1];
System.out.println(exchang
extension=phone[2];
System.out.println(extensi
} //method main
//************************
// methods used to tokenize areacode, exchange and extension
//************************
static int[] token()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.i
int tokens;
StringTokenizer reader;
reader = new StringTokenizer(in.readLin
return new int[] { Integer.parseInt(reader.ne
}// method token
}
>>int area, exchange, extension;
You variables look plausible but really phone numbers are Strings, apart from the very rare occasions when arithmetic needs to be done on them
You variables look plausible but really phone numbers are Strings, apart from the very rare occasions when arithmetic needs to be done on them
ASKER
still doesnt work?????? compiles but the output is garbage.
also, Im not familiar with int [] ....what does this stand for?
import java.io.*;
import java.util.*;
//************************ ********** ********** ********** ********** **
//Programed designed to break up phone and print area code, exchange
// and extension all separately
//************************ ********** ********** ********** ********** **
class Phone
{
public static void main(String[] args)throws IOException
{
int area, exchange, extension;
String reader;
System.out.print("Please enter phone number: ");
area=token();
System.out.println(area+ " is the area code");
System.out.flush();
exchange=token();
System.out.println(exchang e+ " is the exchange");
System.out.flush();
extension=token();
System.out.println(extensi on+ " is the extension");
} //method main
//************************ ********** ********** ********** ********** *****
// methods used to tokenize areacode, exchange and extension
//************************ ********** ********** ********** ********** *****
static int[] token()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.i n));
int tokens;
StringTokenizer reader;
reader = new StringTokenizer(in.readLin e(),"-");
return new int[]{ Integer.parseInt(reader.ne xtToken()) , Integer.parseInt(reader.ne xtToken()) , Integer.parseInt(reader.ne xtToken()) };
}// method token
} // class Phone
also, Im not familiar with int [] ....what does this stand for?
import java.io.*;
import java.util.*;
//************************
//Programed designed to break up phone and print area code, exchange
// and extension all separately
//************************
class Phone
{
public static void main(String[] args)throws IOException
{
int area, exchange, extension;
String reader;
System.out.print("Please enter phone number: ");
area=token();
System.out.println(area+ " is the area code");
System.out.flush();
exchange=token();
System.out.println(exchang
System.out.flush();
extension=token();
System.out.println(extensi
} //method main
//************************
// methods used to tokenize areacode, exchange and extension
//************************
static int[] token()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.i
int tokens;
StringTokenizer reader;
reader = new StringTokenizer(in.readLin
return new int[]{ Integer.parseInt(reader.ne
}// method token
} // class Phone
> with int [] ....what does this stand for?
an array of int's, 3 in this case
an array of int's, 3 in this case
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
you are my new best friend
I was very close to lighting myself on fire
I was very close to lighting myself on fire
Glad I could help :)
hedge243, can you tell me why you ignored my (correct) answer to the question?