i have the following code that reads user input characters and displays the string entered in reverse order.

import java.io.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ReverseString {
  public String reverse(String arg)
 {
        String input = null;
        if (arg.length() == 1)
        {
                return arg;
        }

        else
        {


                //extract the last character entered
                String lastChar = arg.substring(arg.length()-1,arg.length());


                //extract the remaining characters
                String remainingChar = arg.substring(0, arg.length() -1);

                input = lastChar + reverse(remainingChar);
                return input;


        }
  }

}

import java.io.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class TestReverseString
{
  public static void main(String[] args) throws IOException
         {
                System.out.println("Enter string values.");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String inputData;
                inputData = br.readLine();
                ReverseString rs = new ReverseString();
                System.out.println(rs.reverse(inputData));
        }
}

If abc123 is entered, 321cba is returned. How do I get the program to return three two one cba? In other words, I need to spell out the numeric values that are entered.

thanks for your time fellas

Edited 3 Years Ago by pyTony: fixed formating

Hi everyone,

You have to pass each numerical value thus its something down the lines of writing your own parser

Yours Sincerely

Richard West

what does that mean? do i have to use an array or something?

why dont you use some switch statement to see whether the char entered is number or what and the convert it to the appropriate string value?
you can loop through the string using for

Here is the code you need. Read the comments to understand it.

public static void talk(String numbersAndLetters)
{
	[B]//go through the string's characters[/B]
	for (int i = 0; i< numbersAndLetters.length(); i++)	
	{
		[B]//get a char[/B]
		char currentChar = numbersAndLetters.charAt(i);
		
		[B]//switch on the char[/B]
		switch(currentChar)
		{
			[B]//if its 1 print one[/B]
			case '1':
				System.out.print("one "); break;
			[B]//if its 2 print two[/B]
			case '2':
				System.out.print("two "); break;
			[B]//if its 3 print three[/B]
			case '3':
				System.out.print("three "); break;
			[B]//if its 4 print four[/B]
			case '4':
				System.out.print("four "); break;
			[B]//if its 5 print five[/B]
			case '5':
				System.out.print("five "); break;
			[B]//if its 6 print six[/B]
			case '6':
				System.out.print("six "); break;
			[B]//if its 7 print seven[/B]
			case '7':
				System.out.print("seven "); break;
			[B]//if its 8 print eight[/B]
			case '8':
				System.out.print("eight "); break;
			[B]//if its 9 print nine[/B]
			case '9':
				System.out.print("nine "); break;
			[B]//otherwise the char is not a number - just print it out[/B]	
			default:
				System.out.print(currentChar); break;
		}
	}
}
talk("321cba");

For more help, www.NeedProgrammingHelp.com

NPH, DO NOT do peoples' homework for them. It makes sure these people never learn a thing (or rather that all they learn is that there's always a sucker stupid enough to do their work for them).

Hi everyone,

NPH actually jwenting is right about this. I don't mean to be mean but i used to do this sometime back(i think jwenting remembers) and after a while these students started flooding my emails asking me to do their homework.

There was one week when i got 20 requests in my email for me to do their homework. I don't mind helping out and writing a few methods here and there but some of them asked me to write an entire application for them. There was one request i got asking me to write an entire word editor for him and send him the code.

I apologize if i sound mean as that's not what i am trying to do

Yours Sincerely

Richard West

This article has been dead for over six months. Start a new discussion instead.