[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing String value & Accepting returning values with StringTokenizer

Posted on 2005-04-07
24
Medium Priority
?
277 Views
Last Modified: 2010-03-31
I’d like to know how to do these following steps:

The String value (str) needs to be passed to another (separate) CLASS, called “ThreadedLoanCalcServer”.

    String str = "INTEREST="+txtRat.getText()+"&"+"AMOUNT="+txtAmo.getText()+"&"+
                 "PERIOD="+txtTerm.getText()+"&"+"FREQUENCY="+Freq;

Then, ThreadedLoanCalcServer CLASS will calculate and return $275.23&$20343.99, and then, by using “StringTokenizer”, my applet (LoanCalc.CLASS) will accept the values into the below and display.

MPLabel.setText(""+money.format(xxxxx)); // i.e. accept $275.23
TPLabel.setText(""+money.format(zzzzzz)); // i.e. accept $20343.99

Tia
0
Comment
Question by:DanKang
  • 12
  • 12
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13733198
You could also use split() method of String class to break them up
0
 
LVL 92

Expert Comment

by:objects
ID: 13733203
String aString = "word1 word2 word3";
    StringTokenizer parser = new StringTokenizer(str, "&");
    while (parser.hasMoreTokens()) {
        double value = money.parse(parser.nextToken());
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 13733209
> MPLabel.setText(""+money.format(xxxxx)); // i.e. accept $275.23

not sure though why you are formatting when you are already recieving it formatted
why not just use the recieved value
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13733214
ie.

   StringTokenizer parser = new StringTokenizer(str, "&");
   MPLabel.setText(parser.nextToken());
   TPLabel.setText(parser.nextToken());
0
 

Author Comment

by:DanKang
ID: 13733222
I want to use StringTokenizer.       

String s = "$275.23&$20343.99";
      StringTokenizer st = new StringTokenizer(s);
      while (st.hasMoreTokens()) {
            println(st.nextToken());
      }

Is this a way to get the values for the below?

MPLabel.setText(""+money.format(xxxxx)); // i.e. accept $275.23
TPLabel.setText(""+money.format(zzzzzz)); // i.e. accept $20343.99

tia
0
 
LVL 92

Expert Comment

by:objects
ID: 13733244
no you need to specify & as the delimiter, see my example above
0
 

Author Comment

by:DanKang
ID: 13733275
Oh, I see.

double value = money.parse(parser.nextToken());
returns an error:

incompatible types found   : java.lang.Number
required: double
double value = money.parse(parser.nextToken());


 String aString = "$275.23&$20343.99";
    StringTokenizer parser = new StringTokenizer(aString, "&");
    //System.out.println(parser);
    while (parser.hasMoreTokens()) {
    double value = money.parse(parser.nextToken());
    }
//    
//   StringTokenizer parser = new StringTokenizer(str, "&");
   MPLabel.setText(parser.nextToken()); // display monthly payment
   TPLabel.setText(parser.nextToken()); // display total payout
0
 
LVL 92

Expert Comment

by:objects
ID: 13733308
> incompatible types found   : java.lang.Number

sorry, then use:

double value = money.parse(parser.nextToken()).doubleValue();

Though as I mentyioned above if you want to display the formatted value in the label then it would seem you could use the token directly instead of parsing it and then formatting it
0
 

Author Comment

by:DanKang
ID: 13733341
double value = money.parse(parser.nextToken()).doubleValue(); returns an error:

 unreported exception java.text.ParseException; must be caught or declared to be thrown
    double value = money.parse(parser.nextToken()).doubleValue();
1 error

Without this:  

 while (parser.hasMoreTokens()) {
    //double value = money.parse(parser.nextToken());
    double value = money.parse(parser.nextToken()).doubleValue();
    }

It displays fine though.  What is the purpose of putting this?

 while (parser.hasMoreTokens()) {
    //double value = money.parse(parser.nextToken());
    double value = money.parse(parser.nextToken()).doubleValue();
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 13733363
you'll need to handle the exception, do you need to actually parse it though?  Do you need to do any caculations on the value?

If not the follwoing would appear to suffice:

   StringTokenizer parser = new StringTokenizer(str, "&");
   MPLabel.setText(parser.nextToken());
   TPLabel.setText(parser.nextToken());
0
 

Author Comment

by:DanKang
ID: 13733382
Yes, that was what I wanted to ask.

 In the event that the data is invalid or could not be processed for some reason, the server will return a string beginning with the word ERROR (all caps) and followed by a list of messages separated by ampersands. A sample of that could be: ERROR&Invalid value for interest rate&Invalid value for loan amount.

How can we catch them and display onto:

 MPLabel.setText(parser.nextToken());
 TPLabel.setText(parser.nextToken());
?

Then, we have to use:

 while (parser.hasMoreTokens()) {
    //double value = money.parse(parser.nextToken());
    double value = money.parse(parser.nextToken()).doubleValue();
    }
?

0
 
LVL 92

Expert Comment

by:objects
ID: 13733519
then you'd use something like:

StringTokenizer parser = new StringTokenizer(str, "&");
String mp = parser.nextToken();
try
{
   double value = money.parse(mp).doubleValue();
   MPLabel.setText(money.format(value));
}
catch (Exception ex)
{
   // invalid value
}

// now do the same for tp
0
 

Author Comment

by:DanKang
ID: 13736357
If the error string returns "ERROR&Invalid value for interest rate&Invalid value for loan amount", how do you separate the error message and put them in catch to display?

 StringTokenizer parser = new StringTokenizer(aString, "&");
    String mp = parser.nextToken();
    try
    {
       double value = money.parse(mp).doubleValue();
       MPLabel.setText(money.format(value));
    }
    catch (Exception ex)
    {
       MPLabel.setText();  // Display the first error message after "&" -- Invalid value for interest rate
    }
   
    String tp = parser.nextToken();
    try
    {
       double value = money.parse(tp).doubleValue();
       TPLabel.setText(money.format(value));
    }
    catch (Exception ex)
    {
       TPLabel.setText(); // Display the second error message after "&" -- Invalid value for loan amount
    }
0
 

Author Comment

by:DanKang
ID: 13736530
This would be a better question.

The server will return aString either i.e. "$275.23&$20343.99" (which is valid value) or i.e. "ERROR&Invalid value for interest rate&Invalid value for loan amount" (which is invalid value).  If it is invalid value, I want to display the first error message (which would be " Invalid value for interest rate") except "ERROR&" on the first label (which is MPLabel.setText(mp);) and to display the second message (which would be "Invalid value for loan amount") on the second label (which is TPLabel.setText(tp);).

The point is:

if it is in the format of "$275.23&$20343.99", there is no issue
but if it is in the format of "ERROR&Invalid value for interest rate&Invalid value for loan amount", I want to filter "ERROR&" out for the display.

    //String aString = "$275.23&$20343.99";
   // String aString = "ERROR&Invalid value for interest rate&Invalid value for loan amount";
   
    StringTokenizer parser = new StringTokenizer(aString, "&");
    String mp = parser.nextToken();
    try
    {
       double value = money.parse(mp).doubleValue();
       MPLabel.setText(money.format(value));
    }
    catch (Exception ex)
    {
       MPLabel.setText(mp);
    }
   
    String tp = parser.nextToken();
    try
    {
       double value = money.parse(tp).doubleValue();
       TPLabel.setText(money.format(value));
    }
    catch (Exception ex)
    {
       TPLabel.setText(tp);
    }
0
 

Author Comment

by:DanKang
ID: 13736776
I figured out that StringTokenizer parser = new StringTokenizer(aString, "ERROR&"); works fine.
0
 

Author Comment

by:DanKang
ID: 13736934
I need to connect "ThreadedLoanCalcServer" from my applet (LoanCalc.CLASS).   All CLASSes are in the same location.

java ThreadedLoanCalcServer 8080

8080 is port number I chose.

How do you make the connection?
0
 

Author Comment

by:DanKang
ID: 13737403
Is it something like this???

Socket socket = new Socket ("my host xyz", 8080);
OutputStream str = socket.getOutputStream();
InputStream aString = socket.getInputStream();

I need to pass my string ("str") to the server class (ThreadedLoanCalcServer) and get the output from ThreadedLoanCalcServer and display.

Other pieces are working fine except connecting the server class and sent my string and get a returning string from the server class.
0
 

Author Comment

by:DanKang
ID: 13740207
I know  StringTokenizer parser = new StringTokenizer(aString, "ERROR&"); works for this case, but is there any other better way to filter out "ERROR"?

tia
0
 
LVL 92

Expert Comment

by:objects
ID: 13740984
> If the error string returns "ERROR&Invalid value for interest rate&Invalid value for loan amount",
> how do you separate the error message and put them in catch to display?

You already have the error message, eg. in below the mp value will contain the error message

StringTokenizer parser = new StringTokenizer(aString, "&");
    String mp = parser.nextToken();
    try
    {
       double value = money.parse(mp).doubleValue();
       MPLabel.setText(money.format(value));
    }
    catch (Exception ex)
    {
       MPLabel.setText(mp);  // Display the first error message after "&" -- Invalid value for interest rate
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 13740992
Though if you want to display the value if its a number, or display the error if its an error then it again sounds like you don't need to partse:

   StringTokenizer parser = new StringTokenizer(str, "&");
   MPLabel.setText(parser.nextToken());
   TPLabel.setText(parser.nextToken());
0
 

Author Comment

by:DanKang
ID: 13741777
Is this a "recommended" way?

if (aString.startsWith("ERROR")){
        StringTokenizer parser = new StringTokenizer(aString, "ERROR&");
    }
    else {
       StringTokenizer parser = new StringTokenizer(aString, "&");
    }

0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13741810
Sorry I misunderstood earlier, you're sqaying it prepends it with ERROR& if an error occurs.
In that case then you could use:

StringTokenizer parser = new StringTokenizer(str, "&");
if (parser.countTokens()==3) {
   parser.nextToken();   // ERROR
}
MPLabel.setText(parser.nextToken());
TPLabel.setText(parser.nextToken());
0
 

Author Comment

by:DanKang
ID: 13755052
objects, Thank you for your help.
0
 
LVL 92

Expert Comment

by:objects
ID: 13757609
no worries :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

867 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