Link to home
Start Free TrialLog in
Avatar of hedge243
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(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

            


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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
}
>>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
Avatar of hedge243
hedge243

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

            


> with int [] ....what does this stand for?

an array of int's, 3 in this case

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you are my new best friend

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?