Home Answers Viewqa Java-Beginners string to double

 
 


Cathy Dinh
string to double
0 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners

So I have an assignment where we have to convert strings into double.I have to modify the method parseDouble() to fully implement the method to imitate the Double.parseDouble() method of Java. In other words, parseDouble() method takes a String as input and converts it into a double when possible and returns it. If the string cannot be converted into a positive double, it returns -1.You will also modify the main method to ask the user to input a positive double in a JoptionPane, then use your parseDouble method (not Java's built-in parseDouble method) to convert it to a double and return twice the value of the input, or a message that the input is not a valid positive number.

and this is my code: I just can't figure out if I did it right or not

import javax.swing.JOptionPane;

public class Parsing {
    public static int parseInt(String value) {
        int x = 0;
        int digit = 0;
        for (int i=0; i<value.length(); i++) {
            switch (value.charAt(i)) {
            case '0':
                digit = 0;
                break;
            case '1':
                digit = 1;
                break;
            case '2':
                digit = 2;
                break;
            case '3':
                digit = 3;
                break;
            case '4':
                digit = 4;
                break;
            case '5':
                digit = 5;
                break;
            case '6':
                digit = 6;
                break;
            case '7':
                digit = 7;
                break;
            case '8':
                digit = 8;
                break;
            case '9':
                digit = 9;
                break;
            default:
                return -1;
            }
            x = x*10 + digit;
        }
        return x;
    }

    public static double parseDouble(String value) {
        double x = 0;
        int digit = 0;
        boolean beforeDecimal = true;
        int factor = 0;
        for (int i=0; i<value.length(); i++) {
            switch (value.charAt(i)) {
            case '0':
                digit = 0;
                break;
            case '1':
                digit = 1;
                break;
            case '2':
                digit = 2;
                break;
            case '3':
                digit = 3;
                break;
            case '4':
                digit = 4;
                break;
            case '5':
                digit = 5;
                break;
            case '6':
                digit = 6;
                break;
            case '7':
                digit = 7;
                break;
            case '8':
                digit = 8;
                break;
            case '9':
                digit = 9;
                break;
            case '.':
                beforeDecimal = false;
                break;
            default:
                return -1;
            }
            //change the code below
            if (beforeDecimal) {
                x = x*10 + digit;
            }
            //else



        }
        return x;
    }
    public static void main(String[] args) {
        //declare variables
        String input;
        int x;
        // Ask user for an integer
        input = JOptionPane.showInputDialog(null, 
                    "Please input a positive integer");
        x = parseInt(input);
        JOptionPane.showMessageDialog(null, 
                "Twice of that number is "+ 2*x);

    }

}
View Answers









Related Pages:
string to double
string to double   So I have an assignment where we have to convert..., parseDouble() method takes a String as input and converts it into a double when possible and returns it. If the string cannot be converted into a positive double
converting string to double in java
converting string to double in java  Please post an example to converting string to double in java. Thanks!   Convert String to Double Tutorial
How to convert String double quotos to String double double quotes(
How to convert String double quotos to String double double quotes(  Hi How to convert String double quotos to String double double quotes("----->"") By replace? Problem is: String column is "Age is 60" now . whenver
Convert String To Double
Convert String To Double      ... numeric type string value:" message and it converts into a double... StringToDouble Enter string value: 23 double d = 23.0
Conversion from String to double
Conversion from String to double: In this tutorial we will learn how to convert a string type data to double type data. Description: This program will take a String value from mystring variable. The line double mydouble
Conversion from double to String
Conversion from double to String: In this tutorial we will learn how to convert a double type value to String type value. Description: This program will take a double value from console and provide the conversion to String type
Convert Double To String
Convert Double To String      ...; This section learns you the conversion of double into string. The following program... a variable of double type and convert it into the string form by using toString
Making XY line chart with string and double axis
Making XY line chart with string and double axis  Good evening... into the dataset. The horizontal axis are going to be represented by a string (showing day and time) and the vertical indicates temperature (double) Now my question is what
java.lang.String.valueOf(double dnum)
The valueof(double dnum), is the Java String Class method, which creats and holds the double value in String. And the "dnum" converts the double value to String. See the example below... a simple example of valueOf(double dnum) method
Double To Int Without TypeCasting
void main(String[] args) { double d=25.5; int num=new Double(d).intValue(); System.out.println(num...Double To Int Without TypeCasting  How to convert Double to Int
single quotes or double quotes
single quotes or double quotes  Would you initialize your strings with single quotes or double quotes?   Hi friends, Both are good but I would prefer & recommend that we go for initializing string with single
Struts 2 double validator
 extends ActionSupport{   private double percentagemarks;   public String...Struts 2 double validator       The Double validator of Struts 2 Framework checks if the given input is double
The double Keyword
The double Keyword       The double is a Java keyword that may not be used as identifiers i.e. you... to declare a variable, an expression or a method return value of type double
Convert Integer to Double
() method. This method returns a string representation of a double object. It converts a double into a string and also vice-versa. Here is the code... into a double. The the java.lang package provides the functionality
Conversion from short to double
Conversion from short to double: In this tutorial we will learn how to convert a short type data to double type. Description: This program will take a short type value from console and provides a conversion to double type. The line
Convert Object to Double
and the double value represented by the parameter string (str). Here is the code... Convert Object to Double     ... into a double.  Code Description: The given program helps you
Place array of double in a buffer
(String[] args) {     double[] d = ...Place array of double in a buffer In this tutorial you will see how to place array of double in buffer. The put method of DoubleBuffer class
Java Write To File Double
Java Write To File Double In this tutorial you will learn how to write to file primitive type double. Write to file primitive data type double there is a class DataOutputStream that provides a writeDouble() method to write a double
Incrementing a Double by the Smallest Possible Amount
main(String[] args) { double d = 1.5; double incd...Incrementing a Double by the Smallest Possible Amount In this section, you will earn how to increment the double value by the smallest possible amount
Conversion from int to double
Conversion from int to double: In this tutorial we will learn how to convert an int type value to double type data. Description: This program will take... to double type data. The line int a = Integer.parseInt(buffreader.readLine()); is used
Conversion from double to short
Conversion from double to short: In this tutorial we will learn how to convert a double type value to short type value. Description: This program will take a double value from console and provide the conversion to short type
Conversion from double to float
Conversion from double to float: In this tutorial we will learn how to convert a double type value to float type value. Description: This program will take a double value from console and provide the conversion to float type
Conversion from double to byte
Conversion from double to byte: In this tutorial we will learn how to convert a double type value to byte type value. Description: This program will take a double value from console and provide the conversion to byte type. The line
Conversion from double to int
Conversion from double to int: In this tutorial we will learn how to convert a double type value to int type value. Description: This program will take a double value from console and provide the conversion to int type. The line
Conversion from double to long
Conversion from double to long: In this tutorial we will learn how to convert a double type value to long type value. Description: This program will take a double value from console and provide the conversion to long type. The line
Conversion from double to char
Conversion from double to char: In this tutorial we will learn how to convert a double type value to char type value. Description: This program will take a double value from console and provide the conversion to char type. The line
Conversion from byte to double
Conversion from byte to double: In this tutorial we will learn how to convert a byte type value to double type value. Description: This program will take a byte value from console and provides a conversion to double type data
Conversion from float to double
Conversion from float to double: In this tutorial we will learn how to convert a float type value to double type value. Description: This program will take a float value from console and provides a conversion to double type data
Conversion from double to boolean
Conversion from double to boolean: In this tutorial we will learn how to convert a double type value to boolean type value. Description: This program will take a double value from console and provide the conversion to boolean type
Conversion from long to double
Conversion from long to double: In this tutorial we will learn how to convert a long type value to double type value. Description: This program will take... value from console. The line double mydouble = (double)(mylong); converts
Wraps a double type array into a buffer
(String[] args) {     double[] ch = ...Wraps a double type array into a buffer In this tutorial you will see how to wraps a double array into a buffer. The public static DoubleBuffer wrap
Example to show double exception in java
in an object. Double exception occurs in java when the string or primitive data type... Example to show double exception in java       The Tutorial describe you a code from double
Get the capacity of a double type buffer
Get the capacity of a double buffer In this tutorial we will see how to get size of a double buffer using capacity() method. Code...;DoubleBufferDemo {   public static void main(String[] args
How to convet reader to double? - Java Beginners
How to convet reader to double?  I have this set of coding which read...) into Double so that the input can perform arithmetic operation...(String[] args) { portList = CommPortIdentifier.getPortIdentifiers
Compare a double type buffer to another object
(String[] args) {     double[] dbl = ...Compare a double type buffer to another object In this tutorial you will see how to compare double buffer to another object. The compareTo method
Writes the given double value into a buffer at the given index.
Writes the given double value into a buffer at the given index. In this tutorial you will see how to write the given double into buffer at the given index. The put (int index, double d) allow to write a character at particular index
Compare two double type buffer's content
Compare two double buffer's content In this tutorial we will see how to create a double buffer and put content into it and then compare double value...;public static void main(String[] args) {    
Criteria Double Value Compare Example
Criteria Double Value Compare Example In this Example, We will discuss about...;'like' and 'gt'. In This example we compare double data type... class CriteriaDoubleValueCompare { public static void main(String[] args
Creates a view of this byte buffer as a double buffer.
Creates a view of this byte buffer as a double buffer.  In this tutorial, we will see how to creates a view of byte buffer as a double buffer... of byte buffer as a double buffer. int limit
Get the smallest double greater than the given value
Get the smallest double greater than the given value This section demonstrates you how to get the smallest double value greater than the given value. Java... of numbers. It maps numerical ranges to string. Here we have to find the new
Problem with Double type Value in Access Database - Java Beginners
Problem with Double type Value in Access Database  Hello sir I have store Double Type Value in MS Access Databse,but when I search Record... class Test extends JFrame{ public static void main(String[]args){ try
NSString to double
NSString to double  Hi, I want to convert NSString value into double. Please give me good code example for this conversion. Thanks   Hi... scannerWithString:@"1.309"]; double dblLat; [strLat scanDouble:&
NSString to double
NSString to double  Hi, I want to convert NSString value into double. Please give me good code example for this conversion. Thanks
String valueOf()
the string representation of a double, float, short, byte, integer, long etc. As shown... String valueOf()      ...() method of String class. We are going to use valueOf() method of String class
Java String Datatype
Java String Datatype  Weather String in java is class or Datatype,if it's clss then How can i assign String str="hello";   String is a class found in the package java.lang.*. When we represent string in double quotes
Check for number of elements exists between the current position and the limit of a double type buffer.
;void main(String[] args) {     double[] db1... and the limit of a double type buffer. In this tutorial you will see how to check for existence of any element between the current position and the limit of a double
Intro Java help with Double
Intro Java help with Double  I have to evaluate a math expression using double in Java. I can input the code fine for the expression, but I don't know how to view the answer that it gives me. help
STRING.....
STRING.....  plzz sent me d code for counting vowels in a string... gui programme
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both... "String args[]" can mean a "string array called args which is an array
string
string  String helloString = new String(helloArray); System.out.println(helloString); i am unable to understand this. could u plz explain

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.