aspose file tools*
The moose likes Beginning Java and the fly likes String class: simple yes or no question here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String class: simple yes or no question here" Watch "String class: simple yes or no question here" New topic
Author

String class: simple yes or no question here

Mel Ram
Greenhorn

Joined: Sep 30, 2009
Posts: 12
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
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

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

Joined: Sep 30, 2009
Posts: 12
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
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mel Ram
Greenhorn

Joined: Sep 30, 2009
Posts: 12
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

Joined: Sep 30, 2009
Posts: 12
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

Joined: Sep 14, 2009
Posts: 1726
    
  12

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:



When you do things right, people won't be sure you've done anything at all.
Mel Ram
Greenhorn

Joined: Sep 30, 2009
Posts: 12
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String class: simple yes or no question here