string to double

string to double

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 Tutorials/Questions & Answers:
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
Advertisements
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
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
Convert String To Double
Convert String To Double       In this section, we are going to convert a numeric type string value into a double. The double supports big-integer value.  Code Description:ADS
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... The program  takes a variable of double type and convert it into the string form
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
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.....
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
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
string
string  String helloString = new String(helloArray); System.out.println(helloString); i am unable to understand this. could u plz explain
string
string  a java program using string function to input any string... ArrangeStringAlphabetically { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter string
String
String  how to add spaces with string functions.?   Hi... { public static String addSpaceToRight(String s, int n) { return String.format("%1$-" + n + "s", s); } public static String addSpaceToLeft(String s, int n
String
characters in string?   import java.util.*; class RemoveDuplicateCharatcersFromString { public static String removeDuplicates(String s... < s.length(); i++) { String st = s.substring(i, i + 1
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function
string
string  a java program to input a string and display the string...*; import java.io.*; public class FirstLetter{ public static String capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str
string
*; class ExtractWords { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter String: "); String st=input.nextLine(); String str[]=st.split
string
string   just i want to a program in a short form to the given string in buffered reader for example input string: Suresh Chandra Gupta output: S. C...; public class StringTest { public static void main(String [] args
string
string  write a program to accept the string and store the reverse stream into another array and print
string
string  java prgm to find total occurence of a given string pattern in a sentence
string
string  java prgm to find total occurence of a given string pattern in a sentence
String
String  write a program using string it should replace 'c'char to integer number as 1 in whole source
String
String  How to Convert sunnapu gopal to Sunnapu Gopal in java using String
String
String  how to print in between numbers if the question "String s = "1,2,3,4,5,6,8,-25"" out must be 1 2,3,4,5,6,7,8,9,10,upto25
string
and also displaying that contain word? Like I want to find "a" in the string... and a character. It then count the occurrence of that character in the string and display... String_Example { public static void main(String[] args
string
string  program for String reverse and replace in c,c++,java e.g.Input:10,20,hello output:hello;20;10   Hi Friend, Try the following java...(String[] args) { String array[]=new String[5]; Scanner input
string
string  a java program using string function that computes your initials from your full name and display them   Please visit the following links: http://www.roseindia.net/tutorial/java/core/printInitials.html
string
string  a java program using string function and access modifiers to automate the insurance company which will have to manage the information about policy holders Data Members: 1)Policy No 2)Name of policy holder 3)Address 4
Convert Integer to Double
of a double object. It converts a double into a string and also vice-versa. Here... into a double. The the java.lang package provides the functionality for converting an integer type data into a double. Code Description:ADS_TO_REPLACE_1
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
ModuleNotFoundError: No module named 'double'
ModuleNotFoundError: No module named 'double'  Hi, My Python... 'double' How to remove the ModuleNotFoundError: No module named 'double'... to install padas library. You can install double python with following command
ModuleNotFoundError: No module named 'double'
ModuleNotFoundError: No module named 'double'  Hi, My Python... 'double' How to remove the ModuleNotFoundError: No module named 'double'... to install padas library. You can install double python with following command
Convert Object to Double
object value and the double value represented by the parameter string (str... Convert Object to Double     ... into a double.  Code Description:ADS_TO_REPLACE_1 The given program helps
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
Incrementing a Double by the Smallest Possible Amount
{ public static void 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
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
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
Struts 2 double validator
Struts 2 double validator       The Double validator of Struts 2 Framework checks if the given input is double or not. If the input is not double, it generates the error message. Double
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
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 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

Ads