• 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

Character Arrays from String Objects

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to this forum and new to Java...preparing for the SCJP..I will be needing a lot of help around here.It is such a genuine and helping place.Liked it a lot.My doubt of the day is:
I am trying to change a String array to a character array ,it works fine.But when I reverse the character array and then try to change it back to a String.It does not print anything.Can anybody tell me why?Line (8) prints fine,the problem is from line (9) onwards...Thanks in advance
Sneha Jacob
public class text (1)
{ (2)
public static void main(String[] args) (3)
{ (4)
String text = "To be or not to be"; (5)
char[] textArray = text.toCharArray(); (6)
String charStr = String.copyValueOf(textArray); (7)
System.out.println("The character array is" + charStr); (8)

for(int i =0; i< textArray.length;i++) (9)
{ (10)
++count; (11)
} (12)
char[] revtextArray = new char[count];
for(int i = textArray.length;i<=0;i--)
{
int j=0;
revtextArray[j] = textArray[i];
++j;
}

String revStr = String.copyValueOf(revtextArray);
System.out.println("The reverse array is" + revStr);
}
}
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From a first glance, you 're putting everything into
revtextArray[0] in the for loop. j++ does nothing because you reinitialize j to 0 in every run of the for loop
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Alex said is true.
In addition, you should change:
<br /> to<br />
That should produce the effect you want.
April
[This message has been edited by April.Johnson (edited July 30, 2001).]
 
Sarah Jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for showing me the silly mistake I had done....Now I get it.
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to April's code ,
You'll get an exception if you don't replace textArray[i] with textArray[i-1] .
Peace
Ashish
[This message has been edited by Ashish Hareet (edited July 30, 2001).]
 
Sarah Jacob
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Asish,
Will I still get an exception if I replace the for loop as
for(int i = textArray.length-1;i>=0;i--)
{
revtextArray[j] = textArray[i];
++j;
}
Thanks
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope Sarah , will compile n run fine
My mistake , i was working with i > 0 & not i >= 0
With i > 0 it'll give error .
Copy n paste this in your 2nd for loop to get a clear picture of what/how the loop is working -
System.out.println("revtextArray["+j+"] = "+revtextArray[j]+" --->"+" i = "+i+" , j = "+j);
Peace
Ashish
 
reply
    Bookmark Topic Watch Topic
  • New Topic