Home Answers Viewqa Java-Beginners string to double

Have Programming Question? Ask it here!

Cathy Dinh
string to double
0 Answer(s)      3 years and 6 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,
x = parseInt(input);
JOptionPane.showMessageDialog(null,
"Twice of that number is "+ 2*x);

}

}

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
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
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
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
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
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
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
How to fill double array in java
How to fill double array in java  how to fill double array in java
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
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... String addSpaceToRight(String s, int n) { return String.format("%1\$-" + n + "s", s); } public static String addSpaceToLeft(String s, int n) { return
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   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  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
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(" "); for(int i=0
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
String  How to Convert sunnapu gopal to Sunnapu Gopal in java using String
String
String  write a program using string it should replace 'c'char to integer number as 1 in whole source
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 to accept the string and store the reverse stream into another array and print
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
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
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
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 http
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
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