Welcome Guest [Log In] [Register]
Welcome to JGames Forums. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Problem with a class; Problems with converting string to int
Topic Started: Mar 22 2009, 07:40 PM (66 Views)
Albrad
I posted
[ * ]
Hey guys, I'm having problems with this project of mine of converting strings to ints.

Everything works up to the hundreds. The problem is not syntax error but the way that the class solves the problem.

If you want to run the file you would still need 3 more files ( Driver and number lists). Let me know if you want the extra files.

There is a comment at the part of the code thats giving me problems.

here is the code:

//Program ID Name: Word To Number Converter Class
//Written by: Alan B. Autrand
//Date Created: February 25, 2009.
//Last updated: March 22, 2009.

/* Description:
The purpose of this class is to convert a string that represents a number into an actual integer.
Example:

input output
seventy two = 72

The way the class works is by adding all the numbers on a stack.
Ex. fifty nine = 50 + 9 = 59.

*/
import java.util.*;
import java.io.*;

public class WordsToNums {

//Data

ArrayList<String> oneToTwenty = new ArrayList<String>();
ArrayList<String> nullToNinety = new ArrayList<String>();
ArrayList<String> hundredToBillion = new ArrayList<String>();
ArrayList<Integer> nums = new ArrayList<Integer>();
Scanner kb = new Scanner(System.in);
Stack numStack = new Stack();
boolean again = false;
String theLine;
String in;


//The constructor is working properly
//Constructor
public WordsToNums()
{
//Puts in the oneToTwenty Arraylist the list of numbers from the file called "oneTwenty.txt"
try
{
Scanner inputStream = new Scanner(new FileInputStream("oneTwenty.txt"));
int count = 0;
while (inputStream.hasNextLine())
{
theLine = inputStream.nextLine();
count++;
oneToTwenty.add(theLine);
}
}
catch(Exception e)
{
System.out.println(e);
}

//Puts in the nullToNinety Arraylist the list of numbers from the file called "nullNinety.txt"
try
{
Scanner inputStream = new Scanner(new FileInputStream("nullNinety.txt"));
int count = 0;
while (inputStream.hasNextLine())
{
theLine = inputStream.nextLine();
count++;
nullToNinety.add(theLine);
}
}
catch(Exception e)
{
System.out.println(e);
}

//Puts in the hundredToBillion Arraylist the list of numbers from the file called "hundredToBillion.txt"
try
{
Scanner inputStream = new Scanner(new FileInputStream("hundredToBillion.txt"));
int count = 0;
while (inputStream.hasNextLine())
{
theLine = inputStream.nextLine();
count++;
hundredToBillion.add(theLine);
}
}
catch(Exception e)
{
System.out.println(e);
}
}



//Methods
public int convert(String a)
{
StringTokenizer tokenizer = new StringTokenizer(a, " ");
String tempStack;
int index = 0;
String token;
int temp = 0;
int num= 0;

//Puts in a stack all the elements from the StringTokenizer.
while(tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();

if(oneToTwenty.contains(token) || nullToNinety.contains(token) || hundredToBillion.contains(token))
numStack.push(token);
}

//Converts each number on the stack to an integer.
while(!numStack.isEmpty())
{
tempStack = (String)numStack.pop();

if(oneToTwenty.contains(tempStack))
{
temp = oneToTwenty.indexOf(tempStack);
temp = temp + 1;
}
else if(nullToNinety.contains(tempStack))
{
temp = nullToNinety.indexOf(tempStack);
temp = ((temp + 1) * 10);
}
else if(hundredToBillion.contains(tempStack)) //HERE:The problem is this entire if statement. I just dont know how to change the class so it multiplies the number properly.
{
index = hundredToBillion.indexOf(tempStack);

if(index == 0)
temp = temp * 100;
else if(index == 1)
temp = temp * 1000;
else if(index == 2)
temp = temp * 1000000;
else if(index == 3)
temp = temp * 1000000000;
}

num = num + temp;
}

return num;

}
}

Offline Profile Quote Post Goto Top
 
JGames
Member Avatar
Mega Man!!!

I think we need the files to compile it and see how it works. You said it isn't a syntax error and the syntax looks correct so it is probably a logic error. The code tag may be useful too. It is funny though because we are just now learning stacks and tokenizers in 205. I have a Stack related program due next Monday as well.
Edited by JGames, Mar 22 2009, 07:58 PM.
Posted ImagePosted Image
Posted Image
"Always make the best of life's situations."
KHI Game Soundtracks
Galbadia Hotel Game Soundtracks
Offline Profile Quote Post Goto Top
 
Albrad
I posted
[ * ]
here are two of the number lists.
Attached to this post:
Attachments: hundredToBillion.txt (35 Bytes)
Attachments: nullNinety.txt (66 Bytes)
Offline Profile Quote Post Goto Top
 
Albrad
I posted
[ * ]
And here you have the last number list and the driver.
Attached to this post:
Attachments: WordsToNumsDriver.java (467 Bytes)
Attachments: oneTwenty.txt (150 Bytes)
Offline Profile Quote Post Goto Top
 
JGames
Member Avatar
Mega Man!!!

Do you have it set up to receive phrases like "two-hundred" or "two hundred"? With the '-' (dash) or a ' ' (space)?
Posted ImagePosted Image
Posted Image
"Always make the best of life's situations."
KHI Game Soundtracks
Galbadia Hotel Game Soundtracks
Offline Profile Quote Post Goto Top
 
KamiKazeRabbit
Member Avatar
I Ranked Up
[ * ]
Found part of your problem. Remember when you are popping your stack you are working backwards. So when you input "one thousand", "thousand" pops first. It'll skip onetotwenty and nulltoninety and since the variable temp is initialized to zero, it will multiply 0*1000. Then the stack pops "one", and the output ends up as "1".

There's more to it than that, "one hundred five" comes out to "506". I expected "501" (working backwards) so you are probably adding an extra 5 somewhere in there to.
Quote:
 
Oh God! I could be bound in a nutshell, count myself king of infinite space, were it not that I have bad dreams.
-Shakespeare
Offline Profile Quote Post Goto Top
 
JGames
Member Avatar
Mega Man!!!

The 506 probably took the five first and then added 5 and 1 to make 6. Of course, I could be wrong because I didn't look at the code to figure out that. I just looked for a pattern in the number.
Posted ImagePosted Image
Posted Image
"Always make the best of life's situations."
KHI Game Soundtracks
Galbadia Hotel Game Soundtracks
Offline Profile Quote Post Goto Top
 
Albrad
I posted
[ * ]
I see what you mean rabbit. Thanks.
Offline Profile Quote Post Goto Top
 
BloodyRomance
Member Avatar
FLYING HAMMER STRIKE to the face!!!
[ *  *  * ]
i had a problem with a class once. I tried bard, man was it gaaaaaayy-be
BloodyRomance used Sarcasm on the internet!
It's not very effective...
Offline Profile Quote Post Goto Top
 
JGames
Member Avatar
Mega Man!!!

class bard
{
private:
string info;
string trait;

public:
bard( string newInfo );
};

bard::bard( string newInfo )
{
info = newInfo;
trait = "gaaaaaayy-be";
}

int main()
{
string data = "Bard Class";
bard( data );
return 0;
}
Posted ImagePosted Image
Posted Image
"Always make the best of life's situations."
KHI Game Soundtracks
Galbadia Hotel Game Soundtracks
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Got Bugs? · Next Topic »
Add Reply

Affiliates:

JGames TimeSplitters Time Portal My Games Now JDonalds PC Quadkill Dave Mirra 2 Glitches