When i run a single number (1-9), it'll output as ONEONE or TWOTWO, etc.. As well with numbers 11-19, they'll show up as ONEELEVEN or TWOTWELVE, etc. 20-100 outputs fine whereas 101 becomes ONE HUNDRED TEN ONE ONE, etc.I have no idea what I've done any help would be greatly appreciated.

public class Main
{
 public static void main (String args []) throws IOException
 {
    //Initializes the BufferReader for user input
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

    System.out.println("Numbers to Words (1-999 Edition)");
    System.out.println("================================");
    int tryAgain=1;//will initiate the first do-while loop of the program.
    do{
        System.out.println();
        System.out.println("Input a number (1-999) and this program will repeat it to you with words.");
        double numInput=Integer.parseInt(br.readLine());//user inputs number
        System.out.println();

        int error=1;//will initiate error check

    do{
        if(numInput<1 || numInput>999){//if the user enters an invalid number
        System.out.println("Invalid number. Enter a number between 1 and 999.");
        numInput=Integer.parseInt(br.readLine());//user must re-enter a number if they entered an invalid one
        System.out.println();}
    else{
        error=0;}//will end error loop if the number is correct
    }while(error==1);

    double hundredsDigit=Math.floor(numInput/100),
            tensDigit=Math.floor((numInput % 100) / 10);
    double onesDigit = numInput%10,tensones=numInput%100;

    System.out.print("Your number in words is: ");

    if (numInput>=100&&numInput<=999){
        hundreds(hundredsDigit);
        System.out.print(" ");
        if ((tensones>=11)&&(tensones<=19))
            teens(tensones);
        if ((tensones>=1)&&(tensones<=99))
        tensDigit=Math.floor(tensones/10);
        tens(tensones);
        System.out.print(" ");//puts a space between the tens and ones
        onesDigit=tensones%10;
        ones(tensones);}

    if ((numInput>=20)&&(numInput<=99))
        tens(tensDigit);
        System.out.print(" ");
        ones(onesDigit);

    if ((numInput>=10)&&(numInput<=19))//will initiate the teens subroutine)
        teens(numInput);

    if (numInput<=9)
        ones(numInput);

    System.out.println();
    System.out.println();
    System.out.println("Press 1 to try again.");
    tryAgain=Integer.parseInt(br.readLine());//user decides to try again
    System.out.println();
    }while(tryAgain==1);//will repeat program if user chooses to enter another number

 }//closes main body

  //method that will print the "hundreds" part of numbers 100-999
  public static void hundreds(double hundredsDigit){
     if (hundredsDigit==1){
            System.out.print("ONE HUNDRED");
     }
     if (hundredsDigit==2){
            System.out.print("TWO HUNDRED");
     }
     if (hundredsDigit==3){
            System.out.print("THREE HUNDRED");
     }
     if (hundredsDigit==4){
            System.out.print("FOUR HUNDRED");
     }
     if (hundredsDigit==5){
            System.out.print("FIVE HUNDRED");
     }
     if (hundredsDigit==6){
            System.out.print("SIX HUNDRED");
     }
     if (hundredsDigit==7){
            System.out.print("SEVEN HUNDRED");
     }
     if (hundredsDigit==8){
            System.out.print("EIGHT HUNDRED");
     }
     if (hundredsDigit==9){
            System.out.print("NINE HUNDRED");
     }
 }//closes hundreds method

 //method that will print the "tens" part of numbers 20-99
 public static void tens(double tensDigit){
     if (tensDigit==1){
            System.out.print("TEN");
     }
     if (tensDigit==2){
            System.out.print("TWENTY");
     }
     if (tensDigit==3){
            System.out.print("THIRTY");
     }
     if (tensDigit==4){
            System.out.print("FORTY");
     }
     if (tensDigit==5){
            System.out.print("FIFTY");
     }
     if (tensDigit==6){
            System.out.print("SIXTY");
     }
     if (tensDigit==7){
            System.out.print("SEVENTY");
     }
     if (tensDigit==8){
            System.out.print("EIGHTY");
     }
     if (tensDigit==9){
            System.out.print("NINETY");
     }
 }//closes tens method

  //method that will print the numbers 10-19
  public static void teens(double numInput){
      if (numInput==10){
            System.out.print("TEN");
      }
      if (numInput==11){
            System.out.print("ELEVEN");
      }
      if (numInput==12){
            System.out.print("TWELVE");
      }
      if (numInput==13){
            System.out.print("THIRTEEN");
      }
      if (numInput==14){
            System.out.print("FOURTEEN");
      }
      if (numInput==15){
            System.out.print("FIFTEEN");
      }
      if (numInput==16){
            System.out.print("SIXTEEN");
      }
      if (numInput==17){
            System.out.print("SEVENTEEN");
      }
      if (numInput==18){
            System.out.print("EIGHTEEN");
      }
      if (numInput==19){
            System.out.print("NINETEEN");
      }
 }//closes teens method

  //method that will print the "ones" part of numbers 20-99
  public static void ones(double onesDigit){
      if (onesDigit==1){
            System.out.print("ONE");
      }
      if (onesDigit==2){
            System.out.print("TWO");
      }
      if (onesDigit==3){
            System.out.print("THREE");
      }
      if (onesDigit==4){
            System.out.print("FOUR");
      }
      if (onesDigit==5){
            System.out.print("FIVE");
      }
      if (onesDigit==6){
            System.out.print("SIX");
      }
      if (onesDigit==7){
            System.out.print("SEVEN");
      }
      if (onesDigit==8){
            System.out.print("EIGHT");
      }
      if (onesDigit==9){
            System.out.print("NINE");
      }
 }//closes ones method

Yes, well think about the problem and write out a solution in words. First, you need to determine the scope of the number (thousands, hundreds, tens, ones columns, etc), then figure out the number of each found. Then you can encode the number as words. So, take the number 3532. How do you figure out how many 1000's there are? How many 100's? How many 10's, and so on? Write it out, possibly in pseudo-code. Once you are certain you have the process correct, writing code is trivial (relatively).

very inefficiënt code. too many if's, and even if you wanted to use if-s (what remains bad practice) , you should use if - else if

for instance: if onesDigit turns out to be 1 ... is there really a reason to still check whether or not it is 2, 3, 4, 5, 6, 7, 8 or 9?

also: an int would suffice here, since it's only one digit.

but, a way you can do about the same with less lines of code:

public static void ones(int point){
String[] ones = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    if ( point != 0 )
      System.out.println(ones[point]);
}
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.