How to convert a string into a stream

Hi,
Actually, I have developed a small piece of code which is working well with the input from the User Console (Via System.in). The console input may be like 10 + 2. Now, I need to pass the input value through my code (Statically) while instantiate. What should i do?
The code is -
public static void main( String[] args )
     throws ParseException, TokenMgrError, NumberFormatException {
          EvalStr2 parser = new EvalStr2(System.in) ; // I need to pass my value as "10+2" instead of System.in
System.out.println("Result-->>"+parser.Start());
Waiting for your help.
Thanks
Venkatesh

Why not simply provide EvalStr2 with an overloaded constructor that takes a String as a parameter?
Also, to learn how to ask a better question, read
[http://catb.org/~esr/faqs/smart-questions.html]
luck, db
edit To post code, use the code tags -- [code]Your Code[/code]will display asYour CodeOr use the code button above the editing area and paste your code between the {code}{code} tags it generates.
Edited by: Darryl.Burke

Similar Messages

  • XML: How to convert xml string into Node/Document

    I have a string in xml form.
    <name>
    <first/>
    <second/>
    </name>
    I want to convert this string into DOM Node or Document.
    How can I do this?
    Any help or pointer?
    TIA
    Sachin

    Try this :
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new org.xml.sax.InputSource(new StringReader(strXml)));Hope this helps.

  • How to convert Java string into XML one?

    With SAX I can parse an xml file, but I should create xml file by hands.
    Ok, it's simple, but how to encode java string into XML constant
    like "Hello & goodby" into "Hello & goodby" ?
    Is there a standard method for such special xml characters?

    If you are creating your XML "by hand" then just make sure your hands know that you have to do that. It isn't difficult to write a Java method to do it, if "by hand" means "in Java code". Otherwise your XML is not well-formed. And as far as I know there is no package that takes ill-formed XML and fixes it up.

  • How to convert a string into orcle date format

    i need some information about converting string into date format.i have string like '20-SEP-05' .so we have to convert into date formate like yy-mm-dd' (2005-09-20).how can we do it.

    Here's some code to help. I can't remember what method uses what format though
      public static java.sql.Date sqlDateValueOf( String dateString ) {
        String stringDate = null;
        if ( dateString == null || dateString.length() < 10 ) return null;
        String strDay = "";
        String strMonth = "";
        String strYear = "";
        if ( dateString.substring(4,5).equals( "-" ) ) {
          strDay = dateString.substring(8, 10);
          if ( strDay.length() < 2 ) strDay = "0" + strDay;
          strMonth = dateString.substring(5, 7);
          strMonth = dateString.valueOf(Integer.parseInt(strMonth) - 1);
          if ( strMonth.length() < 2 ) strMonth = "0" + strMonth;
          strYear = dateString.substring(0, 4);
          stringDate = strDay + "-" + strMonth + "-" + strYear;
        else if ( dateString.substring(2,3).equals( "-" ) ) {
          strDay = dateString.substring(0, 2);
          if ( strDay.length() < 2 ) strDay = "0" + strDay;
          strMonth = dateString.substring(3, 5);
          strMonth = String.valueOf(Integer.parseInt(strMonth) - 1);
          if ( strMonth.length() < 2 ) strMonth = "0" + strMonth;
          strYear = dateString.substring(6, 10);
          stringDate = strDay + "-" + strMonth + "-" + strYear;
        Calendar cal = Calendar.getInstance();
        cal.set( Calendar.YEAR, Integer.parseInt( strYear ) );
        cal.set( Calendar.MONTH, Integer.parseInt( strMonth ) );
        cal.set( Calendar.DAY_OF_MONTH, Integer.parseInt( strDay ) );
        java.sql.Date outDate = new java.sql.Date( cal.getTimeInMillis() );
        return outDate;
      public static String toDateString( java.util.Date date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Date date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Date date, String format) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat( format );
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Timestamp date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      }

  • How to convert a string into an objects name?

    Hi
    I have the following problem;
    I have a string
    String a = "e1"
    and a method
    call(Event e1) that calls the "Event" class with object the name of the string a.
    How can I convert the strings name into an object so as to use it in the "call" method?
    Any ideas?
    Thanks in advance

    I don't know if this helps you, but you can do things like this.
    String a = "com.example.SomeEvent";
    //Instantiate com.example.SomeEvent
    Object o = Class.forName(a).newInstance();SomeEvent must have a non-argument constructor for this to work.
    Class that will be instantiated depens on runtime value of String a, and can be different across different executions of the program.

  • How to convert a string into a collection of KeyEvents

    I'm working on a program that must interact with a 3rd party application (closed to me, I have no access to the code or anything like that). I have been unable to send data to the program by getting its OutputStream. The only thing that has worked was using a Robot object to send individual key events (i.e. robot.keyPress(KeyEvent.VK_A);). Therefore I'm looking to convert a string (many different strings actually) into a collection of their associated KeyEvents so I can send them to the other application. Does anyone know a good way of doing it, or is anyone able to help me with using the OutputStream?
    Thank you!
    (The 3rd party application is Attachmate InfoConnect, a Unisys terminal emulation program that I have to use to access a mainframe).

    Here is a code sample.     public void checknumber(int vsize){
    int total;
    String anum;
    anum = tf2.getText();
    validnumber = true;
    lines2combine = 1;     //default (an integer)
    recs2make = 1;          //default
    try{
    lines2combine = Integer.parseInt(anum);
    catch (NumberFormatException e){
              System.out.println("Entry for lines to combine not numeric");
         validnumber = false;
         d1Title = "Data Entry Error";
         d1Txt = "For number for lines to combine;";
         d2Txt = "Enter a number.";
         doMessage();

  • How to convert a string into double?

    Hi. I'm having a string with 10340.8878 which is essentially a GPS latitude coordinate. I will need to break it up separately into double type 103,40 and 88.
    103 refers to the degree
    40 refers to the minutes
    88 refers to the seconds
    I will also need to perform a double conversion: the second separated double(40) divided by a factor of 60 and the third separated double(88) divided by a factor of 360.
    How can i go about doing this?
    Thanks!

    new Double(str).doubleValue();
    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Double.html

  • How to convert character string into ascii format correctly

    dear all
    I'm using encryption technique in my application(oracle formas 6i). character stream convert to the ascii format and add some numeric value and again convert to the character format
    >>>>
    here I'm using ASCII and CHR functions.
    >>>>
    .net program also using same algorithm to encrypt this password.....finally both the character strings are not equel.(This happens if the character string's ascii value greater than 127. otherwise both are equel).
    pls give me a solution for this
    thanks and regards
    buddhike

    hir dear
    do u want to encrypt & dcrypt your password.which version of oracle you are using
    first store these procedure in Oracle Databases: dbms_obfuscation_toolkit, dbms_crypto, Transparent Data Encryption
    now u can use these procedure to encrypt & dcrypt your password
    if you any query about those procedures you can ask me
    best regard
    amk

  • How to convert a string into xml file

    Hi,
    i have a string . the string contain fully formated xml data. i mean my string like below
    <?xml version="1.0"?>
    <PARTS>
       <TITLE>Computer Parts</TITLE>
       <PART>
          <ITEM>Motherboard</ITEM>
          <MANUFACTURER>ASUS</MANUFACTURER>
          <MODEL>P3B-F</MODEL>
          <COST> 123.00</COST>
       </PART>
       <PART>
    ......................i want to convert it into an xml file
    can u help me?
    thank u

    Thank you Paul Clapham,
    and sorry ..
    i have some other doubt.. regarding xml
    i want to post an XML file from one server(Server_1) to other server.(Server_2)
    To generate an xml file i used DOM in Server_1.
    using xml.transform , StringWriter i converted it into String.
    I post the string to another server and there i will parse it.
    for that i write the code like below in servlet in server_1
    <form name=fn action=http://localhost:8080/server_2/parseXMl.do method=post>
    <input type=hidden name=xmlFile value="+Xmlstring+">
    <input type=submit >is this process is correct?
    Some of the turorial told that use web-services/XML-RPC
    but i new to both..
    I want to complete it using my knowledge only/
    Is it possible to do it?
    Or any other alternative?
    can help me?

  • How to convert a String into a date in yyyy-MM-dd using SimpleDateFormat?

    Hi Guys,
    I am using the following code
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    public class ValiDATE {
         private static boolean validateDateFormat(String strDate) {
              try {
                   SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd");
                   try {
                        formatter.setLenient(false);
                        formatter.parse(strDate);
                   catch (ParseException e) {
                        // invalid date/datetime format          
                        return false;
              catch (Exception ignored) {
              return true;
         public static void main(String args[]){
              System.out.println(validateDateFormat("11111-11-11"));          
    }In the above snippet even if I pass the string parameter as "11111-11-11" it returns a new date and hence prints TRUE in the console, but I want the date to be in yyyy-MM-dd format that is no of years shouldn't exceed 4 digits in all.
    I don't want to use RegEx, I know this can be done using SimpleDateFormat, but would like to know how that can be done.
    I think it has to do something with this . ( [http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#year|http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#year] )
    Regards
    AR

    Java.slogger wrote:
    Yeah I have gone through the javaDoc and it says For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number. The important part is:
    JavaDoc:
    Any other numeric string, such as a one digit string, a three or more digit string, or a two digit string that isn't all digits (for example, "-1"), is interpreted literally.
    Do you know any workarounds else is using a Regex is the best approach?I don't see this as a problem. Everything behaves as designed and as is sensible.
    So if you want to avoid dates with 5-digit numbers, I'd add something like:
    if (parsedDate.after(CUTOFF_DATE)) {
      throw new OhNoesThisIsEvilException("!");
    }

  • How do I convert a string into a decimal

    I've been tryin to convert a string into decimal but I can't get it to work at all. I keep getting an exception.
    I've tried all of these:
    double myDouble = Double.valueOf(string).doubleValue();
    int myint = Interger.parseInt(string);
    float myfloat = Float.parseFloat(string);
    I can't get anything to work.
    That string i'm reading in is an array containing values with decimals. ex: -45.09348
    All the above work fine if there is no decimal, but fails when it comes to one.
    How do I go about doing this?
    Thanks!

    This works for me:String s = "-45.6789";
    double d = Double.parseDouble(s);
    System.out.println(d);Mark

  • How do you convert this String into a double?

    Hi, in this code I want to convert String arg into a double. I'm trying to do this so that I can convert some string into a double in this calculator that I'm making. Here is the code:
    public class par {
    public static void main(String[] args) {
    String str = "351";
    double arg = new double(str); // should convert the string into a double
         System.out.println(arg);
         System.out.println(str);
    I keep getting errors in line 4, if any of you could help me out here I'll be thankful, i'm kinda new to java and my search for java's equivalent of alof() (its a function in C libraries that converts char[] into a double)lead me to this.
    Tony L.

    Use the Following:
    String num = "123.2";
    double d = Double.parseDouble(num);

  • How can I convert a string into an image

    I want to convert a string into an image.
    I want to know required steps for that.

    look at http://forum.java.sun.com/thread.jsp?forum=31&thread=194763
    drawString looks as something you might need

  • How to convert a String to an Icon

    Hello,
    I'm trying to figure out how to convert a String (specifically one letter) to an Icon for a JTree. The program I am working on uses .png files for the icons at the moment, with each .png being a single uppercase letter. However, recent discussion has us wanting to change to something more dynamic, since we anticipate needing new letters in the near future.
    My task is to modify the current codebase so that the Icons used to identify nodes in the tree are created from a Font within code, rather than reading in .png files. I have done some experimenting with creating a Label with just that String, setting the font and trying to extract the image using calls like createImage(int width, int height) and so on, but so far to no avail.
    If I could figure out how to at least convert the String to an image, I could probably make it work (since I can play around with creating an ImageIcon with that image). Thanks in advance for any help.
    Allan

    Here is some quick and dirty code:
    import java.awt.*;
    import javax.swing.*;
    public class BasicIcon implements Icon {
        private String text;
        public BasicIcon(String text) {
            this.text = text;
        public int getIconWidth() {
            return 16;
        public int getIconHeight() {
            return 16;
        public void paintIcon(Component c, Graphics g,  int x, int y) {
            Color old = g.getColor();
            g.setColor(c.getForeground());
            g.drawString(text, x, y+13);
            g.setColor(old);
        public static void main(String[] args) {
            JPanel cp = new JPanel();
            for (char ch = 'A'; ch < 'F'; ++ch) {
                cp.add(new JButton("letter " + ch, new BasicIcon(String.valueOf(ch))));
            JFrame f = new JFrame("BasicIcon");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(cp);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }One thing that could be improved is the vertical positioning of the letter in the Icon.
    The x,y values passed into paintIcon are the upper-left hand corner of the icon,
    but the y value needed by drawString is the position of the String's baseline.
    To do it right, use one of Font's getLineMetrics methods and play around with ascent.

  • How to convert a string-xml placed  inside an xml tag to xml format.

    I have a invoke activity to invoke Web service(code snippet from bpel) .The text in bold is a string that gets formed based on some condition using concat inside while loop .Before calling the WS i need to convert this string into xml
    Any idea how can we do this ?
    <Invoke_A9WS_createObject_InputVariable>
    <part name="parameters">
    <createObject>
    <request>
    <requests>
    <classIdentifier>10141</classIdentifier>
    <data rowId="0">*<number>C0001</number><description>Component from CDM</description>*</data>
    </requests>
    </request>
    </createObject>
    </part>
    </Invoke_A9WS_createObject_InputVariable>
    Request someone to please help .
    Edited by: 869283 on Jul 14, 2011 4:34 AM

    Thanks Dev for your reply ..I have tried using parsEscapedXML but it gives exception
    Fault ID     default/ProjReverseTransform!1.0*soa_6761b853-f458-4b1a-b9c3-6c060cde9350/transform/210018-BpAss3-BpSeq0.3-3
    Fault Time     Jul 15, 2011 1:23:42 PM
    Non Recoverable System Fault :
    <bpelFault><faultType>0</faultType><subLanguageExecutionFault xmlns="http://schemas.oracle.com/bpel/extension"><part name="summary"><summary>XPath expression failed to execute. An error occurs while processing the XPath expression; the expression is oraext:parseEscapedXML('&lt;?xml version="1.0"?>&lt;number xmlns:ns2="http://schemas.oracle.com/service/bpel/common&amp;quot;>C0001 &amp;lt;/number>&amp;lt;description xmlns:ns2=&amp;quot;http://schemas.oracle.com/service/bpel/common">Component from CDM &lt;description>'). The XPath expression failed to execute; the reason was: internal xpath error. Check the detailed root cause described in the exception message text and verify that the XPath query is correct. </summary></part><part name="code"><code>XPathExecutionError</code></part></subLanguageExecutionFault></bpelFault>
    I am using the assign activity as below
    <assign name="Assign_BpelVar">
    <copy>
    <from expression="oraext:parseEscapedXML('&lt;?xml version=&quot;1.0&quot;?>&lt;number xmlns:ns2=&quot;http://schemas.oracle.com/service/bpel/common&amp;quot;>C0001 &amp;lt;/number>&amp;lt;description xmlns:ns2=&amp;quot;http://schemas.oracle.com/service/bpel/common&quot;>Component from CDM &lt;description>')"/>
    <to variable="parameterVar"
    query="/ns2:parameters/ns2:item/ns2:value"/>
    </copy>
    </assign>
    I have harcoded the string here so as to check the working othersie this string gets formed inside a while loop and is stored in a variable.
    parameterVar is variable of type
    <variable name="parameterVar" element="ns2:parameters"/>
    XSD for parameterVar
    <?xml version= '1.0' encoding= 'UTF-8' ?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://schemas.oracle.com/service/bpel/common"
    targetNamespace="http://schemas.oracle.com/service/bpel/common"
    elementFormDefault="qualified">
    <xsd:element name="parameters">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="item" minOccurs="1" maxOccurs="unbounded">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="value">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:any minOccurs="0"
    maxOccurs="unbounded"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>

Maybe you are looking for

  • Create from acrobat x standard fails worked with 9 pro.

    So we have an older app (delphi 6) that worked fine to create pdf with acrobat 9 pro. the user updated to windows 7 machine and got acrobat X standard and so now it fails to pop up the dialog to name the file. As tho it gets hung. So the helpless lit

  • Message counter doesn't display on iphone

    Hi, The messages icon on my iPhone 4 used to display the number of unread messages, this function has stopped working, also there is no sound nor vibration when a message comes in. The only thing I have done recently is reset my network settings beca

  • How to find my class is running in the same machine or not

    Hi, Can any one help me, I have a class called TestClass it has a main method. I want to restrict this class, to run only one occurrence in my system. i.e. If I open 2 command prompts and If I run this class in 2 command prompts, in one command promp

  • How streams replicate to a different table?

    How can I replicate a shared object from source database to another table that has the different name of the source table ? anyone who has this experience please give me a example, thanks .

  • CCM in same client. advantages & drawbacks.

    Hi everybody, I am in a new implementation with CCM 2 as purchasing catalog, and I have a doubt. I have read the landscape options document and I understand when I should use CAT & CSE in different systems. But I don't know which are the reasons for