mpa
mpa

Reputation: 43

How to reverse a string within an array?

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!

Upvotes: 1

Views: 171

Answers (2)

Geeth Lochana
Geeth Lochana

Reputation: 164

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);

Upvotes: 0

SomeJavaGuy
SomeJavaGuy

Reputation: 7357

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);
} 

Upvotes: 1

Related Questions