Solved

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

Posted on 2004-08-15
12
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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