By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

convert number string to number

P: 7
how can i convert number string to number?(without using parsint)
Jan 5 '11 #1
Share this Question
Share on Google+
9 Replies


Rabbit
Expert Mod 10K+
P: 12,357
Why can't you use parseInt?
Jan 5 '11 #2

nathj
Expert 100+
P: 938
A fair and valid question - this is exactly what parseInt is for. Check out this link: http://www.exampledepot.com/egs/java...onvertNum.html for examples on all the parse... functions.
Jan 6 '11 #3

Prasant jinaga
P: 7
Hi Korea,
If str is a number string then u can convert to number by
int i=Integer.valueOf(str)

Thanks,
Prasant
Jan 6 '11 #4

P: 7
thx u for your help but i am java starter and my teacher sayed me to write code to convert string to number for example :
0ne million there hundred ===== 1.003.000
Jan 6 '11 #5

nathj
Expert 100+
P: 938
As far as I'm aware there are no standard classes to achieve this as valueOf() takes a string like "1003000" and will convert it to an int.

I'm not sure how to parse a proper numeric name (One million and three thousand) into an int.

I would be thinking of a series of loops and checks to parse the string. Interested to hear if anyone else has any ideas on it as it could be an interesting little project.
Jan 7 '11 #6

Rabbit
Expert Mod 10K+
P: 12,357
It all depends on how many different ways you want to be able to capture how someone is going to spell something. Are they going to say thirtyfour, thirty-four, or thirty four. And then there's the question of typos. If you're going to need to capture different ways of typing the same thing, I'm thinking regular expressions. But, seeing as how this is a school assignment, they probably just need to follow a strict format. In which case they can just code specifically for that format.
Jan 7 '11 #7

nathj
Expert 100+
P: 938
RegExp could work. Coding for specifics would be my recommendation and then implementing some sort of exception handling. You could develop your own illegalformatexception class.
Jan 7 '11 #8

P: 7
"""ITSSS MY CODE TO CONVERT NUMBER TO TEXT """""

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* java for convert num to word ;)
* saeed nejahadreza
*/

package test2;

class wtn
{
// az methode split estefade gardad
}

class Ntw{

private static final String[] tensNames = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" };

private static final String[] numNames = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" };

private static String convertLessThanOneThousand(int number)
{
String out;
if (number % 100 < 20)
{
out = numNames[number % 100];
number /= 100;
}
else
{
out = numNames[number % 10];
number /= 10;
out = tensNames[number % 10] + out;
number /= 10;
}
if (number == 0)
return out;
return numNames[number] + " hundred" + out;
}
public static String convert(int number)
{
if (number == 0)
{
return "zero";
}

int millions=0;

int hundredThousands=0;

int thousands=0;

String snumber =Integer.toString(number);

if(number>=10000000)
{
millions = Integer.parseInt(snumber.substring(0,3));

hundredThousands = Integer.parseInt(snumber.substring(3,6));

thousands = Integer.parseInt(snumber.substring(6,9));
}
else if(number>=100000)
{
hundredThousands = Integer.parseInt(snumber.substring(0,3));

thousands = Integer.parseInt(snumber.substring(3,6));
}

else if(number>=10000)
{
hundredThousands = Integer.parseInt(snumber.substring(0,2));

thousands = Integer.parseInt(snumber.substring(2,5));
}

else if(number>=1000)
{
hundredThousands = Integer.parseInt(snumber.substring(0,1));

thousands = Integer.parseInt(snumber.substring(1,4));
}

else
{
thousands = Integer.parseInt(snumber.substring(0,3));
}

String tradMillions;

switch(millions)
{
case 0: tradMillions=""; break;

default: tradMillions=convertLessThanOneThousand(millions) + " million ";
}


String result = tradMillions;

String tradHundredThousands;

switch(hundredThousands)
{
case 0: tradHundredThousands=""; break;

default: tradHundredThousands=convertLessThanOneThousand(hu ndredThousands) + " thousand ";
}

result = result + tradHundredThousands;

String tradThousand;

tradThousand = convertLessThanOneThousand(thousands);

result = result + tradThousand;

return result;
}

}


public class Main {

public static void main(String[] args) {

int num=9746537;
System.out.println( Ntw.convert(num));


}
}
Jan 7 '11 #9

P: 7
now i need help to convert text mood to number mood ;)
Jan 7 '11 #10

Post your reply

Sign in to post your reply or Sign up for a free account.