• Post Reply Bookmark Topic Watch Topic
  • New Topic

Store string input in a array and print them in reverse order  RSS feed

 
ivan auyoung
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a college question ask me to write a class StringRevert which does the following:
promting user for an interger n
creating an array of n string
repeatedly read character string from user input and store them in the array until end of array is reached or user inpu quit(quit should not saved in array)
print the string from array in reverse order, from last enter to first enter.
assume user always supplie correct input

this is what iver done so far but i have no idea how to get it working.



thank you for any help
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivan auyoung wrote:I have a college question ask me to write a class StringRevert which does the following...

I'm not quite sure what you mean. If I enter:
Hello
World
do you want:
World
Hello
or:
dlroW
olleH
?

Winston
 
ivan auyoung
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
ivan auyoung wrote:I have a college question ask me to write a class StringRevert which does the following...

I'm not quite sure what you mean. If I enter:
Hello
World
do you want:
World
Hello
or:
dlroW
olleH
?

Winston

i want:
World
Hello

thanks
 
Preethi sakar
Greenhorn
Posts: 19
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivan auyoung wrote:I have a college question ask me to write a class StringRevert which does the following.....


Use this code surely you will get answer....



output:

Enter number of string
2
Enter 2 String :
hello
world
Reverse order for given String
world
hello


I hope this code will be helpful.......
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi's implementation is correct and functional...

If you would like a cleaner implementation you can also use the following:



Explanations are embedded within the code by use of end-of-line comments. Any further explanation needed can be requested
 
Preethi sakar
Greenhorn
Posts: 19
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Preethi's implementation is correct and functional...

If you would like a cleaner, professional implementation you can also use the following........


Thanks for your guidance..

s I have doubt in end of line i can understand what code will display but what is purpose of using conditional statement over there



thanks in advance...

Have a nice day
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivan auyoung wrote:i want:
World
Hello
thanks

Then what you need is to process your array of Strings (once you have it) in reverse order; and if you don't know how to do that, then you need to read the tutorials, rather than simply parrott ready-made solutions.

I also got 8.8 million results when I Googled "Java array processing".

This is a very simple problem, Ivan. I suspect you're overthinking it because you're thinking about getting the strings AND processing them, when the only thing you (apparently) don't know how to do is process them in reverse order.

Break things down into simple pieces. It's an essential part of programming.

Winston
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi sakar wrote:

Thanks for your guidance..

s I have doubt in end of line i can understand what code will display but what is purpose of using conditional statement over there



thanks in advance...

Have a nice day


The purpose for the conditional is to format the output. If I use tokens[i] the strings will be jammed together and if I use tokens[i] + " " then there will be unnecessary white space in the output.
 
Preethi sakar
Greenhorn
Posts: 19
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:

The purpose for the conditional is to format the output. If I use tokens[i] the strings will be jammed together and if I use tokens[i] + " " then there will be unnecessary white space in the output.


ok!!! thanks Rico but i have another doubt also



what is purpose of this if condition??

thanks in advance
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi sakar wrote:

ok!!! thanks Rico but i have another doubt also



what is purpose of this if condition??

thanks in advance


The purpose of this condition was used to provide the following requirement:

ivan auyoung wrote:repeatedly read character string from user input and store them in the array until end of array is reached or user inpu quit(quit should not saved in array)


The loop inside the condition was used to clear out any null references remaining if the user quits without supplying a value for each index in the array as those would have been part of the output which is not desirable.

Example: If the size of the array is 7 and the user supplied values up to index 4, the output would be null null null user_value user_value user_value user_value when the desired output is suppose to be user_value user_value user_value user_value
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:The loop inside the condition was used to clear out any null references remaining if the user quits without supplying a value for each index in the array as those would have been part of the output which is not desirable.

Example: If the size of the array is 7 and the user supplied values up to index 4, the output would be null null null user_value user_value user_value user_value when the desired output is suppose to be user_value user_value user_value user_value

There's a much easier way to do that


And your method for printing st, nd or th will only work up to 20, but I'll leave it for the OP to work out how to improve that.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
There's a much easier way to do that




This is sleek

Joanne Neal wrote:And your method for printing st, nd or th will only work up to 20, but I'll leave it for the OP to work out how to improve that.


I was being lazy here because I figured for an assignment testing won't go beyond 20.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:And your method for printing st, nd or th will only work up to 20, but I'll leave it for the OP to work out how to improve that.

@Ivan: And I would say that a method (a proper method) is exactly what you want for something like this. Just one possibility:I'll leave you fill in the details.

You could possibly make it even more generic by making 'n' a Number (java.lang.Number); but there might be a bit more logic involved.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!