Understanding Strings in Java: Moving Words Around

  • Thread starter Thread starter zzoo4
  • Start date Start date
  • Tags Tags
    Mean
AI Thread Summary
Strings in Java are a type of variable used to store sequences of characters, defined with quotation marks. When declaring a string, such as `String name = "";`, the variable 'name' is initialized as an empty string. Initializing a string with a space, like `String name = " ";`, means the variable holds a single space character, not an empty string. To manipulate strings, such as changing the order of words, methods like `indexOf` and `substring` can be utilized. For further learning, it's recommended to explore tutorials on variables and the String class methods available in Java.
zzoo4
Messages
42
Reaction score
0
This isn't really a homework but
We are learning about Strings in Java. And I don't get it? (was absent for a day and teacher is not available due to family issues)

What does this mean or do??
String name= ""; //?

and if I have a 3 words like "I like pie"
Why do I use String to move it those words around?? What is String?
 
Physics news on Phys.org
strings are a type of variable that stores a number of letters in a specific order.

looking at

String name = "";

String - specifies the type of variable you are declaring
name - you are calling this variable 'name'
equal sign - the stuff after the equal sign will be assigned to the the variable 'name' and that stuff will be a string
"" - this presents the letters that will be stored to your variable. In order for the compiler to determine a string (the date you are storing) from a variable name, all strings have these quotation marks around them. In this case there is nothing in these quotations marks so you are storing an empty string (0 characters) to the variable you called "name."

does this make sense?
 
Oh What if it's like this??

String name = " ";

I am basically initializing the space right?? Like in between any words?? Like I like sopa. Hello everyone

But how can I use a loop along with this?
 
zzoo4 said:
Oh What if it's like this??

String name = " ";

I am basically initializing the space right?? Like in between any words??
You are not initializing the space - you are initializing the variable name with a string that consists of one space character.
zzoo4 said:
Like I like sopa. Hello everyone

But how can I use a loop along with this?
This is pretty vague. What do you want to do?
 
I still don't get what you mean by initializing.

What if I had to change the orders of the word?
Like
"Hello World" to "World Hello"
 
When a variable is initialized, it is given a value for the first time.

Code:
int num;
int val = 5;

Here the variable num is uninitialized, and val has an initial value of 5.

Since you are working with the String class, you should look to see the methods that are available - see http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html.

One way to switch the order of words in a string would be to:
1) Find the position of the space character in the string - the indexOf could be used.
2) Use the substring method to extract the first word of the string and the second word.
3) Create a new string built up from the substrings you found in the previous step.
 
What if i want to initialize the first letter words in anything i type??


Are there any good tutorials that can teach me String??
 
zzoo4 said:
What if i want to initialize the first letter words in anything i type??
You don't initialize letters or words, you initialize variables. Varaibles are just buckets to hold things. You are telling the program to allot some memory space and assign it a label (num), though you have not told it what to put in that space yet.

"int num" says:

- set aside some memory for a value
- label this memory 'num' so I can refer to it later
- the value it will contain is yet to come
- but it will be an integer

zzoo4 said:
Are there any good tutorials that can teach me String??
Sounds like you first need to read the opening chapters on variables.

Here's a bit from wiki :

http://en.wikipedia.org/wiki/Uninitialized_variable
 
Last edited:
Back
Top