Solved

A question for Diakov

Posted on 1998-10-19
1
214 Views
Last Modified: 2010-03-30
Hey.
Here are the code for text reading and splitting, it has still some errors. Can you answer?

 my text file has following values: title;a1;40;a2;33;a4;50;

      Here is how I wrote the code, but it doesn't work yet


       String param0 = "";
              try
              {      
                  String url_string = String.valueOf(getParameter("text"));
                  URL a;
                  URLConnection conn;
                  a = new URL(url_string);
                  conn = a.openConnection();
                  conn.setUseCaches(false);
                  DataInputStream dis = new DataInputStream(conn.getInputStream());
                  String inputLine = "";
                  while ((inputLine=dis.readLine())!= null)
                  {
                         param0 =param0 + inputLine;
                  }
                  dis.close();
                 }catch (java.io.IOException e){}
                 java.util.StringTokenizer st  = new java.util.StringTokenizer(param0, ";");
                 while(param0 != null)
                 {
                     if( je==0)
                     {
                      //Firts variable is title
                         title=st.nextToken();
                     }
                    //don't go any more in the string above:
                     je=1;
                   //get labels:
                     labels[i] = st.nextToken();
                   //get values:
                     values[i] = Integer.parseInt(st.nextToken());
                    //drawing color:
                     colors[i] = Color.black;
                    // number of variables:
                     columns=columns+1;
                     if (values[i] > max)
                     {
                         max = values[i];
                     }
                     i++;
                }
0
Comment
Question by:tittapo
1 Comment
 
LVL 8

Accepted Solution

by:
diakov earned 50 total points
Comment Utility
Hello 'tittapo',

This is a working example. I've put the test file on my web server.


//file test_tokenizer.java
import java.net.*;
import java.io.*;

public class test_tokenizer
{

  public static void main(String[] argv)
  {
    String param0 = "";
    try
    {        
      String url_string = "http://hermes.cs.utwente.nl:5111/altered_serve/test.txt";
      URL a;
      URLConnection conn;
      a = new URL(url_string);
      conn = a.openConnection();
      conn.setUseCaches(false);
      DataInputStream dis = new DataInputStream(conn.getInputStream());
      String inputLine = "";
      while ((inputLine=dis.readLine())!= null)
        param0 =param0 + inputLine;
      dis.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    java.util.StringTokenizer st  = new java.util.StringTokenizer(param0, ";");
    while(st.hasMoreTokens())
    {
      System.out.println(st.nextToken());
    }
  }
}

The basic idea of the tokenizers is calling the nextToken() until hasMoreTokens() returns false. You can linearly call nextToken several times, but it is a good idea to test for hasMoreTokens after each nextToken().

Hope this helped,
  Nik

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now