Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java program

 
Shreegowri Bhojanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need a program that gives largest distance of occurrence of a given character in a given string.
For example, Given string "my name is samitha" and given a character "a" pgm should output 7.
I tried but got error.

Thanks in advance.
 
Tim Cooke
Sheriff
Pie
Posts: 3131
135
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't really a job discussion so I moved it from that forum over to Java in General.

Shreegowri Bhojanna wrote:I tried but got error

Tried what?

You're not going to get a ready made answer here but if you show us what you've tried so far and explain what the current problem is that you are facing then you are more likely to get some constructive help.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example is great, but it doesn't really make a spec. Why 7 and not 13?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect the String.indexOf and lastIndexOf methods to come in handy.

Although if 7 is a possible solution, then 12 would be more likely to be another one than 13. It all depends on how "distance" is defined. So we really need that spec, like Fred said.
 
Shreegowri Bhojanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:An example is great, but it doesn't really make a spec. Why 7 and not 13?



Hi, I have the same doubt but it was the question asked for me in one of the online java exam.
And it was like i should get the same answer they specified.
 
Paul Murray-Cbr
Greenhorn
Posts: 12
Java Mac Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he must mean 'the greatest distance between any two consecutive occurrences.

One way to deal with these sorts of problems is to ask "how would I go about doing this if I did not have a computer?" Then get your computer to do it that way.
 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shreegowri Bhojanna wrote:Hi, I have the same doubt but it was the question asked for me in one of the online java exam.
And it was like i should get the same answer they specified.

What online Java exam?

I'm afraid we aren't mind-readers Shreegowri, so you need to supply information. Otherwise, we're just shooting in the dark.

However, I'd definitely follow Paul's advice: Forget all about Java, and work out how YOU would do it.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said "I tried but got an error". That doesn't tell us much. There are something like one million ways you could "try and get an error". If we start guessing - i.e. saying "did you do this? how about this? Well, maybe this?", it will take us a LONG time to find the solution to your problem.

so, please post your code. Tell us EXACTLY what happens when you compile and/or run it. Your job is to provide the details and make it as easy as possible for us to help you. I would suggest you read some of our How To Ask Questions FAQ first.
 
Shreegowri Bhojanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k k, I got it.I found out the error, while passing parameter i used String instance instead of BufferedString.
Anyhow thank you for all suggestions next time while posting first i will post my code.
And sorry to all of you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a BufferedString?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic