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

Thread: Help changing int's stored in array to words ( 1 to one) and printing in reverse.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help changing int's stored in array to words ( 1 to one) and printing in reverse.

    java - print numbers as word equivalent but in reverse

    --------------------------------------------------------------------------------

    I am working on a program in java on notepad++ that opens a file(reverse.in) opens it and extracts the integers to an array. Now I need convert the numbers to words and print them out in reverse. For example 0 1 2 3 would become three two one zero.

    I have the array set but have no idea how to convert the numbers and print in reverse. This is only my 3rd week of java so the more info the better!!!
    import java.util.*;
    import java.io.*;
     
    public class Reverse1{
    public static void main(String[] args) throws Exception{
    int i =0;
    File file = null;
    file = new File("Reverse.in");
    BufferedReader bufRdr = new BufferedReader(new FileReader(file));
    String line = "";
    String [] numbers = new String[10];
     
    while ((line = bufRdr.readLine()) != null){
     
    StringTokenizer st = new StringTokenizer(line,",");
    while (st.hasMoreTokens()) {
    numbers[i]= st.nextToken();
    } 
    i++;
    }
     
    for (i = 1; i < numbers.length; i++) { 
    System.out.print("" + numbers[i]); 
    System.out.println();
    } 
    System.out.println();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,049
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Help changing int's stored in array to words ( 1 to one) and printing in reverse.

    To get words from integer values look at using the switch statement
    or using if/else if statements to test the int value and set the word.

    Are the values integers or Strings? 1 or "1"
    If they are Strings you will need to convert them to integer values. See the Integer class for the parse method to use.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Help changing int's stored in array to words ( 1 to one) and printing in reverse.

    You can place the words into an array then use the numbers as indicies into the array. This solution does not scale well if you plan on expanding the program to include much larger numbers. ie one thousand and forty nine.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  2. [SOLVED] implementing a dictionary of words using an array of linked lists
    By McTown in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 02:19 PM
  3. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 02:18 PM
  4. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM