• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Tokenizer Problem

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i have two classes one a test class and another one a class called triangle here is the triangle class, when the input dialog comes out i enter the following information: triangle 3 4 5 from that im suppose to be getting the following output "triangle 3 4 5 (the perimeter) (the area), but intead im getting these weird results(39.0--0.012.0-)someone please help here is my triangle class:

class Triangle
{
double s1,s2,s3;

Triangle(double s1, double s2, double s3)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}

double perimeter()
{
return s1 + s2 + s3;
}

double area()
{
double a = 1/2 *(s1 + s2 + s3);
return a*(a-s1)*(a-s2)*(a-s3);
}
}

and here is the test class:

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 s = "";
double s1 = 0,s2= 0,s3=0;
String figure = "";
String str = JOptionPane.showInputDialog(s);
while(str != "")
{

StringTokenizer tok = new StringTokenizer(str);

if(tok.hasMoreTokens())
figure = tok.nextToken();
if(tok.hasMoreTokens())
s1 = Double.parseDouble(tok.nextToken());
if(tok.hasMoreTokens())
s2 = Double.parseDouble(tok.nextToken());
if(tok.hasMoreTokens())
s3 = Double.parseDouble(tok.nextToken());



if(figure.equalsIgnoreCase("Triangle"))

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

}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your error is in the statement Traingle b =new Traingle(s1,s2,s3);
u must omit this statemnt and directly dispaly s1,s2,s3
 
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
thanks alot i took your advice and everything worked fine.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic