944,111 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1697
  • Java RSS
Aug 11th, 2005
0

I Need Help!!

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dmart is offline Offline
3 posts
since Aug 2005
Aug 11th, 2005
0

Re: I Need Help!!

Hi everyone,

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

Yours Sincerely

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Aug 11th, 2005
0

Re: I Need Help!!

what does that mean? do i have to use an array or something?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dmart is offline Offline
3 posts
since Aug 2005
Aug 11th, 2005
0

Re: I Need Help!!

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
yni420 is offline Offline
103 posts
since Oct 2004
Aug 12th, 2005
0

Re: I Need Help!!

Here is the code you need. Read the comments to understand it.
public static void talk(String numbersAndLetters)
{
	//go through the string's characters
	for (int i = 0; i< numbersAndLetters.length(); i++)	
	{
		//get a char
		char currentChar = numbersAndLetters.charAt(i);
		
		//switch on the char
		switch(currentChar)
		{
			//if its 1 print one
			case '1':
				System.out.print("one "); break;
			//if its 2 print two
			case '2':
				System.out.print("two "); break;
			//if its 3 print three
			case '3':
				System.out.print("three "); break;
			//if its 4 print four
			case '4':
				System.out.print("four "); break;
			//if its 5 print five
			case '5':
				System.out.print("five "); break;
			//if its 6 print six
			case '6':
				System.out.print("six "); break;
			//if its 7 print seven
			case '7':
				System.out.print("seven "); break;
			//if its 8 print eight
			case '8':
				System.out.print("eight "); break;
			//if its 9 print nine
			case '9':
				System.out.print("nine "); break;
			//otherwise the char is not a number - just print it out	
			default:
				System.out.print(currentChar); break;
		}
	}
}
Java Syntax (Toggle Plain Text)
  1. talk("321cba");
For more help, www.NeedProgrammingHelp.com
NPH
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NPH is offline Offline
55 posts
since May 2005
Aug 12th, 2005
0

Re: I Need Help!!

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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 12th, 2005
0

Re: I Need Help!!

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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Can Anyone Help Me Please?
Next Thread in Java Forum Timeline: Abstract Error Message





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC