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 6 of 6

Thread: How to get rid of this?

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to get rid of this?

    Hello, so I am still stumped on this 99 bottles problem. I got most of it working however there are still some issues. This is what i have so far.

     public class BottleSong { 
     private int bottles = 0;
     
    public BottleSong(int n)
     
    {
     
        bottles = n;
     
     
     
     
     
    }
     
    public void printNumInEnglish(int n)
     
            {
     
     
     
               int tens = n/10;
     
                int ones = n%10;
     
                String t = new String();
     
                String o = new String();
     
     
     
     
     
                switch (tens)
     
                {
     
     
     
     
     
                    case 1:
     
                        switch (ones)
     
                        {
     
                             case 0:
     
                                  o = "Ten";
     
                                break;
     
                              case 1:
     
                                  o = "Eleven";
     
                                  break;
     
                             case 2:
     
                                  o = "Twelve";
     
                                  break;
     
                              case 3:
     
                                  o = "Thirteen";
     
                                  break;
     
                              case 4:
     
                                  o = "Fourteen";
     
                                  break;
     
                              case 5:
     
                                  o = "Fifteen";
     
                                  break;
     
                              case 6:
     
                                  o = "Sixteen";
     
                                  break;
     
                              case 7:
     
                                  o = "Seventeen";
     
                                  break;
     
                              case 8:
     
                                  o = "eighteen";
     
                                  break;
     
                              case 9:
     
                                  o = "nineteen";
     
                                  break;
     
                        }
     
     
                    case 2:
     
                        t = "Twenty";
     
                        break;
     
                    case 3:
     
                        t = "Thirty";
     
                        break;
     
                    case 4:
     
                        t = "Forty";
     
                        break;
     
                    case 5:
     
                        t = "Fifty";
     
                        break;
     
                    case 6:
     
                        t = "Sixty";
     
                        break;
     
                    case 7:
     
                        t = "Seventy";
     
                        break;
     
                    case 8:
     
                        t = "Eighty";
     
                        break;
     
                    case 9:
     
                        t = "Ninety";
     
                        break;
     
                }      
     
                if (tens != 1)
     
                {
     
                    switch (ones)
     
                    {
     
                        case 1:
     
                            o = "One";
     
                            break;
     
                        case 2:
     
                            o = "Two";
     
                            break;
     
                        case 3:
     
                            o = "Three";
     
                            break;
     
                        case 4:
     
                            o = "Four";
     
                            break;
     
                        case 5:
     
                            o = "Five";
     
                            break;
                       case 6:
     
                            o = "Six";
     
                            break;
     
                        case 7:
     
                            o = "Seven";
     
                            break;
     
                        case 8:
     
                            o = "Eight";
     
                            break;
     
                        case 9:
     
                            o = "Nine";
     
                            break;
     
                    }
     
     
     
                }
     
                       System.out.print(t + " " + o + " ");
     
     
     
    }
     
     public void printStanza(int n)
     
        {     
     
            String [] numbers = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "ninteen" };
     
            String [] tens = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
     
            if(n < 20)
     
                System.out.println(numbers[n-1]);
     
            else 
     
     
            if(n % 10 == 0)
     
                System.out.println(tens[(n/10)-2]); 
     
            else
     
                System.out.println(tens[(n/10)-2] + " " + numbers[n%10]);     
     
     
     
     
     
             printNumInEnglish(n);
     
     
     
     
            if (n == 1) {
     
                System.out.println("one bottle of beer on the wall, ");
     
            }
     
            else {
     
                System.out.println("bottles of beer on the wall, ");
     
            }
     
     
     
            printNumInEnglish(n); 
     
            if (n == 1) {
     
               System.out.println("one bottle of beer, ");
     
           }
     
            else {
     
                System.out.println("bottles of beer, ");
     
            }
     
            System.out.println("Take one down, pass it around,");
     
            n--;
     
     
     
            printNumInEnglish(n); 
     
            if (n == 1) {
     
                System.out.println("bottle of beer on the wall.");
     
            }
     
            else {
     
                System.out.println("bottles of beer on the wall.");
     
            }
     
            System.out.println();
     
        }
     
     
     
        public void printSong() {
     
            // Loop from 99 down to 0
     
            for (int num = bottles; num > 0; num--) {
     
                printStanza(num);
     
            }
     
        }
     
     
     
        public static void main(String[] args) {
     
            BottleSong bs = new BottleSong(99);
     
            bs.printSong();
     
        }
     
     
     
    }


    When i run it, there are issues with the numbers past 20. It looks like:

    twenty
    Twenty bottles of beer on the wall,
    Twenty bottles of beer,
    Take one down, pass it around,
    Twenty nineteen bottles of beer on the wall.

    eighteen
    Twenty nineteen bottles of beer on the wall,
    Twenty nineteen bottles of beer,
    Take one down, pass it around,
    Twenty eighteen bottles of beer on the wall.

    seventeen
    Twenty eighteen bottles of beer on the wall,
    Twenty eighteen bottles of beer,
    Take one down, pass it around,
    Twenty Seventeen bottles of beer on the wall.

    sixteen
    Twenty Seventeen bottles of beer on the wall,
    Twenty Seventeen bottles of beer,
    Take one down, pass it around,
    Twenty Sixteen bottles of beer on the wall.

    How do I get rid of the twenty in front of the teen numbers? If someone can help me with this that would be greatly appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: How to get rid of this?

    Is this the same problem: Why isn't this code working?

    Why not do this on the same thread instead of starting a new one?

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to get rid of this?

    No one knows the answer?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: How to get rid of this?

    Where in the code is the lower case word printed? twenty
    Where is the upper case word printed? Twenty

    What does the uppercase word stay the same and the lower case one change?

    Add some println statements to the code to print out all the variables used to compute the values that controls what is printed out. See why the uppercase word never changes and the lower case one does.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to get rid of this?

    Quote Originally Posted by Norm View Post
    Where in the code is the lower case word printed? twenty
    Where is the upper case word printed? Twenty

    What does the uppercase word stay the same and the lower case one change?

    Add some println statements to the code to print out all the variables used to compute the values that controls what is printed out. See why the uppercase word never changes and the lower case one does.
    Can you please elaborate? What println statements and where should they go? Thank you.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: How to get rid of this?

    Put a println at the beginning of each method and print out the arguments that are passed to.

    Print out all the variables that you use to compute what is to be printed out.
    Put the println right after the statement where their value is changed.