| 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: |
| Problem with a class; Problems with converting string to int | |
|---|---|
| Topic Started: Mar 22 2009, 07:40 PM (66 Views) | |
| Albrad | Mar 22 2009, 07:40 PM Post #1 |
|
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; } } |
![]() |
|
| JGames | Mar 22 2009, 07:56 PM Post #2 |
|
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.
|
![]() ![]() ![]() "Always make the best of life's situations." KHI Game Soundtracks Galbadia Hotel Game Soundtracks | |
![]() |
|
| Albrad | Mar 22 2009, 09:24 PM Post #3 |
|
I posted
![]() ![]() ![]()
|
here are two of the number lists. |
![]() |
|
| Albrad | Mar 22 2009, 09:24 PM Post #4 |
|
I posted
![]() ![]() ![]()
|
And here you have the last number list and the driver. |
![]() |
|
| JGames | Mar 22 2009, 09:41 PM Post #5 |
|
Mega Man!!!
![]()
|
Do you have it set up to receive phrases like "two-hundred" or "two hundred"? With the '-' (dash) or a ' ' (space)? |
![]() ![]() ![]() "Always make the best of life's situations." KHI Game Soundtracks Galbadia Hotel Game Soundtracks | |
![]() |
|
| KamiKazeRabbit | Mar 22 2009, 10:08 PM Post #6 |
|
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. |
-Shakespeare | |
![]() |
|
| JGames | Mar 22 2009, 10:11 PM Post #7 |
|
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. |
![]() ![]() ![]() "Always make the best of life's situations." KHI Game Soundtracks Galbadia Hotel Game Soundtracks | |
![]() |
|
| Albrad | Mar 22 2009, 10:57 PM Post #8 |
|
I posted
![]() ![]() ![]()
|
I see what you mean rabbit. Thanks. |
![]() |
|
| BloodyRomance | Oct 18 2009, 10:45 PM Post #9 |
|
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... | |
![]() |
|
| JGames | Oct 20 2009, 07:32 PM Post #10 |
|
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; } |
![]() ![]() ![]() "Always make the best of life's situations." KHI Game Soundtracks Galbadia Hotel Game Soundtracks | |
![]() |
|
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Got Bugs? · Next Topic » |





![]](http://209.85.122.85/static/1/pip_r.png)







hundredToBillion.txt (35 Bytes)
5:34 PM Nov 27




