• Post Reply Bookmark Topic Watch Topic
  • New Topic

reverse a string/check functionality of methods

 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to write a bunch of different methods, and i solved all except two. the first must be able to take a String from the user and reverse it. here's what i have so far:



right now, it's returning the empty space to which rev was initialized, i think, but i don't know why.


the second involves returning an array of ints containing 1001, 2002, 3003...to the n. i have this:




also, i wrote two methods that take an array of numbers, and i want to test them, but when i run them, it just allows me to type in numbers continually. i want to make sure they work:

 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Your first query, the following code doesn't return you reversed string


simple logic change your for loop condition,
initialize ii with nextLine.length-1
condition >= zero
with ii--

Could you make clear with your second query
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samrat Gadamsetty wrote:
simple logic change your for loop condition starting from nextLine.length-1 and the condition till zero with ii--


i don't think i understand what you're saying here. i need to change the for loop so it reads:



what do you mean by " the condition till zero with ii"?

second question: i need to write a method that returns an array of ints. those ints must begin at 1001, then 2002, then 3003 (so just adding 1001) an undefined (n) number of times.

third question: since the last two methods i posted don't print anything on their own, how can i test them to make sure i wrote them correctly? right now, i'm testing them with this code:



however, when i run it, the program just allows me to enter numbers continually. it doesn't print anything. does that mean my method is incorrect in some way?
 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i don't think i understand what you're saying here. i need to change the for loop so it reads:



it should be


 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...if that's the only change i was supposed to make, it didn't work. it still returned the empty space, and i'm relatively sure my for loop isn't the issue. well, okay, not the ONLY issue.
 
Samrat Gadamsetty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...but that works for me



the above code prints "tarmas"

still if it didn't work, post the code where you call rev function

 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm taking user input Strings to be reversed using this:



i feel like i need to create an array or something...but that could most definitely be wrong.

EDIT: wait, i'm getting somewhere.



now, when prompted, i type rev followed by whatever i want reversed, spaces and all, and it works. so now i just need help with my other questions.

EDIT 2:

no, wait, now when i test rev, it just returns a space followed by whatever i typed in without reversing the order...what?
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless of the solution, please start using a StringBuilder. Using += to append to your String is quite inefficient. Your code, modified to use StringBuilder:
As you see the core stays the same, there are just four changes:
- declare rev as StringBuilder instead of String.
- initialize it with a new StringBuilder. The constructor I used will create an empty StringBuilder that will initially have a capacity for nextLine.length() characters.
- use the append method instead of +=.
- return rev.toString() instead of rev itself.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did it that way because i've never heard of StringBuilder. this is the way our prof. taught us to do it up until now, but if StringBuilder is more efficient, i'll use that, thanks.

EDIT: when i used what you gave me, it just printed a space followed by my input. i don't understand. sometimes, the code i wrote works; sometimes, it does what yours just did.
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob's example just demonstrates how to use a StringBuilder. He didn't incorporate Samrat's changes, which reads the String back to front and create a new String. If you are getting an empty String back sometimes from your rev() function, it means you are passing in an empty String. That is, maybe your scanner isn't working, or the way you are typing in input is a bit wonky. I'd put a println() at the top of the rev() method, so you can see what the input is.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, okay, i understand. i can fix that. i have one more question. the code i'm using for another method should allow the user to put in a number and get back a number in an array. iota (the method) at 0 would be 1001, at 1 would be 2002, at would be 3003, etc., and this array needs to help n numbers.



at the moment, if i enter IOTA followed by a number, i get back the number of 0s that corresponds to the value i entered. typing iota 4 gets me 0 0 0 0 because i have n initialized to 0.

therefore, i need to n needs to be the length of the input? i get an "exception in thread main" error when i try that. here's the code i'm using to test it:




what am i doing wrong?
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I don't get that. Your iota() method does some of the right things, but then returns null. In your calling code, the a.length should cause a NullPointerException. Are you sure you defined iota() that way?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!