• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with StringTokenizer  RSS feed

 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, how are u all doing? I am having problems with the StringTokenizer and I am hoping you all can help, I have a test class that accepts the values of geometric figures in the following order (name of figure, side/radius, side 2, side 3) here is my test class and I want to know why its not working I am pretty sure its something wrong in my StringTokenization :

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.StringTokenizer;

class Driver
{

public static void main(String[] arg)
{


String str = getData.getString("Enter data for a figure");
while(str != "")
{

StringTokenizer tok = new StringTokenizer(str);

while(tok.hasMoreTokens())

{
String figure = tok.nextToken();
double s1 = Double.parseDouble(tok.nextToken());
double s2 = Double.parseDouble(tok.nextToken());
double s3 = Double.parseDouble(tok.nextToken());

if(figure.equalsIgnoreCase("Triangle"));
Measurments b = new Triangle(s1,s2,s3);
System.out.println(s1 + '\t' + s2 +'\t' + s3 + '\t'
+ "-" + b.area() + b.perimeter() + "-" );
}

}
}

}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you don't say what the problem actually is. A quick look at the code suggests that the StringTokenizer part is fine (although it's nice to actually check if there is a next token before grabbing it), but that you're gonna get an error because "getData" is undefined. Not sure what you want "getData" to be -- did you copy it from some example someplace? If so, you also need to copy the part where "getData" is declared.
 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getData is just another class I have to use the JOption Pane for example here is a part of that class:

class getData
{
static String str;

static String getString(String s)
{
str = JOptionPane.showInputDialog(s);
return str;
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice how your use of a nonstandard class name -- i.e., a class name starting with a lower-case letter -- confused me and made me think getData was an undeclared variable. This is an excellent example of why it's important to follow coding conventions. Java classes are always named with an initial capital letter.

Anyway, OK, great. But you still haven't told us what the problem is! Just saying "this doesn't work" implies that someone's going to carefully study your code, or copy and paste and compile and run it. That's assuming an awful lot. It generally works better if you just tell us what you expect to happen when you run this code, and what happens instead. That way the potential number of people who could help is much larger!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!