Solved

A question for Diakov

Posted on 1998-10-19
1
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 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