View Single Post
  #24 (permalink)  
Old 04-30-2008, 02:23 AM
apfroggy0408 apfroggy0408 is offline
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
So I've decided to abandon the array ship, and just use the different String methods.

Here's what I have.

Code:
import java.util.*; public class p499proj12l2 { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("Please enter word: "); String sent = input.nextLine(); String trimsent = sent.trim(); int length = trimsent.length(); int gate; int index; do{ System.out.println("Enter index wanted: "); index = input.nextInt(); int b = length - 1; if (index > b) { System.out.println("Index doesn't exist"); gate = 0; } else { gate = 1; } } while(gate != 1); Recurse(trimsent, index, length); } public static String Recurse(String trimsent, int index, int length) { if(index <= length) { Recurse(trimsent, index + 1 , length); char chars = trimsent.charAt(index); System.out.print(chars); } else { System.out.println(); System.out.println(trimsent); } return null; } }
What the code is supposed to do is ask for a word and an index. At the entered index the word is supposed to be spelled backwards from that spot using a recursive method.

i.e. shoe index 1 = seoh.

I'm having trouble just with my Recurse method.

Because for some reason I get these results...

Shoe , index 1 = eohs
Shoe , index 2 = eoh
Shoe , index 3 = eo

Now I see the pattern, but I'm confused on how it's getting there.
My else statement always runs first too for some reason unbeknownst to me...

Last edited by apfroggy0408 : 04-30-2008 at 02:42 AM.
Reply With Quote