3 Replies - 2607 Views - Last Post: 30 September 2009 - 08:58 AM Rate Topic: -----

#1 situation  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 28-October 08

how to print a single character every time?

Posted 30 September 2009 - 06:03 AM

I would like to ask
how can I print a single character from a string every time??

for example
in array[1] , a word "language" is stored

I would like to print a single reversed character every time
ie,
the output is
e
g
a
u
g
n
a
l

however, I print the wrong result
I know I should not use substring
But I don't know what method can fit my requirement

So, I hope people can help me
Thank you very much!

				for ( int y = 0; y < tempStr[20].length() - 1; y++ )
				System.out.println(tempStr[20].substring(tempStr[20].length()- y));




Is This A Good Question/Topic? 0
  • +

Replies To: how to print a single character every time?

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: how to print a single character every time?

Posted 30 September 2009 - 06:21 AM

you need another loop
for ( int y = 0; y < tempStr[20].length(); y++ )
	for(int x = tempStr[y].length()-1; x >= 0; x--)
				System.out.println(tempStr.charAt(x));


Was This Post Helpful? 0
  • +
  • -

#3 situation  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 28-October 08

Re: how to print a single character every time?

Posted 30 September 2009 - 08:29 AM

View Postmostyfriedman, on 30 Sep, 2009 - 05:21 AM, said:

you need another loop
for ( int y = 0; y < tempStr[20].length(); y++ )
	for(int x = tempStr[y].length()-1; x >= 0; x--)
				System.out.println(tempStr.charAt(x));


thank you for your reply first

when I compile my program,
an error message is shown

ps, I have import java.io.*;

Quote

cannot find symbol
symbol : method charAt(int)
location: class java.lang.String[]
System.out.println(tempStr.charAt(x));

Was This Post Helpful? 0
  • +
  • -

#4 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: how to print a single character every time?

Posted 30 September 2009 - 08:58 AM

Alright, if it were me, I would do it this way.
First of all, make "language" a String. Then make separate all the characters into a char array with toCharArray() method. Then use a for loop to reversely loop and print out the characters.
It should look like this I guess.
String str = "language";
char[] chArr = str.toCharArray();
for(int i = chArr.length - 1;i >= 0;i--)
   System.out.println(chArr[i]);



I guess it should look like that. Didn't try out that code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1