[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-08-15
12
Medium Priority
?
299 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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