Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    20

    Unanswered: String Tokenizer

    anyone tell me what is wrong with this code??

    import java.io.*;
    import java.util.*;

    public class Document {

    public static InputStreamReader input = new InputStreamReader(System.in);
    public static BufferedReader keyboardInput = new BufferedReader(input);

    public static void main(String[] args) throws IOException
    {

    String fileName = args[0];

    FileReader file = new FileReader(fileName);
    BufferedReader MyFile = new BufferedReader(file);

    //FileWriter file2 = new FileWriter("outputfile.txt");
    //PrintWriter MyFile2 = new PrintWriter(file2);

    //Attributes and variable definitions.

    int i;
    String x;
    int LineCounter = 0;
    int z;
    int numberoftokens = 0;
    int y;
    StringTokenizer TokenizeMe;

    z = 0;
    y = 0;

    while ( (x = MyFile.readLine()) != null) {
    LineCounter++;
    }
    System.out.println(LineCounter + " lines");

    String [][] Text = new String [20][20];

    TokenizeMe = new StringTokenizer(MyFile.readLine());
    numberoftokens = TokenizeMe.countTokens();

    String currentWord;

    while (z < LineCounter){
    currentWord = TokenizeMe.nextToken();
    while (currentWord != null) {

    Text[z][y] = currentWord;
    }
    //System.out.println(Text[z]);
    z ++;
    }




    //System.out.println(Text[LineCounter]);


    String ReadTheNextLineMethod = MyFile.readLine();

    MyFile.close();

    }
    }

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1

    Re: String Tokenizer

    Originally posted by rhinoball
    anyone tell me what is wrong with this code??

    Sure. It throws NullPointerException here:

    TokenizeMe = new StringTokenizer(MyFile.readLine());

    After you fix that, it will go into an endless loop here:

    while (currentWord != null) {

  3. #3
    Join Date
    Feb 2004
    Posts
    18

    Re: String Tokenizer

    Originally posted by rhinoball
    anyone tell me what is wrong with this code??

    import java.io.*;
    import java.util.*;

    public class Document {

    public static InputStreamReader input = new InputStreamReader(System.in);
    public static BufferedReader keyboardInput = new BufferedReader(input);

    public static void main(String[] args) throws IOException
    {

    String fileName = args[0];

    FileReader file = new FileReader(fileName);
    BufferedReader MyFile = new BufferedReader(file);

    //FileWriter file2 = new FileWriter("outputfile.txt");
    //PrintWriter MyFile2 = new PrintWriter(file2);

    //Attributes and variable definitions.

    int i;
    String x;
    int LineCounter = 0;
    int z;
    int numberoftokens = 0;
    int y;
    StringTokenizer TokenizeMe;

    z = 0;
    y = 0;

    while ( (x = MyFile.readLine()) != null) {
    LineCounter++;
    }
    System.out.println(LineCounter + " lines");

    String [][] Text = new String [20][20];

    TokenizeMe = new StringTokenizer(MyFile.readLine());
    numberoftokens = TokenizeMe.countTokens();

    String currentWord;

    while (z < LineCounter){
    currentWord = TokenizeMe.nextToken();
    while (currentWord != null) {

    Text[z][y] = currentWord;
    }
    //System.out.println(Text[z]);
    z ++;
    }




    //System.out.println(Text[LineCounter]);


    String ReadTheNextLineMethod = MyFile.readLine();

    MyFile.close();

    }
    }

    Thw loope "while (currentWord != null) " will result in an infinite loop. This is because the value of currentword will always be a not null value within the loop. Once you are entering the loop the value of currentword is not changing. You have to include this statement "currentWord = TokenizeMe.nextToken();" again in the while loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •