The Artima Developer Community
Sponsored Link

Java Answers Forum
Recursive method using StringReverse

9 replies on 1 page. Most recent reply: Nov 9, 2002 1:15 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Valerie Tappin

Posts: 2
Nickname: val
Registered: Nov, 2002

Recursive method using StringReverse Posted: Nov 3, 2002 6:40 PM
Reply to this message Reply
Advertisement
How do I write a recursive method stringReverse that takes a character array containing a string as an argument, print the string backward and returns nothing?


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Recursive method using StringReverse Posted: Nov 4, 2002 3:35 PM
Reply to this message Reply

public void stringReverse(char[] characters){
for (int i = 0; i < characters.length; i++){
System.out.print(characters[characters.length - i -1]);
}
}

Marie Harris

Posts: 6
Nickname: match
Registered: Nov, 2002

Single Subscript Array Posted: Nov 6, 2002 5:09 PM
Reply to this message Reply
How do I resolve the problem below:

Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Recursive method using StringReverse Posted: Nov 7, 2002 2:28 PM
Reply to this message Reply
Some one requested separatley concerning my post:
What should be at the beginning of this program?

The method that can be inserted into any java class that will reverse and print any character array argument supplied to it.

The following is an example program.



public class Test{

public static void main(String[] args){
Test test = new Test();
String testString = "Click the Post Message button to submit your message immediately.";
test.stringReverse(testString.toCharArray());
System.exit(0);
}

public void stringReverse(char[] characters){
for (int i = 0; i < characters.length; i++){
System.out.print(characters[characters.length - i -1]);
}
}

}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Recursive method using StringReverse Posted: Nov 7, 2002 5:56 PM
Reply to this message Reply
> How do I write a recursive method stringReverse that...

Something is still missing, Charles... ;-)

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Recursive method using StringReverse Posted: Nov 8, 2002 7:09 AM
Reply to this message Reply
This may be a better example of recursion, since a true recursion should call itself.


public void stringReverse(char[] characters){
if (characters.length > 0){
System.out.print(characters[characters.length -1]);
char[] temp = new char[characters.length -1];
for (int i = 0; i < temp.length; i++){
temp[i] = characters[i];
}
stringReverse(temp);
}
}

Ramu

Posts: 29
Nickname: sripoorna
Registered: Oct, 2002

Re: Recursive method using StringReverse Posted: Nov 8, 2002 8:11 AM
Reply to this message Reply
hi
use this code
public void testing(int length)
{
if (length == 0)
{
ch1[k] = ch[length];
System.out.println("after reversing...."+new String(ch1));
}
else
{
ch1[k] = ch[length];
length = length - 1;
k = k + 1;
testing(length);
}
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Recursive method using StringReverse Posted: Nov 8, 2002 10:49 AM
Reply to this message Reply
A more concise method:

public void stringReverse(char[] characters){
if (characters.length > 0){
System.out.print(characters[characters.length -1]);
stringReverse(String.copyValueOf(characters, 0, characters.length -1).toCharArray());
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Recursive method using StringReverse Posted: Nov 8, 2002 11:55 AM
Reply to this message Reply
> use this code

I can think of more reasons not to use it than to use it (too many to list, in fact!), not the least of which is the simple fact that Charles already posted a much better solution.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Single Subscript Array Posted: Nov 9, 2002 1:15 PM
Reply to this message Reply
Solution to the single-subscripted array problem:

You can download the code at:
http://www.quantumhyperspace.com/SourceBank/IntegerTest.java


/* IntegerTest.java
* @author: Charles Bell
* @version: Nov 9, 2002
*/

import javax.swing.*;

/** Problem:
* Use a single-subscripted array to solve the following problem.
* Read in 20 numbers, each of which is between 10 and 100, inclusive.
* As each number is read, print it only if it is not a duplicate of a
* number already read. Provide for the "worst case" in which all
* 20 numbers are different. Use the smallest possible array
* to solve this problem
*/
public class IntegerTest{

/* a single-subscripted array */
private int[] numbers;

/* between 10 and 100, inclusive. */
private int min = 10;
private int max = 100;

/* 20 numbers */
private int problemSize = 20;
private boolean debug = true;


public static void main(String[] args){
IntegerTest test = new IntegerTest();
test.runProblem();
System.exit(0);
}

/* Does all the work to complete the problem.
*/
public void runProblem(){
String message = "Enter a number between "
+ String.valueOf(min) + " and "
+ String.valueOf(max) + " inclusive.";
/* Read in 20 numbers */
for (int i = 0; i < problemSize; i++){
int nextNumber = getNumberInRange(message);
if (!testNumber(nextNumber, numbers)){
if (numbers == null) {
numbers = new int[1];
numbers[0] = nextNumber;
}else{
/* The array size will only increase if a new number is input. */
numbers = addArrayElement(nextNumber, numbers);
}
/* As each number is read, print it only if it is not
* a duplicate of a number already read
*/
if (debug) System.out.println(nextNumber);
JOptionPane.showMessageDialog(null, String.valueOf(nextNumber));
}
}
if (debug) System.out.println("numbers input: " + problemSize);
/* The array size will be equal to the problemSize if all different numbers are entered. */
/* The array size will be less than the problemSize if duplicate numbers were entered. */
if (debug) System.out.println("array size: " + numbers.length);
if (debug) {
for (int i = 0; i < numbers.length; i++){
System.out.println("numbers[" + i + "] = " + numbers[i]);
}
}
}

/** Uses a JOptionPane input dialog to request a number string from the
* the user, providing the user an input message. Checks to ensure the
* number input is in the range required by the problem. Loops until
* range requirement is met.
*/
private int getNumberInRange(String inputMessage){
int n = -1;
while ((n < min) || (n > max)){
String input = JOptionPane.showInputDialog(null, inputMessage);
try{
n = Integer.parseInt(input);
if ((n < min) || (n > max)) JOptionPane.showMessageDialog(null,
"That was an invalid integer. Try again.",
"Error", JOptionPane.ERROR_MESSAGE);
}catch(NumberFormatException nfe){
n = -1;
System.err.println("NumberFormatException: " + nfe.getMessage());
JOptionPane.showMessageDialog(null, "That was invalid. Try again.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
return n;
}

/* Test a number to see if in input array.
*/
private boolean testNumber(int n, int[] array){
boolean testStatus = false;
if (array != null){
for (int i = 0; i < array.length; i++){
if (n == array[i]) testStatus = true;
}
}
return testStatus;
}

/** Creates a new array from intarray adding an element with value n.
*/
public int[] addArrayElement(int n, int[] intarray){
int[] newarray = new int[intarray.length + 1];
for (int i = 0;i < intarray.length;i++){
newarray[i] = intarray[i];
}
newarray[intarray.length] = n;
return newarray;
}

}

Flat View: This topic has 9 replies on 1 page
Topic: re-connecting the server with the received cookie value Previous Topic   Next Topic Topic: LAN authentication in intranet application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use