Results 1 to 3 of 3

Thread: Need some help

  1. #1
    Cross4321 is offline Member
    Join Date
    Jan 2010
    Location
    Somewhere
    Posts
    2
    Rep Power
    0

    Default Need some help

    i tried to make a program Number to Word ranging -9999 to 9999

    idk how do i even make it work

    Java Code:
    import java.io.*;
    
    public class Num2Word {
    
        public static void main (String[] args) {
    
            BufferedReader dataln=new BufferedReader
                    (new InputStreamReader(System.in));
    
            String o="Y";
            String x;
    
            do { String ans="",ones="",tens="",hundreds="",thousands="";
    
            try {
                System.out.println("Enter a Number Between -9999 to 9999:");
                x=dataln.readLine();
                int num=Integer.parseInt(x);
                int ones=num%10;
                int tens=(num/10)%10;
                int hundreds=(num/100)%10;
                int thousands=num/1000;
                if(num>=1&&num<=9999){
    
            //Units Method
                switch(ones){
                       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;
                    }
            //Special Method
                switch(ones){
                       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;
                    }
            //Tens Method
                switch(tens){
                       case 10:
                       System.out.print("Eleven");
                       break;
                       case 20:
                       System.out.print("Twelve");
                       break;
                       case 30:
                       System.out.print("Thirteen");
                       break;
                       case 40:
                       System.out.print("Fourteen");
                       break;
                       case 50:
                       System.out.print("Fifteen");
                       break;
                       case 60:
                       System.out.print("Sixteen");
                       break;
                       case 70:
                       System.out.print("Seventeen");
                       break;
                       case 80:
                       System.out.print("Eighteen");
                       break;
                       case 90:
                       System.out.print("Nineteen");
                       break;
            }
            //Hundreds Method
                switch(hundreds){
                       case 1:
                       System.out.print("OneHundred");
                       break;
                       case 2:
                       System.out.print("TwoHundred");
                       break;
                       case 3:
                       System.out.print("ThreeHundred");
                       break;
                       case 4:
                       System.out.print("FourHundred");
                       break;
                       case 5:
                       System.out.print("FiveHundred");
                       break;
                       case 6:
                       System.out.print("SixHundred");
                       break;
                       case 7:
                       System.out.print("SevenHundred");
                       break;
                       case 8:
                       System.out.print("EightHundred");
                       break;
                       case 9:
                       System.out.print("NineHundred");
                       break;
        }
            //Thousands Method
                switch(thousands){
                       case 1:
                       System.out.print("OneThousand");
                       break;
                       case 2:
                       System.out.print("TwoThousand");
                       break;
                       case 3:
                       System.out.print("ThreeThousand");
                       break;
                       case 4:
                       System.out.print("FourThousand");
                       break;
                       case 5:
                       System.out.print("FiveThousand");
                       break;
                       case 6:
                       System.out.print("SixThousand");
                       break;
                       case 7:
                       System.out.print("SevenThousand");
                       break;
                       case 8:
                       System.out.print("EightThousand");
                       break;
                       case 9:
                       System.out.print("NineThousand");
                       break;
        }
                ans=ones+tens+hundreds+thousands;
                System.out.println("Your Input is"+ans);
                System.out.println("Continue?");
                System.out.print("Press Y if Yes");
                x=dataln.readLine();
                }
    
                else {
                    System.out.println("Invalid Range");
                }
    
            }
    
            catch(IOException e){
                System.out.println("Error!");
            }
            }
            while (o=="Y");
        }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can't switch on Strings. Just integral primitives, chars and enums.
    You should also compare strings using the equals method.
    P.S Smell fried rats when you start to repeat the same (or similar) things over again in code.

  3. #3
    Cross4321 is offline Member
    Join Date
    Jan 2010
    Location
    Somewhere
    Posts
    2
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •