• 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

Need String program

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi This is Sunil, i'm new to java,
i wanted one string program i.e.

Ex: input something like "asdfghjlk"
i wanted to out put like "asd" , "ahj" and revesre of input..
please do needful,,,
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us what have you done so far, let us know where you are stuck and we'll try to help you.
Please note that we don't hand out ready made solutions here. Read this and this.

Hint: For your query, you might want to have a look at the java.lang.String class and its methods.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer the below link. You can get all the in-built String operations here
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sir,
I'm not getting how to write code for this.....
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:Hi sir,
I'm not getting how to write code for this.....


Did you refer to the String class? Did you have a look at the methods there? Did you find anything useful?
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In interview they asked me to write code by using String calss..
they had given String str = "asdfghjlk";
they wanted output like "asd" , "ahj" and revesre of input..
but i didt solved this one,

they told me to write 3 different programs for these 3 outputs.
 
Siva Arun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:In interview they asked me to write code by using String calss..



It seems to be you are new to Java. I am wondering how did you attend interview without complete knowledge.
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please send me the programs. i want to check....
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:please send me the programs.


As I've already mentioned earlier, we don't hand out ready made solutions here. Please show some effort.

Same hint again: See if you can make use of any of the String manipulation methods in the java.lang.String class.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:please send me the programs. i want to check....



Sunil,
Like you have been told before, if you are looking for ready made code, you are at the wrong place.
Show us, what you have got and we will help put it right.
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Fine
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read these FAQ entries:
NotACodeMill
DoYourOwnHomework
ShowSomeEffort (already mentioned by Pushkar Choudhary)
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, please tel me the logic how to display only "bgr" form the below string
String str = "bangalore";

Thank you
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you made any attempt to start? Let us see what you've done so far.

Same hint for the third time: See if you can make use of any of the String manipulation methods in the java.lang.String class.
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have gone reverse string by using" String reverse = new StringBuffer(string).reverse().toString();"
and displaying starting 3 character by using substring=str.substring(0,3);
now i want to display "bgr" from string "bangalore".. i'm not getting any logic
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:now i want to display "bgr" from string "bangalore"..


Is there a pattern to get the output by selecting some specific index from the given string? If yes, you could try using the charAt(int index) method and then append the chars into a String.
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i use charAt(int index) means it display only single character
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:If i use charAt(int index) means it display only single character


Did you read my last comment completely?
If not, here it is again:

Pushkar Choudhary wrote:you could try using the charAt(int index) method and then append the chars into a String.

 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For append we have to use stringbuffer
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:For append we have to use stringbuffer


java.lang.StringBuffer or java.lang.StringBuilder.
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not getting sir
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil ajay wrote:I'm not getting sir


I'll ask my earlier question again. Have you made any attempt to code? Show us what you've done so far.
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class xyz
{
public static void main(String[] args)
{
String string="bangalore";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println("String after reverse:"+reverse);
substring=str.substring(0,3);
System.out.println("string :"+substring);
}
}
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when you post any code.

Code looks okay. What is the problem you are facing now? Is it that you want to get the String "bgr" from "bangalore"? What have you tried for achieving that?
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in one interview they had told me to write. i didt wrote. i'm nt getting that logic so asking
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the output.....
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. Let's go step-by-step.
First of all, I don't think any method from the java.lang.String class would readily give you "bgr" from "bangalore" (Somebody correct me if I'm wrong here).

There are 2 ways you can achieve this (maybe more, but of the top of my head, I can think of only these 2):
1. Use the substring() method to get the "b", "g" and "r" separately and then append them to get "bgr". You already know the substring() method.
2. Use the charAt() method to get 'b', 'g' and 'r' separately as characters and then convert them into Strings and append them to get "bgr".
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops...! Too late...
But glad you got the result.
 
sunil ajay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic