• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

help with StringTokenizer

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() + "-" );
}

}
}

}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic