Understanding Strings in Java: Moving Words Around

  • Thread starter Thread starter zzoo4
  • Start date Start date
  • Tags Tags
    Mean
Click For Summary

Discussion Overview

The discussion revolves around understanding Strings in Java, particularly focusing on their initialization, manipulation, and the use of loops. Participants explore the concept of Strings as variables that hold sequences of characters and how to rearrange words within those Strings.

Discussion Character

  • Conceptual clarification
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions the meaning and purpose of declaring a String variable, specifically "String name = "";".
  • Another participant explains that Strings are variables that store letters in a specific order and details the components of the declaration.
  • There is a discussion about initializing a String with a space character, with one participant suggesting it serves as a placeholder between words.
  • A participant expresses confusion about the term "initializing" and seeks clarification on how to change the order of words in a String.
  • One participant describes the process of initializing a variable and contrasts it with uninitialized variables, providing an example with integers.
  • Another participant suggests methods for rearranging words in a String, mentioning the use of indexOf and substring methods.
  • Several participants inquire about tutorials or resources for learning more about Strings in Java.

Areas of Agreement / Disagreement

The discussion contains multiple viewpoints regarding the initialization of Strings and the concept of manipulating them. There is no consensus on the best approach to rearranging words or the clarity of the term "initializing."

Contextual Notes

Participants express varying levels of understanding regarding the initialization of variables and the manipulation of Strings, indicating a need for foundational knowledge in Java programming concepts.

Who May Find This Useful

Individuals learning Java programming, particularly those interested in understanding Strings and variable initialization, may find this discussion beneficial.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K