Solved

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

Posted on 2004-08-15
12
283 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
HashTable highest marks enumeration alternative 9 48
swing controls 2 23
Java List 4 40
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

749 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