Link to home
Start Free TrialLog in
Avatar of ukapu2005
ukapu2005

asked on

Can any one provide me java code on string operations.

Can any one provide me java code for the following info.
I have a string variable 'val' that contains following info.

CUSTINFO - "1009","Kapu","Udaya","xyz","abcd","St Louis","USA","63146","3144531654","","uday4777@yahoo.com"
CUSTORDR - "14501","","","300.0","C"

I would need to write java code to find the occurrences of 'CUSTORDR' from val and once it exists,
I would need to grab the value 14501 into some long variable, 300 into some float variable.

Thanks in adavance.
ASKER CERTIFIED SOLUTION
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here is working sample program:

public class N {
  public static void main(String[] args) {
    String val = "CUSTINFO - \"1009\",\"Kapu\",\"Udaya\",\"xyz\",\"abcd\",\"St Louis\",\"USA\",\"63146\",\"3144531654\",\"\",\"uday4777@yahoo.com\"\nCUSTORDR - \"14501\",\"\",\"\",\"300.0\",\"C\"";
    long longValue = 0;
    float floatValue = 0;

    int indx = val.indexOf("CUSTORDR");
    int startInd = 0, endInd = 0;
    if (indx == -1) {
      //no "CUSTORDR" in val
    } else {
        for(int i = indx; i < val.length(); i++) {
            char c = val.charAt(i);
            if (Character.isDigit(c)) {
                startInd = i;
                break;
            }
        }
        for(int i = startInd; i < val.length(); i++) {
            char c = val.charAt(i);
            if (!Character.isDigit(c)) {
                endInd = i;
                break;
            }
        }
        longValue = new Long(val.substring(startInd, endInd)).longValue();
        for(int i = endInd; i < val.length(); i++) {
            char c = val.charAt(i);
            if (Character.isDigit(c) || (c == '.')) {
                startInd = i;
                break;
            }
        }
        for(int i = startInd; i < val.length(); i++) {
            char c = val.charAt(i);
            if (!Character.isDigit(c) && !(c == '.')) {
                endInd = i;
                break;
            }
        }
        floatValue = new Float(val.substring(startInd, endInd)).floatValue();
    }
    System.out.println("longValue: "+longValue+"\nfloatValue: "+floatValue);
  }
}
Avatar of ukapu2005
ukapu2005

ASKER

The above program fails for the following string val. i wanted to get all occurrences of CUSTORDR. Can you please upgrade your program.

val=CUSTINFO - "1009","Kapu","Udaya","xyz","abcd","St Louis","USA","63146","3144531654","","uday4777@yahoo.com"
CUSTORDR - "14501","","","300.0","C"
CUSTINFO - "1009","Kapu","Udaya","xyz","abcd","St Louis","USA","63146","3144531654","","uday4777@yahoo.com"
CUSTORDR - "14502","","","400.0","C"
CUSTINFO - "1009","Kapu","Udaya","xyz","abcd","St Louis","USA","63146","3144531654","","uday4777@yahoo.com"
CUSTORDR - "14503","","","500.0","C"
put a loop around Stillunaware's pgm..

something like..

 int indx = val.indexOf("CUSTORDR");

while (indx>=0)
{
full code

at end
val=val.substring(indx);
indx=val=val.substring(indx);
}//end while

I have given the following string

val=CUSTINFO - "1009","Kapu","Udaya","xyz","abcd","St Louis","USA","63146","3144531654","","uday4777@yahoo.com"
CUSTORDR - "14501","","","300.0","C"

How were you able to get
val = "CUSTINFO - \"1009\",\"Kapu\",\"Udaya\",\"xyz\",\"abcd\",\"St Louis\",\"USA\",\"63146\",\"3144531654\",\"\",\"uday4777@yahoo.com\"\nCUSTORDR - \"14501\",\"\",\"\",\"300.0\",\"C\"";

Is there any standard way to convert?  Thanks in advance. Your help in this regard is appreciated.
Where do You get the string from?

If You enter it manually into Your code, than it must start and end with " character, to have this character in a middle of a string You have to escape it with backslash \"
Avatar of CEHJ
This looks like homework  ...