First of all, Happy new year!!!

import java.io.*;

public class Main {
    public static void main(String[] args) {
        
          int input = 0;
          
           switch (input) {
            case 1:
                System.out.print("one ");
                break;
            case 2:
                System.out.print("two ");
                break;
            case 3:
                System.out.print("three ");
                break;
            case 4:
                System.out.print("four ");
                break;
            case 5:
                System.out.print("five ");
                break;
            case 6:
                System.out.print("six ");
                break;
            case 7:
                System.out.print("seven ");
                break;
            case 8:
                System.out.print("eight ");
                break;
            case 9:
                System.out.print("nine ");
                break;
        }
           
           
            switch (input) {
            case 11:
                System.out.print(" eleven");
                break;
            case 12:
                System.out.print(" twelve");
                break;
            case 13:
                System.out.print(" thirteen");
                break;
            case 14:
                System.out.print(" fourteen");
                break;
            case 15:
                System.out.print(" fifteen");
                break;
            case 16:
                System.out.print(" sixteen");
                break;
            case 17:
                System.out.print(" seventeen");
                break;
            case 18:
                System.out.print(" eighteen");
                break;
            case 19:
                System.out.print(" nineteen");
                break;
        }
          switch (input) {
            case 1:
                System.out.print(" ten ");
            case 2:
                System.out.print(" twenty ");
                break;
            case 3:
                System.out.print(" thirty ");
                break;
            case 4:
                System.out.print(" forty ");
                break;
            case 5:
                System.out.print(" fifty ");
                break;
            case 6:
                System.out.print(" sixty ");
                break;
            case 7:
                System.out.print(" seventy ");
                break;
            case 8:
                System.out.print(" eighty ");
                break;
            case 9:
                System.out.print(" ninety ");
                break;
        }
          switch (input) {
            case 1:
                System.out.print("one hundred ");
                break;
            case 2:
                System.out.print("two hundred ");
                break;
            case 3:
                System.out.print("three hundred ");
                break;
            case 4:
                System.out.print("four hundred ");
                break;
            case 5:
                System.out.print("five hundred ");
                break;
            case 6:
                System.out.print("six hundred ");
                break;
            case 7:
                System.out.print("seven hundred ");
                break;
            case 8:
                System.out.print("eight hundred ");
                break;
            case 9:
                System.out.print("nine hundred ");
                break;
        }
      
          
          System.out.print("Enter a number between 0 and 999: ");
                
          BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
           String n = "";
            int p = Integer.parseInt(n);
                  
                   if (p > 99 && p < 1000) {
                     int h = p/100;
                        System.out.println(h);
                      int x = p % 100;        
                      if (x > 10 && x < 20) {  
                        System.out.println(x);   
                   }
                  
                     if (x > 0 && x < 100) {
                        int y = x / 10;   
                         System.out.println(y);         

                         int z = x % 10;  
                          System.out.println(z);        
            }
        }
                      if ( p > 19 && p < 100) {
                        int t = p / 10;    
                          System.out.println(t);                
                        int u = p % 10;    
                          System.out.println(u);  
        }
                        if (p > 10 && p < 20) { 
                           System.out.println(p);        
        }
                        if (p < 10) {           
                            System.out.println(p);          
        }
    }
}

Hi, I'm beginner learning programming .:$ The goal of this java program is to convert an integer ranging from 0-999 to its word equivalent, and it should ask the user if he/she still wants to enter a new number(I have no idea how can I make that possible ):icon_eek:
I'm not even sure of this codes are close enough to attain that goal.

Can you point out my errors? What should I change? Am I getting there?

Any help is appreciated.

Have a nice day!

Recommended Answers

All 3 Replies

Welcome to Daniweb, and a Happy New Year.
To get to the point: there are some improvements possible to your code, but that of course depends on your current knowledge, for example: are you familiar with writing your own methods and/or using arrays?
If you know how arrays work, then you can leave out a whole bunch of switch statements by intelligently making use of the capabilities of an array.
Let me give you an example:

public class Convert
{
  public static void main(String args[])
  {
    String[] oneToNine =
    {
      "one", 
      "two", 
      "three", 
      "four", 
      "five", 
      "six", 
      "seven", 
      "eighth", 
      "nine"
    };

    String[] specialCases =
    {
      "ten", 
      "eleven", 
      "twelve", 
      "thirteen", 
      "fourteen", 
      "fifteen", 
      "sixteen", 
      "seventeen", 
      "eigtheen", 
      "nineteen"
    };
    
    for (int number = 0; number < 20; number++)
    {
      // display the number we're about to convert
      System.out.print(number + ": ");
      
      // the actual conversion is done here
      if ( number == 0 )
        System.out.println( "zero" );
      else if ( 0 < number && number < 10 )
        System.out.println( oneToNine[ number - 1 ] );
      else if ( 10 <= number && number < 20 )
        System.out.println( specialCases[ number - 10 ] );
    }
  }
}

This piece of code will allow you to convert numbers within the range 0-20 (including zero).

We are only allowed to use the switch method.

and it should ask the user if he/she still wants to enter a new number(I have no idea how can I make that possible)

You can make it possible by using a loop, are you allowed to use them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.