Passing String value & Accepting returning values with StringTokenizer

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
DanKangAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
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
 
objectsCommented:
You could also use split() method of String class to break them up
0
 
objectsCommented:
String aString = "word1 word2 word3";
    StringTokenizer parser = new StringTokenizer(str, "&");
    while (parser.hasMoreTokens()) {
        double value = money.parse(parser.nextToken());
    }
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
objectsCommented:
> 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
 
objectsCommented:
ie.

   StringTokenizer parser = new StringTokenizer(str, "&");
   MPLabel.setText(parser.nextToken());
   TPLabel.setText(parser.nextToken());
0
 
DanKangAuthor Commented:
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
 
objectsCommented:
no you need to specify & as the delimiter, see my example above
0
 
DanKangAuthor Commented:
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
 
objectsCommented:
> 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
 
DanKangAuthor Commented:
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
 
objectsCommented:
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
 
DanKangAuthor Commented:
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
 
objectsCommented:
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
 
DanKangAuthor Commented:
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
 
DanKangAuthor Commented:
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
 
DanKangAuthor Commented:
I figured out that StringTokenizer parser = new StringTokenizer(aString, "ERROR&"); works fine.
0
 
DanKangAuthor Commented:
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
 
DanKangAuthor Commented:
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
 
DanKangAuthor Commented:
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
 
objectsCommented:
> 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
 
objectsCommented:
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
 
DanKangAuthor Commented:
Is this a "recommended" way?

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

0
 
DanKangAuthor Commented:
objects, Thank you for your help.
0
 
objectsCommented:
no worries :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.