# is something wrong with my code? Lab 10.1

• October 23rd, 2013, 10:40 PM
is something wrong with my code? Lab 10.1
Here are the instructions:

The int t contains an integer between 1 and 50 (inclusive). Write code that outputs the number in words and stores the result in the String inwords. For example, if t is 35 then inwords should contain "thirty five". "t" can be any number from 1 to 50.

Code Java:

```  int t = ; String inwords;   String []wordnames = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "twenty", "thirty", "fourty", "fifty"}; String []multiplesOfTen = {"10", "20", "30", "40", "50"}; String []units = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};   while (t <= 12){ while (units[t].length < 50){ System.out.print(wordnames[t]); } };```

Another thing I've tried is this:

Code Java:

```  inwords = " "; String []wordnames = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "twenty", "thirty", "fourty", "fifty"}; String []multiplesOfTen = {"10", "20", "30", "40", "50"}; String []units = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};   while (t <= 12){ if (units[t] == Integer.toString(t)) System.out.println(units[t]); };```
• October 23rd, 2013, 11:13 PM
Junky
Re: is something wrong with my code? Lab 10.1
Do you have a specific question?
• October 23rd, 2013, 11:18 PM
Ubiquitous
Re: is something wrong with my code? Lab 10.1
One thing is you initialized t as int t = ; Did you mean to initialize it at 0?

Another thing you have nested while loops which are infinite loops. while (t <= 12) how is the condition ever going to be broken if you don't make it possible? t will always be less than 12 in this case as t never changes.

Now if you were to do this on paper how would you start tackling this problem? Take it one step at a time. What exactly is it you are trying to get the program to accomplish? Right now you are telling the computer to print out ("one" * infinite amount of times)
• October 24th, 2013, 12:20 AM
Re: is something wrong with my code? Lab 10.1
I would want it to print the name of the number once, up to 50. So if the number was 55, I guess that's an error. But if the number if 45, I want it to say "forty-five." Basically, I want it to say in words, the name of the number one time. Should I use an if statement? Will that work better or is that not a solution?
• October 24th, 2013, 12:26 AM
Junky
Re: is something wrong with my code? Lab 10.1
Well you are sorta on the right track using arrays but if the objective is to print out the number in words why do you have an array with 10, 20, 30 etc and one with 1, 2, 3 etc?

You should be able to do this with math and not use if statements. If your input is 20 then how can you equate that to the word "twenty" in an array at an index of 1. Ten would be index 0, twenty would be index 1 etc.
• October 24th, 2013, 12:38 AM
Re: is something wrong with my code? Lab 10.1
I have one array for digits 1-12 because those are the ones that are expressed in one syllable. I figured I'd store those in the same array so that I could add elements from that onto the second array, with 10, 20,...etc. Then I would just use a bunch of if-statements for the teens. I think I'm doing it wrong though, which is what I think I'm getting from you. So then, you would use just one array?
• October 24th, 2013, 12:53 AM
Junky
Re: is something wrong with my code? Lab 10.1
No I would use as many arrays as necessary but the point I was trying to make is why do those arrays have integer values in them when the objective is to print words?
• October 24th, 2013, 10:14 AM
Re: is something wrong with my code? Lab 10.1
Because I wanted to do it for each integer. I figured I could say that (pseudocode):

Code Java:

```if var[0 == num[0] {   System.out.println(words[0])   }```

That's just pseudocode just now obviously, but I think I want to do something where every time the integer value in the integer array is equal to the array[word that goes in same place as int], it prints that word. At least, that's what I was originally trying to do.

Thanks for helping by the way. :D
• October 28th, 2013, 11:17 AM
Re: is something wrong with my code? Lab 10.1
Here's what I've changed my code to:

Code Java:

```  String[] multiplesOfTen = {"", "ten", "twenty", "thirty", "forty", "fifty"}; String[] units = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; inwords = " ";   if ((t < 10) || (t > 10) && (t <= 12)){ inwords = units[t]; } else if (t % 10 == 0){ inwords = multiplesOfTen[t]; } else if (t > 20){ int a = t / 10; int b = t % 10; inwords = multiplesOfTen[a] + " " + units[b];   } else if (t == 11){ inwords = "eleven"; } else if (t == 12){ inwords = "twelve"; } else if (t == 13){ inwords = "thirteen"; } else if (t == 14){ inwords = "fourteen"; } else if (t == 15) { inwords = "fifteen"; } else if (t == 16) { inwords = "sixteen"; } else if (t == 17){ inwords = "seventeen"; } else if (t == 18){ inwords = "eighteen"; } else if (t == 19) { inwords = "nineteen"; }   System.out.println(inwords);```

