Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: is something wrong with my code? Lab 10.1

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool 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.



     
     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:

     
    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]);
    };


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: is something wrong with my code? Lab 10.1

    Do you have a specific question?
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default 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)

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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):

    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.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: is something wrong with my code? Lab 10.1

    Here's what I've changed my code to:

     
    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.

  10. #10
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default 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:
       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.
     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.

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: is something wrong with my code? Lab 10.1

    Quote Originally Posted by AlexHail View Post
    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.
    Improving the world one idiot at a time!

  12. #12
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: is something wrong with my code? Lab 10.1

    Quote Originally Posted by Junky View Post
    No, that is the wrong way to solve this problem.
    How about being constructive instead of self satisfying posts.

  13. #13
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

  14. #14
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: is something wrong with my code? Lab 10.1

    Quote Originally Posted by Junky View Post
    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.

  15. #15
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  16. #16
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: is something wrong with my code? Lab 10.1

    Quote Originally Posted by Junky View Post
    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.

  17. #17
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: is something wrong with my code? Lab 10.1

    Have you tried println()?

  18. #18
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

Similar Threads

  1. Need Help with While Loop Lab
    By LennDawg in forum Other Programming Languages
    Replies: 4
    Last Post: June 29th, 2012, 10:41 AM
  2. Replies: 2
    Last Post: February 7th, 2012, 04:26 PM
  3. Help with a lab!
    By Spicy McHaggis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2012, 08:03 AM
  4. Java SE 7 OCA Training Lab Full Version Launched
    By treacyjane in forum The Cafe
    Replies: 0
    Last Post: November 4th, 2011, 08:57 AM
  5. PLEASE HELP ME WITH MY COLLEGE LAB!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 29th, 2011, 04:06 PM