• 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

Java String printing

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question is asked in my interview

Suppose that i have a string name
String str="Sudhir Kumar Ojha";
Now i want to print only SKO w ithout using methods
 
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhir Kumar Ojha wrote:The following question is asked in my interview

Suppose that i have a string name
String str="Sudhir Kumar Ojha";
Now i want to print only SKO w ithout using methods



without using String methods?
 
Sudhir Kumar Ojha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Exactly

mainly charAt(int index); method
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhir Kumar Ojha wrote:
mainly charAt(int index); method



Pretty straightforward question. How did you answer it during the interview?

Henry
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhir Kumar Ojha wrote:Yes Exactly

mainly charAt(int index); method



Alternatively, you can use split and then substring to get Initial alphabet of each word. java.lang.String
 
Sudhir Kumar Ojha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can  you write a code
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhir Kumar Ojha wrote:Can  you write a code



Below is the code.


 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhir Kumar Ojha wrote:Now i want to print only SKO w ithout using methods

Not even a signle method?  like we can do that in C language using only loop?
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Khalid wrote:without using String methods?


Sudhir Kumar Ojha wrote:Yes Exactly
mainly charAt(int index); method


M Khalid wrote:

Don't you think, you still used .split() and .substring() methods of String, what do you say?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:Don't you think, you still used .split() and .substring() methods of String, what do you say?



Well, M Khalid did say "alternatively", so this is a side discussion, and not an answer to the interview question... which only allowed the charAt() method.

Henry
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:which only allowed the charAt() method.

Ok, you mean to say we are allowed to use charAt() method ?
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:

Henry Wong wrote:which only allowed the charAt() method.

Ok, you mean to say we are allowed to use charAt() method ?


Sudhir Kumar Ojha wrote:
Yes Exactly
mainly charAt(int index); method




You are not allowed to user charAt method primarily.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm.... good point. I think I may have misread the question.... here's a few cows for the confusion that I caused.

Henry
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to me that the question is ambiguous. What does it mean "without using methods" Does that mean "Without using any methods, of any class, period?" if so, the question is ridiculous. You can't do it, since you have to use a method to print anything.

Does it mean without using ANY method of String? Then you can create a StringBuffer and proceed from there. Or does it mean "you can't use charAt(), but you can use other methods?" I would have asked the interviewer to clarify the question.
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Interviewer might intentionally ask you not even using single method of String Or StringBufffer Or StringBuilder and try to confuse you and make you use logic like we do in C where String is an array of chars which is not in case of Java.
  • If allows few methods then, this is what so far I tried, there could be more ways to do so..
  • Output:
    Using charAt method only
    SKO
    Using getChars() and length() methods
    SKO
    Using split(\s) method only
    SKO
     
    Sudhir Kumar Ojha
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You all taking wrong i mean to say that not use charAt() method in this program.



    And i dont have any answer at time of interview so i simly skiped it
     
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If we can't use String's charAt() method, can we use StringBuilder's charAt() method? If so, we can use the String literal to create a StringBuilder.
     
    Sudhir Kumar Ojha
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Daniel Cox


    In interview HR ask like this


    You have a string like this
    String strr="SUdhir Kumar Ojha";
    Now you have to print only SKO without using methods specially charAt() method of String class.

    I hope now you fully understand this question.
     
    Fred Kleinschmidt
    Bartender
    Posts: 732
    10
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This interview question is one of those of the form "How would you do this in a way other than the correct way?" I would be tempted to answer it "I wouldn't. I would do it the correct way." Presumably it is meant to test the applicant's knowledge of other ways of doing things. In that case, I would answer "I would look up the documented API to see if there was another and possibly better way."

    No one should be expected to be intimately familiar with all methods of all classes in the Java libraries. Asking an applicant how to do something in a roundabout way is just silly, and demonstrates that that company is clueless when it comes to software development.
     
    Sudhir Kumar Ojha
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well thanks guys and specially Fred Kleinschmidt because i also reply same in my interview and i say him i can not do it without using methods.
     
    Daniel Cox
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I hope that that was the question that was asked. I find it strange that a sensible company asked such a silly question. You can't do anything without calling a method. At the very least, you must call the main method.
     
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    HR asked you a coding question ?
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I know off topic  but couldn't stop praising someone's art, I visited Salvin.in It was just awesome. Great work Salvin!
     
    M Khalid
    Ranch Hand
    Posts: 100
    2
    Python Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It is silly question, but i guess the interviewer didn't convey his/her question properly.
    I guess it would be like without using String methods directly or different ways to get the result.
    because in my early interviews i was also asked questions like
    "How to reverse a String without using String methods directly"
    For Eg: String is "Java" so we can use StringBuilder method reverse to directly reverse the string in a single line of code.
    but instead interviewer expectation was ( IMO ) the alternative ways , like storing string into char array and then looping backwards to get the reverse string.
    Hope that's the case here.

     
    M Khalid
    Ranch Hand
    Posts: 100
    2
    Python Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganesh Patekar wrote:

  • Interviewer might intentionally ask you not even using single method of String Or StringBufffer Or StringBuilder and try to confuse you and make you use logic like we do in C where String is an array of chars which is not in case of Java.
  • If allows few methods then, this is what so far I tried, there could be more ways to do so..
  • Output:
    Using charAt method only
    SKO
    Using getChars() and length() methods
    SKO
    Using split(\s) method only
    SKO



    Exactly , interviewer needs alternative methods. Your second method (above) is what i guess was expected in such interview questions.
     
    Daniel Cox
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    M Khalid wrote:Exactly , interviewer needs alternative methods.


    I think Sudhir Kumar Ojha made it clear that no methods are allowed. Not even the main method.
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In college days, I really adored such programs dealing with Strings which is an array of chars in C language but in Java there are so many already built in methods for that so I would have much appreciated interviewer If he/she had asked question regarding String literal pool concepts Or Why Strings are so special Or immutable in Java...
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic