Solved

A question for Diakov

Posted on 1998-10-19
1
225 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
ID: 1225996
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
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:

937 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

15 Experts available now in Live!

Get 1:1 Help Now