• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to reverse the string in the loop  RSS feed

 
Itzel Coronado
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




[HENRY: Added Code Tags]
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Itzel Coronado,

When you post your code, very important to UseCodeTags. Hopefully moderators will add them for you this time.

Beside that, there are some mistakes in your code. You might still in progress of building it.

Do you have any specific questions about it?
 
Itzel Coronado
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, so my terminal doesn't tell me it has any errors, i can run the program, but once i put in a string it will tell me:

Enter the string you would like to reverse:
heloo you
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10
at java.lang.String.charAt(String.java:646)
at ReverseString.main(ReverseString.java:15)

i have no idea how to fix that.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Welcome to the Ranch.

As Liutaurus has already pointed out if you UseCodeTags it makes it much easier to read.

For your first bug, StringIndexOutOfBounds means that you are referring to a character that is "off the end" of the string.
It is important to remember that the first character of a string is at index 0.
And the last character of a string is...
So given the string "Hello", it is 5 characters long, numbered from 0 to 4. 'H' is character 0, 'o' is character 4.



Your loop is missing one very important step.
In every loop you write to iterate over something you must have
- a starting place
- an ending condition
- code that runs each time through the loop, that can make the end condition come true. Otherwise it will loop forever.

I'm guesssing right now that your program is running forever (or at least until the computer runs out of memory)
 
Ash Jon
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch

This should help:



Hope it helps
 
Itzel Coronado
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i initialize the variable?
 
Ash Jon
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hope it helps
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"initializing a variable" means give it a starting value. So assign it a value.

So at the start of your loop you want
- your result string to be empty with no characters in it
- your index to refer to the last character in the string.

 
Itzel Coronado
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, i don't even know how to do that, how do i make the index refer to the last character in the string?

int index;
index = sentence.length();
String reverse_sentence="";
while (index>=0)
{
reverse_sentence = reverse_sentence + sentence.charAt(index);
index—;
}
System.out.println (reverse_sentence);
}
}


can i finish the loop by putting index--; or is there anther way i have to finish the loop?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Itzel Coronado wrote:I'm sorry, i don't even know how to do that, how do i make the index refer to the last character in the string?


Well, let's see if you can work it out...

If the string length is 10, meaning the length() method returns 10. And obviously, the first character is at position zero. So, what is the position of the last character?

Henry
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Itzel Coronado,

Once again, please UseCodeTags when you post your code. It makes easier to read it.

When you copy/paste, simply select your code and click button "Code" (picture below).



Screen-Shot-2015-03-20-at-00.50.59.png
[Thumbnail for Screen-Shot-2015-03-20-at-00.50.59.png]
How To
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Itzel Coronado wrote:can i finish the loop by putting index--; or is there anther way i have to finish the loop?


Probably I couldn't explain more clear than this:
While loop
For loop
With both loops you can achieve actually the same result. In your case you can use one or another, whichever is easier for you to understand.

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a word processor to enter your source code? Because this:



isn't going to work. What probably happened is you typed "--" and the word processor decided you needed an endash and replaced it. What you need is



Don't use word processors to enter code. Use vi or notepad or better yet Notepad++ if you're on Windows.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!