• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String class: simple yes or no question here

 
Mel Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assign the index of the first occurrence of the character 'c' in s1 to an int variable k.

So I need to right just a small portion of this program, but I do not know what it means "to an int variable k." Is this what it means?

String s1 = "cacoon";
System.out.println(s1.indexOf('c'));
 
Paul Clapham
Sheriff
Posts: 21002
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you didn't use the letter "k" anywhere in that... oh sorry, you just wanted a yes or no answer. Okay. Then the answer is no.
 
Mel Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, you didn't use the letter "k" anywhere in that... oh sorry, you just wanted a yes or no answer. Okay. Then the answer is no.


Usually in math or in programming, when they say k, it refers to any integer... that short program returns and prints an integer of 0. My question is whether the k that is referring to is what the program returns and prints OR something else that I can even form the question.

Any help? THanks!!
 
Paul Clapham
Sheriff
Posts: 21002
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mel Ram wrote:Usually in math or in programming, when they say k, it refers to any integer... that short program returns and prints an integer of 0.

Yeah, it does, doesn't it? So why did you write that program? It doesn't seem to have much to do with your original post where you said
Assign the index of the first occurrence of the character 'c' in s1 to an int variable k.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the 'assignment' operator in java is the '=' symbol. you use this to assign a value to a variable.

when you decleare a variable, you must have two things - a type and a name. for example, you could have a 'double' type variable named 'fred'. you would declare it like this:

double fred;

if i then wanted to assign a value to it, i would us the operator mentioned above:

fred = 7.3;

i can also assign the value of one variable to another:

double bob;
bob = fred;

and, i can declare and assign a value in one step:

double george = 17.0;

note that anything that returns the correct type can be used on the right-hand side: a literal (like '12'), another variable (if it's the correct type) or even a method:

george = getValueOfPi();

(assuming there is such a method).
 
Mel Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Mel Ram wrote:Usually in math or in programming, when they say k, it refers to any integer... that short program returns and prints an integer of 0.

Yeah, it does, doesn't it? So why did you write that program? It doesn't seem to have much to do with your original post where you said
Assign the index of the first occurrence of the character 'c' in s1 to an int variable k.


Well, because that is what I got from it. So I am guessing its not so could you point me in the right direction as to how to find the solution. I am not understanding the problem, evidently. lol

Thanks!
 
Mel Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:the 'assignment' operator in java is the '=' symbol. you use this to assign a value to a variable.

when you decleare a variable, you must have two things - a type and a name. for example, you could have a 'double' type variable named 'fred'. you would declare it like this:

double fred;

if i then wanted to assign a value to it, i would us the operator mentioned above:

fred = 7.3;

i can also assign the value of one variable to another:

double bob;
bob = fred;

and, i can declare and assign a value in one step:

double george = 17.0;

note that anything that returns the correct type can be used on the right-hand side: a literal (like '12'), another variable (if it's the correct type) or even a method:

george = getValueOfPi();

(assuming there is such a method).


OK so what the problem is saying assign the integer k to a value let say 12... and this will replace the character 'c' to the number 12. So it would print out 12acooon. Is that right?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mel,

What is the output of your two lines of code?

If you need to assign the int value of the first instance of a variable, you need to use the "=" operator, as fred said -- instead of using the println() method. I would say "the variable k equals the integer result of the "indexOf" method when s1 is the parameter."

The application will only output 0cacoon (not 12cacoon) if you say this:

 
Mel Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:Mel,

What is the output of your two lines of code?

If you need to assign the int value of the first instance of a variable, you need to use the "=" operator, as fred said -- instead of using the println() method. I would say "the variable k equals the integer result of the "indexOf" method when s1 is the parameter."

The application will only output 0cacoon (not 12cacoon) if you say this:



aha! That's what is reffering to!! I just didn't understand what the question stated. I got it now. awesome! thanks a lot to everyone!!
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I agree with it printing anything...Here is the assignment as stated:

Assign the index of the first occurrence of the character 'c' in s1 to an int variable k.

so lets break it down..
Assign <something> to an int variable k.

this says to me i need this:

now I need to figure out what that <something> is. let's just focus on that:
the index of the first occurrence of the character 'c' in s1

let's work backwards:
s1

i assume is a string
the character 'c' in s1

well...there might be many such characters
the first occurrence of the character 'c' in s1

Ah...so now i need to find where the FIRST 'c' character is in the string. There's probably a method for that in the String class.
the index of the first occurrence of the character 'c' in s1 to an int

A string is sort of like an array of characters. The first letter in the string is at position '0', the second is at position '1', etc. the 'index of' tells me I want to know what position the letter is in. so in the string
Fred is a Java developer

The index of the first occurrence of the character 'e' is at 2. 'F' is at 0, 'r' is at 1, and 'e' is at 2.

So if the assignment was "Assign the index of the first occurrence of the character 'e' in "Fred is a Java developer" to an int variable f" when I was done, I'd expect f to have the value 2. I wouldn't be changing the string at all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic