Converting a string to the corresponding value of type double

Hey, im a really noob at this, but I need some help with this question. I've got to complete this in order to progress to my second year although im not doing programming and will not need it in the second year. The question is as follows:
"Write a method called convertDouble as follows:
public double convertDouble (String value) {
This method should convert its parameter (a string like "3.1415") to the corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method Double.parseDouble() to do the hard work for you. "
To be honest I dont really have a clue what how to write this so any help would be much appreciated. Cheers guys

Well the simplest way is to use Java's already inbuilt converter:
which is if you have a string S which represents a double i.e 3.142
You can use
double number = Double.parseDouble(S); and thats it.
The other way to do it is to loop through the string and check whether it is a number
by using Character.IsDigit(ch) ? or somethign similar.
and if it is a digit then subtract a fixed amount depending on its ASCII value.
so if character with a value of 40 represents 0 (im not sure if it does) then
any Digit character - 40 would give you the decimal value.
Please ask questions if i dont make sense.
Edited by: z0rgy on Aug 18, 2008 7:36 AM

Similar Messages

  • How to copy the corresponding values other fields in  Search help

    Hi All,
    My selection screen contains 5 fields. three are obligatory parameters, one date field and one select option.
    All are from the same table . I had created search help for one field .
    when I'll click the hit list of the search help I want the corresponding values to get copied to other fields in the selection screen, as my hit list contain values for all those fields.
    How Can I copy those values.
    Please help me on this.

    Hi,
    Please try this.
    In the search help set the export parameter for the other fields also.
    Thanks
    Naren

  • Reading Excel sheet the corresponding value

    the corresponding value of another colum should be read from excel sheet .
    soppose there are 4 coloumns and i enter the value of column one then colun 2,3,4 value should be read
    Thanks
    Sharad.

    Duplicate post.

  • How to convert a string to a currency value ? getting a dump with exception

    Hi,
    I am getting a exception not caught in CX_SY_CONVERSION_NO_NUMBER.
    I am trying to convert a string value with ',' to a currency value.
    I tried using Replace and Condense. But the error still persists.
    Is there a FM or a casting that I can use?
    Cheers
    Kiran

    Hi,
    Sorry I got my question wrong. I have a problem - that when I'm trying to pass a value from a string to a currency field.
    But, the currency field is a field symbol.
    so, I have
    data abc type string.
    abc = "5345"
    <curr_val> = abc.
    now <curr_val> = 0.000
    Please suggest.

  • Converting a string to an integer value

    How can I convert a string to an integer?

    sorry, not "of new Integer.." but "or new Integer(.."
    Message was edited by:
    s-e-r-g-e

  • How to add the resultant value in a double

    Hay guys
    I am adding two vectors (each vector has 3000 values). I want to add them together and store in a double variable. But when i compile i have an error. (incompatible type found, Object required.)
    How can i store the values of two vector in an object
    here is my peice of code
      public void result (){
            double temp1;
            for(int i=0; i<=  3000;i++){
            temp1 = (double)v.elementAt(i) + (double)v1.elementAt(i);
      NOTE: i already have 3000 values (or objects) in my vectors
    Thanks alot in advance

    Okay guss. i Used (Double) in my vectors as object because with lowercase (double) it throws error. Asbestos you said (What are the objects in the vectors? Are they Doubles (with a capital 'D')? If so, use the methods found in the Double API to change the Doubles into doubles, instead of trying to cast them.) i tried but i didnt able to findout how to use that API. Can anybody show me
    Here is my full code.
    public class Simple {
        public long c=0;
        public long a = 16807;
        public long m = 2147483647;
        public long  XO = 238640;
        public long  XO1 = 248921;
        public Vector v = new Vector();
        public Vector v1 = new Vector();
        public Vector vLast = new Vector();
        public double x = 0, R = 0;
        public double LCDx (){
        for(int i=0; i<= 3000;i++){
        x = (((a)*(XO))+c)%m;
        R = x/m;
        XO = (long)x;
        v.addElement(new Double(R));
        System.out.println(i+" Value: "+R);
        return R;
        public double LCDy (){
        for(int i=0; i<=  3000;i++){
            x = (((a)*(XO1))+c)%m;
            R = x/m;
            XO1 = (long)x;       
            v1.addElement(new Double(R));
        return R;
      public void result (){
            double temp1;
            for(int i=0; i<=  3000;i++){
    // PROBLEM PART
             double temp = (double)v.elementAt(i) + (double)v1.elementAt(i);
              vLast.add(temp); 
      }Thanks alot in advnacee

  • F:convertNumber with type="percent" and value of type double.

    I got a problem: when trying to use f:convertNumber with type "percent" to convert number of type double I get wrong number. ex.:
    Actual number is 5, but I get 500,00% instead of 5,00%
    Is it a bug or I'm doing something wrong?

    Sorry.
    Just realized, that according to java.text.DecimalFormat value is multiplied by 100.

  • The Best Way to Assign an Enum to the Corresponds value of a String?

    I am reading from and xml file configuration file. The data being returned is stored as a String and corresponds to a Enum value. My question is - What is the best way to assign the enum the value that corresponds to value of the string?
    public enum Names { BOB, FRED, JIM, SAM }
    String name = "FRED";   // This is the data retuned from the xml file.
    Names n =              // I want to assign n the value of Names.FREDI know I could do an "if" statement. However, this is a contrived example with only a few enum values. In the real world, this enum list could be quite large.
    I have looked into reflection, but I don't know whether there is an easier way to achieve my objective?
    Thank you for your time,
    Harold Clements

    dcminter wrote:
    But for looking up "how do I do X with a subclass of type Y", I think the API documentation should be able to answer that.I think I disagree when it's a language feature.That's definitely a point worth observing.
    By that reasoning you'd expect to populate the API docs with definitions for class (and this and super) all over the place.I think you're exaggerate, and you know it ;-)
    If those were to be mentioned in the JavaDoc, they should obviously go into the documentation of java.lang.Object only.
    I suppose you could have a "synthetics" section to the docs in much the same way that the inherited methods and fields are defined but it doesn't seem very useful.It's not useful if it's just clutter that's the same everywhere, of course.
    But in this particular case a simple "note that enum classes have a syntetic valueOf() method that does foo bar baz" in the body of the class documentation would have been a nice guesture (especially since that part is pretty small here anyway).
    And yes, I also think that the length field of arrays should find some mention in the JavaDoc. I don't know how and where, 'though.

  • Using sql bulk copy throwing exception -The given value of type String from the data source cannot be converted to type int of the specified target column

    Hi All,
    I am reading notepads files and inserting data in sql tables from the notepad-
    while performing sql bulk copy on this line it throws exception - "bulkcopy.WriteToServer(dt); -"data type related(mentioned in subject )".
    Please go through my  logic and tell me what to change to avoid this error -
    public void Main()
    Dts.TaskResult = (int)ScriptResults.Success;
    string[] filePaths = Directory.GetFiles(@"C:\Users\jainruc\Desktop\Sudhanshu\master_db\Archive\test\content_insert\");
    for (int k = 0; k < filePaths.Length; k++)
    string[] lines = System.IO.File.ReadAllLines(filePaths[k]);
    //table name needs to extract after = sign
    string[] pathArr = filePaths[0].Split('\\');
    string tablename = pathArr[9].Split('.')[0];
    DataTable dt = new DataTable(tablename);
    |
    string[] arrColumns = lines[1].Split(new char[] { '|' });
    foreach (string col in arrColumns)
    dt.Columns.Add(col);
    for (int i = 2; i < lines.Length; i++)
    string[] columnsvals = lines[i].Split(new char[] { '|' });
    DataRow dr = dt.NewRow();
    for (int j = 0; j < columnsvals.Length; j++)
    //Console.Write(columnsvals[j]);
    if (string.IsNullOrEmpty(columnsvals[j]))
    dr[j] = DBNull.Value;
    else
    dr[j] = columnsvals[j];
    dt.Rows.Add(dr);
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=UI3DATS009X;" + "Initial Catalog=BHI_CSP_DB;" + "User Id=sa;" + "Password=3pp$erv1ce$4";
    conn.Open();
    SqlBulkCopy bulkcopy = new SqlBulkCopy(conn);
    bulkcopy.DestinationTableName = dt.TableName;
    bulkcopy.WriteToServer(dt);
    conn.Close();
    Issue 1:-
    I am reading notepad: getting all column and values in my data table now while inserting for date and time or integer field i need to do explicit conversion how to write for specific column before bulkcopy.WriteToServer(dt);
    Issue 2:- Notepad does not contains all columns nor in specific sequence in that case i can add few column ehich i am doing now but the issue is now data table will add my columns + notepad columns and while inserting how to assign in perticular colums?
    sudhanshu sharma Do good and cast it into river :)

    Hi,
    I think you'll have to do an explicit column mapping if they are not in exact sequence in both source and destination.
    Have a look at this link:
    https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopycolumnmapping(v=vs.110).aspx
    Good Luck!
    Kaur.
    Please mark as answer if this resolves your issue.

  • Strings: Finding the index value of the 1st alphabet character

    Hi
    I'm trying to find a way of finding the index position of the 1st alphabetic character i.e. letters from "a" to "z" in a string line.
    I've tried looking at the java doc pages on the String Class which contains methods such "indexOf" but i'm very confused on how this all works??
    Could someone please help me.
    Thanks
    Rahul

    Another way is to use regular expressions:
    import java.util.regex.*;
    class RegExTest {   
        public int indexOfRegEx(String regex , String inString){
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(inString);
            boolean found = m.find();
            return found ? m.start() : -1;
        public static void main(String[] args) {
            RegExTest test = new RegExTest();
            System.out.println(test.indexOfRegEx("[A-Za-z]", "a124321"));
            System.out.println(test.indexOfRegEx("[A-Za-z]", "12A4321"));
            System.out.println(test.indexOfRegEx("\\p{Alpha}", "124321"));
            System.out.println(test.indexOfRegEx("\\p{Alpha}", "124321a"));
    }

  • How can I convert a string of hex values to a hex format string programatically?

    Is there a way to convert a string of the following format:
    1400010107070D0305006A01 ........           ("Normal display" string)
    programatically to:
    1400 0101 0707 0D03 05006A01 ..........      ("Hex display" striing)
    I need to do this in order to calculate a CRC16 value.
    See attached VIs
    Thank you.
    Solved!
    Go to Solution.
    Attachments:
    CMM_SN_MULTI.vi ‏50 KB
    CMM_CRC16_Calculator.vi ‏23 KB

    You can iterate over the string and use the String to Hex VI. If you work with two bytes of the string at a time you can get a U8 array of the desired binary values. Then when you are complete you can either work with the byte array of convert it back to a string using Byte Array to String.
    EDIT: GerdW typed faster than I did.
    Message Edited by Mark Yedinak on 03-18-2010 02:55 PM
    Mark Yedinak
    "Does anyone know where the love of God goes when the waves turn the minutes to hours?"
    Wreck of the Edmund Fitzgerald - Gordon Lightfoot

  • Given a string, how do you find the numeric value in sparse ring?

    Situation: I receive the string value of the sparse ring, and a control reference to the same type of control, but not the control that sourced the string. How can I convert the string, into the actual numeric value of the ring? For example:
    Using the "VISA Select Event Type" ring (Functions -> Instrument I/O -> VISA -> Advanced -> Event -> Select Event Type.ctl), lets say I get the string "ServiceRequest" how do I convert that into 1073684491, the actual value of the ring?
    This is a rework of an existing function that worked fine with "normal" rings.
    Thanks.

    Unfortunately this method will not work, its a generic routine that must be able to take any ring, so I can't hardwire the cases to known outputs. And, of course, searching the array of Strings[] will just find me a useless index.
    My interm solution is this:
    1) pick a small, but likely range of possible values for the ring
    2) iterate through these values, assigning each one in turn to ring
    3) read the RingText property to see if it matches the passed string
    4) if it does, then exit. If it doesn't keep looping.
    5) if no value produces a matching string, declare defeat (raise an error or produce other output that indicates a match could not be found).
    The routine only runs in the UI so I am not too concerned about the time delay this method will i
    mpose, but I would greatly appreciate a smoother solution.
    Thanks for your suggestions.

  • Converting String to the Number

    Dear Members,
    I'm new to OAF.
    I have a requirement in which I need to convert a string to the number.
    Can any one please help me how to achieve this in OAF?
    Regards.

    Please import oracle.jbo.domain.Number;
    Then include the conversion in a try catch block
    try
    Number numValue = new Number (stringVal);
    catch(SQLException e)
    e.printStackTrace();
    Regards
    Sumit

  • How to convert binary string to ascii

    Hi,
    See i have a string like this,
    String str = "10011100110001011001110011000101";
    Now i want to find exact ascii value of the binary string.
    Thanks in advance.

    First want to convert these bits to corresponding
    characters and then have to find ascii of the same.... corresponding to what? I think converting these "bits" to the corresponding chars is the whole story. But what about the original format?
    Guessing mode on ... does this help?
    String input = "...";
    StringBuffer result = new StringBuffer();
    for (int i = 0;i < input.length();i += 8) {
      result.append((char) Integer.parseInt(input.substring(i, i + 8), 2));
    System.out.println(result);

  • Can I convert a string to a JComponent

    If I have a JButton named "exitButton" and I have a String with the text value = "exitButton" is there any way to convert this string into an object that I can then use to perform actions with, for example:
    exitButton.isEnable(false);

    Hi, if you have only a feew buttons/components and there know at compiletime. you can make a vectro containing a keypair containign the string and the button if you don't want to do it this way i believe you can do it using the reflection package it can do taht (i believe) never used it myslef but have a look at the reflection api.
    Hope This Helps You,
    Robert

Maybe you are looking for

  • HTTP 503 Service Unavailable Error in SAP XI 3.0

    Hi All,    I am doing a simple File -> File scenario which picks up a .txt file from a folder and post it to the same folder .My file is getting picked up.But I am getting an error <SAP:Stack>HTTP response contains status code 503 with the descriptio

  • MainStage 2.0.1 released

    This update improves overall performance and provides numerous fixes and enhancements, including the Loopback and Playback plug-ins: Issues addressed include: • The Reverse option for Loopback now behaves consistently • Pitchbend data is transmitted

  • SOAP adapter Header data

    This is SOAP to PROXY We are getting userid information in SOAP Header . This information has to be captured and pass to receiver. 1) How to access the SOAP header data 2) Can I Map it to target strcture, if so how target structure shud be formed. Pl

  • Import data from text file to a table using t-sql

    Hi, I am trying to import data from a text file to a table using the below query but it is returning an error. SELECT *, 2 FROM OPENROWSET(BULK 'W:\file.txt', FORMATFILE = 'W:\format_file.xml', FIRSTROW = 1, LASTROW = 1) as f The error is: Bulk load

  • Why cant i get a facetime call to connect?

    I cant get my mac book or iphone to connect a facetime call why please help?