File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Need String program Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Need String program" Watch "Need String program" New topic
Author

Need String program

sunil ajay
Greenhorn

Joined: Apr 09, 2008
Posts: 17
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,,,
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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.
Siva Arun
Greenhorn

Joined: Feb 23, 2010
Posts: 28
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


"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan.
sunil ajay
Greenhorn

Joined: Apr 09, 2008
Posts: 17
Hi sir,
I'm not getting how to write code for this.....
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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

Joined: Apr 09, 2008
Posts: 17
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

Joined: Feb 23, 2010
Posts: 28
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

Joined: Apr 09, 2008
Posts: 17
please send me the programs. i want to check....
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8439

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.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
sunil ajay
Greenhorn

Joined: Apr 09, 2008
Posts: 17
Ok Fine
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Please read these FAQ entries:
NotACodeMill
DoYourOwnHomework
ShowSomeEffort (already mentioned by Pushkar Choudhary)


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
sunil ajay
Greenhorn

Joined: Apr 09, 2008
Posts: 17
Hi, please tel me the logic how to display only "bgr" form the below string
String str = "bangalore";

Thank you
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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

Joined: Apr 09, 2008
Posts: 17
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

Joined: May 21, 2006
Posts: 425

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

Joined: Apr 09, 2008
Posts: 17
If i use charAt(int index) means it display only single character
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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

Joined: Apr 09, 2008
Posts: 17
For append we have to use stringbuffer
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

sunil ajay wrote:For append we have to use stringbuffer

java.lang.StringBuffer or java.lang.StringBuilder.
sunil ajay
Greenhorn

Joined: Apr 09, 2008
Posts: 17
I'm not getting sir
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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

Joined: Apr 09, 2008
Posts: 17
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

Joined: May 21, 2006
Posts: 425

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

Joined: Apr 09, 2008
Posts: 17
in one interview they had told me to write. i didt wrote. i'm nt getting that logic so asking
sunil ajay
Greenhorn

Joined: Apr 09, 2008
Posts: 17
i got the output.....
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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

Joined: May 21, 2006
Posts: 425

Oops...! Too late...
But glad you got the result.
sunil ajay
Greenhorn

Joined: Apr 09, 2008
Posts: 17
thank you
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Need String program
 
Similar Threads
Convert from char to byte?
key with combinations
How Can I do it without using if condition
Sending any string via cmd. code given
Defining a data type