Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A question for Diakov

Posted on 1998-10-19
1
Medium Priority
?
264 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 150 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

604 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