I think its a lot better but it still won't work.
• October 28th, 2013, 07:34 PM
AlexHail
Re: is something wrong with my code? Lab 10.1
You have several logic problems in your quest to finish your code so I will suggest a route to take... Follow at your own will and post back for questions

• Create an array of size 50 (indexes 0-49), holding a string representation of every number 1-50.
- i.e. 1 = "one", 2 = "two", 13 = "thirteen", 46 = "forty six", etc...
• When you have your t value (however you receive your input), print out the value from your array at the index 't' minus one
- for example, if I wanted to print out the string presentation of a number t I would do:
Code :

` System.out.println("t = " + nameOfTheArray[t - 1]);`
(assuming you initialized your array values in incremented order -- 1,2,3,4,5,6... etc)

So in the end, whatever number you have 1-50 should be matched up with the value at that index of the array minus 1. If you wanted to restrict t, simply add an if statement before you check for the string representation.

i.e.
Code :

``` if(t < //LOWEST NUMBER || t > //HIGHEST NUMBER){ System.out.println("t is out of range!"); }```

Do not be afraid of large arrays as long as they contain comprehendible, relatable information. These storage types can be used to hold many useful groups of data such as the number of people in a theatre or the amount of guests at specific tables at a massive dinner party. The possibilities are limited to your knowledge of dimensional arrays and your ability to apply that knowledge.
• October 28th, 2013, 07:48 PM
Junky
Re: is something wrong with my code? Lab 10.1
Quote:

Originally Posted by AlexHail
Create an array of size 50 (indexes 0-49), holding a string representation of every number 1-50.

No, that is the wrong way to solve this problem.
• October 28th, 2013, 07:54 PM
AlexHail
Re: is something wrong with my code? Lab 10.1
Quote:

Originally Posted by Junky
No, that is the wrong way to solve this problem.

• October 28th, 2013, 07:58 PM
Junky
Re: is something wrong with my code? Lab 10.1
My posts have been constructive. The way the OP was trying to solve this was correct by having a "one two three" and "ten twenty thirty" String arrays. they just need some nudging in the right direction. On the other hand your post is bad advice. Why? what happens if requirements change to handling numbers up to 1,000,000. Are you advocating creating an array containing 1,000,000 Strings?
• October 28th, 2013, 08:13 PM
AlexHail
Re: is something wrong with my code? Lab 10.1
Quote:

Originally Posted by Junky
My posts have been constructive. The way the OP was trying to solve this was correct by having a "one two three" and "ten twenty thirty" String arrays. they just need some nudging in the right direction. On the other hand your post is bad advice. Why? what happens if requirements change to handling numbers up to 1,000,000. Are you advocating creating an array containing 1,000,000 Strings?

If the question was print a string ranged from 1-1,000,000 then my advice would be deemed bad because there are more efficient ways of achieving this. But it wasn't, it was 1 to 50. Considering there were no specifics as to the value of t being inputed by a user, nor the range of the value, I was quick to assume this was a low-level task. String parsing can be confusing to new programmers and explaining it all in one post while providing links to JavaDoc didn't seem very useful after reading his work so far. Thus I offered an elementary solution.

You will use less lines of code initializing the values in a String array rather than setting a main String value to its desired literal via else-if statements.
• October 28th, 2013, 08:29 PM
Junky
Re: is something wrong with my code? Lab 10.1
Sigh!

I have had this same argument recently. When you write code it should be as flexible as possible, that is produce correct output for all situations and inputs. Writing code to solve a single specific purpose is poor practice and advising someone to do that is wrong.
• October 28th, 2013, 08:46 PM
AlexHail
Re: is something wrong with my code? Lab 10.1
Quote:

Originally Posted by Junky
Sigh!

I have had this same argument recently. When you write code it should be as flexible as possible, that is produce correct output for all situations and inputs. Writing code to solve a single specific purpose is poor practice and advising someone to do that is wrong.

That is true. I suggested a syntactically correct algorithm. From this, his personal development can progress and branch out to discover your statement on their own. If his question had been "How can I efficiently convert any given integer into its string definition", my response would have been different.

I provide grounds to extract from, not answers to compare to the rest of the possibilities. If you would like to explain that concept to him along with your suggestion, you should. It would be more beneficial than this conversation now.
• October 28th, 2013, 08:51 PM
javaiscool
Re: is something wrong with my code? Lab 10.1
Have you tried println()?
• October 28th, 2013, 09:05 PM
Junky
Re: is something wrong with my code? Lab 10.1
@Alex

So you are advocating that teaching someone the wrong way to code is perfectly fine and leave it up to them to learn the correct way on their own. Why not teach them the correct way in the first place?