1
import java.io.*;
import java.util.Scanner;

class FileReverse{
   public static void main(String[] args) throws IOException{

      //charAt(int index): Returns the char value at the specified index.
      //substring(int beginindex, int endindex): Returns a new string that is a substring of the string. 
      //valueOf(char c): Returns the string representation of the char argument

      Scanner in = null;
      PrintWriter out = null;
      String line = null;
      String[] token = null;
      int i ,n ;

      // check number of command line arguments is at least 2
      if(args.length < 2){
         System.out.println("Usage: FileCopy <input file> <output file>");
         System.exit(1);
      }

      // open files
      in = new Scanner(new File(args[0]));
      out = new PrintWriter(new FileWriter(args[1]));

      // read lines from in, extract and print tokens from each line
      while( in.hasNextLine() ){
         // trim leading and trailing spaces, then add one trailing space so 
         // split works on blank lines
         line = in.nextLine().trim() + " "; 

         // split line around white space 
         token = line.split("\\s+"); 

         // reverses the input and prints it to the console.
         n = token.length;
         for(i=0; i<n; i++){
            stringReverse(token[i],(n-1));
            System.out.println(token);
         } 
      }

      // close files
      in.close();
      out.close();
   }

   public static String stringReverse(String s, int n){
      if(n == 0){
         return s.charAt(0) + "";
      }

      char let = s.charAt(n);
      return let + stringReverse(s,(n-1));
   }
}

We are given a file with this input

abc defg hi

jkl mnop q
rstu v wxyz

and it must be returned as

cba
gfed
ih
lkj
ponm
q
utsr
v
zyxw

My code compiles but I keep getting an indexoutofboundsexception and I can't figure out to fix this. Help would be greatly appreciated!

5
  • What list throws the exception? Can you include the stacktrace please? Commented Apr 13, 2016 at 5:45
  • 1
    i think your output is wrong, where does the g in gfed come from? Commented Apr 13, 2016 at 5:46
  • 1
    Yes, the output seems either incorrect or different "waxy" becomes "zyxw" too Commented Apr 13, 2016 at 5:49
  • Reversing a string is the same whether it is in an array or not. Commented Apr 13, 2016 at 5:49
  • 1
    As Java strings are immutable, invoking stringReverse(token[i],(n-1)); is pointless anyway. You need to do something with the returned value. Commented Apr 13, 2016 at 5:52

2 Answers 2

1

You are splitting the String at each whitespace. in the following code

n = token.length;
for(i=0; i<n; i++){
    stringReverse(token[i],(n-1));
    System.out.println(token);
}

you check how many elements where seperated by whitespaces. But the mistake you are making is, that you are parsing n-1 to the function Stringreverse (aside of the fact that you don´t store the return value). You are using the second argument as the length of the String, but what you are doing currently is, you are passing the amount of array elements that you got returned after splitting the initial String by whitespaces. You should call it as:

for(i=0; i<n; i++){
    String reversed = stringReverse(token[i],token[i].length()-1);
    System.out.println(reversed);
} 
Sign up to request clarification or add additional context in comments.

4 Comments

it seems like it is giving me the addresses: [Ljava.lang.String;@232204a1 [Ljava.lang.String;@232204a1 [Ljava.lang.String;@4aa298b7 [Ljava.lang.String;@7d4991ad [Ljava.lang.String;@7d4991ad [Ljava.lang.String;@7d4991ad [Ljava.lang.String;@28d93b30 [Ljava.lang.String;@28d93b30 [Ljava.lang.String;@1b6d3586
@MonserratPalabrica did you create a class and named it String?
oops sorry I did not do that! I have done that now and it prints out abc as cba but it prints defg as just fed and then I get an arrayindexoutofboundsexception cba fed Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at FileReverse.main(FileReverse.java:41)
@MonserratPalabrica replace the for loop with the one i wrote at the end of the question and the exception should be gone.
0

Replace

for(i=0; i<n; i++){
    stringReverse(token[i],(n-1));
    System.out.println(token);
 }

from

for(i=0; i<n; i++){
   token[i]= stringReverse(token[i],token[i].length()-1);
   System.out.println(token[i]);
}

You have to pass the each string size (not the taken array size) to the stringReverse method.

I am not sure why did you use following code inside the for loop

System.out.println(token);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.