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: Whats wrong with my Code?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my Code?

    hi there can any one help me figure out how to convert numbers into string without using an array and a method please thank you


    example of my arrayed code i dont like this

    the code here is working but i want to use the other way for not using array
    just like switches and if and loops only
    im sorry for not using the proper tags to this code im a new comer thank you for your time to reply on this thread



    i made a code here but i did'nt run what i want to output sir

    package UnderPackage;
    import java.util.Scanner;
     
    public class NumberToWords {
        static Scanner input = new Scanner(System.in);
        public static void main(String[] args) {
     
            int number;
            int b;
            String thousands ="";
            String hundred ="";
            String tens ="";
            String firstTen ="";
            String ones ="";
            System.out.println("Enter a number: ");
            number = input.nextInt();
           int th = number/1000;
     
            switch (th){
                case 1:
                    thousands ="one thousand";
                break;
     
                case 2:
                    thousands ="two thousand";
                break;
     
                case 3:
                    thousands ="three thousand";
                break;
     
                case 4:
                    thousands ="four thousand";
                break;
     
                case 5:
                    thousands ="five thousand";
                break;
     
                case 6:
                    thousands ="six thousand";
                break;
     
                case 7:
                    thousands ="seven thousand";
                break;
     
                case 8:
                    thousands ="eight thousand";
                break;
     
                case 9:
                    thousands ="nine thousand";
                break;
     
                case 10:
                    thousands ="ten thousand";
                break;
            }
            switch(number/100){
     
    	case 1:
    		hundred = "one hundred";
    	break;
     
    	case 2:
    		hundred = "two hundred";
    	break;
     
    	case 3:
    		hundred = "three hundred";
    	break;
     
    	case 4:
    		hundred = "four hundred";
    	break;
     
    	case 5:
    		hundred = "five hundred";
    	break;
     
    	case 6:
    		hundred = "six hundred";
    	break;
     
    	case 7:
    		hundred = "seven hundred";
    	break;
     
    	case 8:
    		hundred = "eight hundred";
    	break;
     
    	case 9:
    		hundred = "nine hundred";
    	break;
     
    }
     
            if(number<= 19 && number >=11){
                int special = number%100;
                switch (special){
                    case 11:
                        firstTen = "eleven";
                    break;
     
                    case 12:
                        firstTen = "twelve";
                    break;
     
                    case 13:
                        firstTen = "thirteen";
                    break;
     
                    case 14:
                        firstTen = "fourteen";
                    break;
     
                    case 15:
                        firstTen = "fifthteen";
                    break;
     
                    case 16:
                        firstTen = "sixteen";
                    break;
     
                    case 17:
                        firstTen = "seventeen";
                    break;
     
                    case 18:
                        firstTen = "eightteen";
                    break;
     
                    case 19:
                        firstTen = "nineteen";
                    break;
                }
                System.out.println(th + special);         
            }
            switch (number/10){
                case 1:
                    tens ="ten";
                break;
     
                case 2:
                    tens ="twenty";
                break;
     
                case 3:
                    tens ="thirty";
                break;
     
                case 4:
                    tens ="fourty";
                break;
     
                case 5:
                    tens ="fifty";
                break;
     
                case 6:
                    tens ="sixty";
                break;
     
                case 7:
                    tens ="seventy";
                break;
     
                case 8:
                    tens ="eighty";
                break;
     
                case 9:
                    tens ="ninety";
                break;
            }
            if(number>0){
                switch (number){
                    case 1:
                        ones ="one";
                    break;
     
                    case 2:
                        ones ="two";
                    break;
     
                    case 3:
                        ones ="three";
                    break;
     
                    case 4:
                        ones ="four";
                    break;
     
                    case 5:
                        ones ="five";
                    break;
     
                    case 6:
                        ones ="six";
                    break;
     
                    case 7:
                        ones ="seven";
                    break;
     
                    case 8:
                        ones ="eight";
                    break;
     
                    case 9:
                        ones ="nine";
                    break;
     
                }
     
                 System.out.print(thousands + hundred +firstTen+ tens + ones);
     
            }
        }  
       }
    Last edited by broodler; February 12th, 2014 at 04:22 PM. Reason: for better understanding


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Whats wrong with my Code?

    When posting code, please use the highlight tags to preserve formatting.

    What does this code do? What are you confused about? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Whats wrong with my Code?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my Code?

    sorry sir i got sleep . . same concept but i dont want to use array here i dont know how to convert it not using arrays and methods

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Whats wrong with my Code?

    convert it not using arrays and methods
    Writing code that doesn't use arrays or methods is a step backwards. Is that a requirement from your instructor to show you how hard it is and how ugly a program would be without using those two techniques?

    It could be done with a long chain of if/else if statements
    or with a switch statement.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my Code?

    yes sir it is. . im confusing when starting to think the logic after declaring variables and accept an input type

Similar Threads

  1. Whats wrong with my code?
    By incredibleX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2014, 03:40 PM
  2. Whats wrong with my code?
    By green001 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 10th, 2014, 06:02 PM
  3. whats wrong with my code
    By abhi1402 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 6th, 2013, 09:17 AM
  4. [SOLVED] Im not sure whats wrong with my code
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 25th, 2013, 10:08 PM
  5. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM