Solved

why is my method using a Tokenizer hanging up on the second token?

Posted on 2004-08-15
12
273 Views
Last Modified: 2010-03-31
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(exchange+ " is the exchange");
            System.out.flush();
            extension=token();
            System.out.println(extension+ " 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.in));

            int tokens;
            StringTokenizer reader;

            reader = new StringTokenizer(in.readLine());
            
            return Integer.parseInt(reader.nextToken("-"));

            }// method token


} // class Phone

            


0
Comment
Question by:hedge243
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11805994
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)
0
 
LVL 92

Expert Comment

by:objects
ID: 11805996
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.in));

          int tokens;
          StringTokenizer reader;

          reader = new StringTokenizer(in.readLine(), "-");
         
          return new int[] { Integer.parseInt(reader.nextToken()), Integer.parseInt(reader.nextToken()), Integer.parseInt(reader.nextToken()) };

          }// method token
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11806016
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.in));
            return in.readLine().split("-");
      }

}
// class Phone
0
 
LVL 92

Expert Comment

by:objects
ID: 11806033

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(exchange+ " is the exchange");
          extension=phone[2];
          System.out.println(extension+ " 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.in));

          int tokens;
          StringTokenizer reader;

          reader = new StringTokenizer(in.readLine(), "-");
         
          return new int[] { Integer.parseInt(reader.nextToken()), Integer.parseInt(reader.nextToken()), Integer.parseInt(reader.nextToken()) };

          }// method token
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11806044
>>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
0
 

Author Comment

by:hedge243
ID: 11806057
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(exchange+ " is the exchange");
            System.out.flush();
            extension=token();
            System.out.println(extension+ " 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.in));

            int tokens;
            StringTokenizer reader;

            reader = new StringTokenizer(in.readLine(),"-");
            
            return new int[]{ Integer.parseInt(reader.nextToken()), Integer.parseInt(reader.nextToken()), Integer.parseInt(reader.nextToken()) };


            }// method token


} // class Phone

            


0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Expert Comment

by:objects
ID: 11806096
> with int [] ....what does this stand for?

an array of int's, 3 in this case

0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 11806102
> still doesnt work??????  compiles but the output is garbage.

you need to make changes to main, see the complete code i posted above.
0
 
LVL 92

Expert Comment

by:objects
ID: 11806155
0
 

Author Comment

by:hedge243
ID: 11806197
you are my new best friend

I was very close to lighting myself on fire
0
 
LVL 92

Expert Comment

by:objects
ID: 11806205
Glad I could help :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11807977
hedge243, can you tell me why you ignored my (correct) answer to the question?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now