Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

using string tokenizer to retrieve rationals

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example code for my assignment follows. We are to use rationals instead of integers. My partner and I have tried this program several ways, but have not been able to get the tokenizer to extract the numerator/denominator. Could any of you help out please?
Also, for those of you who prompted me to solve the "extend" problem I posted, my partner stepped me through it today and helped me solve it!!! Thanks for all your help!!!
Mary Ellen
 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS!!! Forgot to post the code!!!
// TestCommandParameters.java: Pass parameters from the command line
package Chapter6;
public class TestCommandParameters
{
// Main method
public static void main(String[] args)
{
// The result of the operation
int result = 0;
if (args.length != 3)
{
System.out.println(
"Usage: java TestCommandParameters " +
"operator operand1 operand2");
System.exit(0);
}
// Determine the operator
switch (args[0].charAt(0))
{
case '+': result = Integer.parseInt(args[1]) +
Integer.parseInt(args[2]);
break;
case '-': result = Integer.parseInt(args[1]) -
Integer.parseInt(args[2]);
break;
case '*': result = Integer.parseInt(args[1]) *
Integer.parseInt(args[2]);
break;
case '/': result = Integer.parseInt(args[1]) /
Integer.parseInt(args[2]);
}
// Display result
System.out.println(args[1] + args[0] + args[2] + "=" + result);
}
}
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mary
What is your exact requirement or question?
You want to solve this using StringTokenizer class?
or when you give the commandline arguments like this how to get the right output - java TestCommandParameters * 20/3 5/2 ?
 
Sheriff
Posts: 16668
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MaryEllen,
Glad to hear that you were able to solve "extend" by yourself (with a little help from your friend). Programming is like swimming: you won't really learn how to do it until you actually try to do it.
As for the StringTokenizer, have you tried
<pre>
StringTokenizer st1 = new StringTokenizer(args[1], "/");
StringTokenizer st2 = new StringTokenizer(args[2], "/");
</pre>
Also, your result is an int and that will cause some loss of precision in your answer.
J.Lacar

[This message has been edited by JUNILU LACAR (edited March 08, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic