Hello, I was wondering how to create the code for a word rectangle in java?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello, I was wondering how to create the code for a word rectangle in java?
Can you define a "word rectangle" and show an example?
If you don't understand my answer, don't ignore it, ask a question.
When you enter a word it will display it as a regular word then shift a letter to the right, and it will be in rows and columns for the amount of letters the word has. Example:
COMPUTERS
OMPUTERSC
MPUTERSCO
PUTERSCOM
UTERSCOMP
TERSCOMPU
ERSCOMPUT
RSCOMPUTE
SCOMPUTER
Last edited by Pettsa; May 3rd, 2012 at 07:46 PM.
If the project is only changing the position of characters in a String, look at the String class's methods.
And the StringBuilder and StringBuffer classes.
If you don't understand my answer, don't ignore it, ask a question.
Um, I understand those methods but I don't know where to start.
Start by removing one character from the start of the String.
Concatenate that character at the end of the String.
Print it.
Do it again until done.
If you don't understand my answer, don't ignore it, ask a question.
Um, I am sorry, I understand the methods, really I do, this is all I have so far, could you please show me how to do it. My teacher doesn't teach us at all how to do these things and I am so stuck on how to do it, please if you can. Here is what I have so far:
package wordrectangle;
import javax.swing.JOptionPane;
import java.lang.String;
public class WordRectangle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Declare variables
int word, str;
//Ask the user to enter a word from a string and convert to a double
String inputWord = JOptionPane.showInputDialog("Enter a word and this "
+ "program will make a word rectangle with it:");
}
}
If you understand the methods and how to use them and what they do, what are you waiting for to use some of them to solve the problem?I understand the methods,
A hint:
1)extract two Strings from the one String. One with a single character, the other with the rest of the characters.
abcd -> a and bcd
2) concatenate the single character String at the end of the other String ->bcda
If you don't understand my answer, don't ignore it, ask a question.
I just don't know where they would go in my program.
Solve the problem one small step at a time.
Take a String and move its first character to the end of the String. Can you do that?
When that is done, what next?
If you don't understand my answer, don't ignore it, ask a question.
Ok I'm trying to figure this out......
Here is what I have so far.... can you show me an example of what to do?
package wordrectangle; import javax.swing.JOptionPane; import java.lang.String; public class WordRectangle { /** * @param args the command line arguments */ public static void main(String[] args) { //Declare variables int count = 0; char firstLetter; //Ask the user to enter a word from a string and convert to a double String word = JOptionPane.showInputDialog("Enter a word and this " + "program will make a word rectangle with it:"); //Declare the length that will find the number of vowels in the sentence for (int i = 0; i < word.length(); i++) { firstLetter = word.charAt(0); word = word.substring(1, word.length()); System.out.println(firstLetter + word); } } }
What prints out when you execute the program? Is the output what you want?
If you don't understand my answer, don't ignore it, ask a question.
It outputs:
computers
omputers
mputers
puters
uters
And, no that is not what I want.
It looks like you are getting the second part of the String OK. what is happening to the first letter that is supposed to be concatenated to the end of the String? It looks like you remove it and throw it away
If you don't understand my answer, don't ignore it, ask a question.
How would I add it to the end?
Use the + operator:
"sdd" + "e" -> "sdde"
If you don't understand my answer, don't ignore it, ask a question.
On what line would I do that?
Have you tried it yet to see what happens? See posts #6 & #8 where I gave the steps the code should do.
If you don't understand my answer, don't ignore it, ask a question.
All I got it to work but... how would I loop it to make that word only have to right rows and columns for the specific word. Ex computers, 9 rows, 9 columns? Here is the code so far:
package wordrectangle;
import javax.swing.JOptionPane;
import java.lang.String;
*/public class WordRectangle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Declare variables
int count = 0;
char firstLetter;
//Ask the user to enter a word from a string and convert to a double
String word = JOptionPane.showInputDialog("Enter a word and this "
+ "program will make a word rectangle with it:");
//Declare the length that enable the word to be mixed up in the sentence
for (int i = 0; i < word.length(); i++) {
firstLetter = word.charAt(0);
word = word.substring(1, word.length());
System.out.println(firstLetter + word);
i = word.indexOf(word);
i = word.indexOf(firstLetter);
word += firstLetter;
Please post the output from the program and add some comments describing what you want to change.
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting. You did not use code tags.
Why does the code change the value of i two times inside of the loop?
It is dangerous to change the loop control variable inside the loop.
If you don't understand my answer, don't ignore it, ask a question.
public class WordRectangle { /** * @param args the command line arguments */ public static void main(String[] args) { //Declare variables int count = 0; char firstLetter; //Ask the user to enter a word from a string and convert to a double String word = JOptionPane.showInputDialog("Enter a word and this " + "program will make a word rectangle with it:"); //Declare the length that enable the word to be mixed up in the sentence for (int i = 0; i < word.length(); i++) { firstLetter = word.charAt(0); word = word.substring(1, word.length()); System.out.println(firstLetter + word); i = word.indexOf(word); i = word.indexOf(firstLetter); word += firstLetter; } } }
I want the program to stop when it makes a 9 x 9 rectangle. (Depending on the word, using a "computers" as an example. The program keeps running so its 9 x Inifinity.) If you could change it that would be awesome.
Why does the code change the value of i two times inside of the loop?The program keeps running so its 9 x Inifinity.
It is dangerous to change the loop control variable inside the loop.
If you don't understand my answer, don't ignore it, ask a question.
I'm not sure, I am lost at what to do next. Do you think you can fix the code?
Why does the code change the value of i two times inside of the loop?
i = word.indexOf(word); i = word.indexOf(firstLetter);
If your are not sure, then remove them.
If you don't understand my answer, don't ignore it, ask a question.
Then what?