473,434 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

convert number string to number

how can i convert number string to number?(without using parsint)
Jan 5 '11 #1
9 3860
Rabbit
12,516 Expert Mod 8TB
Why can't you use parseInt?
Jan 5 '11 #2
nathj
938 Expert 512MB
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
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
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
938 Expert 512MB
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
12,516 Expert Mod 8TB
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
938 Expert 512MB
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
"""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
now i need help to convert text mood to number mood ;)
Jan 7 '11 #10

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

Similar topics

3
by: Chua Wen Ching | last post by:
I have a problem. But on .NET 1.1 My Scenario: Actually I will have a string of hexadecimals read from a xml file. Then from the hexadecimals, i will add 1 value whenever i made any...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
11
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.