• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read a line of text and reverse each word do not use StringTokenizer

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried a lot but i am not able to remove the ArrayIndexOutOfBounds Exception at runtime but it compiles just fine

import java.util.*;
public class Rev {
static void rev(String jik){ //method to reverse the given string
int i,j;
char ji[]=new char[jik.length()];

for(i=0,j=jik.length();i<jik.length()&&j>=0;j--){
ji[i]=jik.charAt(j);
i++;
}
String jk=ji.toString();

System.out.println(jk+" ");
}
public static void main(String[] arg){
int i,start,fin;
start=0;

String str;
Scanner in = new Scanner(System.in);
str=in.nextLine();
for(i=0;i<str.length();i++){
if(str.charAt(i)==' '){
fin=i-1;
String ut=str.substring(start, fin);
rev(ut);
start=i+1;

}


}
}
}

>
 
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harish! Welcome to javaranch.

Vinoth gave a perfect code to implement what you want. Although this is a small thing but I want to point this out. You can convert the char array back into string using the string constructor. Also there is a small typo in vinoth's program so lemme give you the code again

 
harish thiru
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks dude for both of your help but now i am not getting output though exceptions are not coming

import java.util.*;
public class Rev {
static void rev(String jik){
int i,j;

char one[]=jik.toCharArray();
char two[]=new char[one.length];
for(i=0,j=one.length-1;i<one.length&&j>0;i++,j--){
two[i]=one[j];
}
String b = new String(two);

System.out.print(b+" ");
}
public static void main(String[] arg){
int i,start,fin;
start=0;

String str;
Scanner in = new Scanner(System.in);
str=in.nextLine();
for(i=0;i<str.length();i++){
if(str.charAt(i)==' '){
fin=i-1;
String ut=str.substring(start, fin);
rev(ut);
start=i+1;

}


}
}
}


>
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't thin k you have helped him at all, I am afraid. Simply giving a solution means he hasn't worked out things for himself and doesn't know why the Exception occurred.
 
harish thiru
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey ritchie i understood why exception occurred because i used jik.length() and not jik.length()-1 for j initialisation

String a="test";
char one[]=a.toCharArray();
cahr two[]=new char[one.length];
for(int i=0,j=one.length-1;i<one.length&&j>=0;i++,j--){ it shud be j>=0; anyway thnks dude
two[i]=own[j];
}



 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic