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
Solved

A question for Diakov

Posted on 1998-10-19
1
241 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 83
fibonacci ten numbers 4 45
MySQL  on Tomcat 8 68
web application structure 18 100
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

838 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