PDA

View Full Version : Java Help needed!



[XoL]
09-11-2012, 01:38 AM
Hey guys, sorry I have a question I can't seem to figure this out...

I have to: Write a program that will read a line of text as input and then display the line with the first word moved to the end of the line.
http://i.imgur.com/Lw6CZ.png


This is what I did so far, kind of >.< there was a lot of googling lol.


import javax.swing.JOptionPane;


public class FirstToBack2

{
public static void main(String[] args)
{
String theWord = JOptionPane.showInputDialog("Enter the Words: ");
JOptionPane.showMessageDialog(null,theWord);
int space = theWord.indexOf(' ');
String firstWord = theWord.substring(0, space);
JOptionPane.showMessageDialog(null, firstWord);
JOptionPane.showMessageDialog(null, theWord + firstWord);

}
}


Anyway please help me figure it out, in the dumbest way possible :p (I don't know any of the library functions) Also please keep using JOptionPane

Kasi
09-11-2012, 12:44 PM
import javax.swing.JOptionPane;

public class FirstToBack2

{
public static void main(String[] args)
{
String[] theWord = JOptionPane.showInputDialog("Enter the Words: ").split(" "); // split the input to string
int I = 0; // for looping
String First = theWord[0] + " "; // set the 'First' string to the first word + a space
for (I = 1; I < theWord.length - 1; I++) { // for 1 to highest - 2 since its less than
First = First + theWord[I] + " "; // first equals itself + the next word and a space.
}
First = First + theWord[theWord.length - 1]; // finally add the last word, without space.
String Last = ""; // initialize the string.
for (I = 1; I < theWord.length; I++) { // for 1 to highest - 1
Last = Last + theWord[I] + " "; // itself + the next word and a space
}
Last = Last + theWord[0]; // add the first word.
JOptionPane.showMessageDialog(null, First); // output
JOptionPane.showMessageDialog(null, Last); // output
}
}


that's one way to do it, just look at all the methods, hit . on your variable and look at all the methods in eclipse or alt-enter in netbeans on you variable and it should show all the methods to it

Bobarkinator
09-11-2012, 02:08 PM
import javax.swing.JOptionPane;

public class FrontToBack {

public static void main(String[] args) {
String sentence = JOptionPane.showInputDialog("Enter the words: ");
String[] words = sentence.split(" "); //Gets each word as an array element
String result = "";

//Loop through the array putting all the words together except the first word
for (int i = 1; i < words.length; i++) {
result += words[i] + " ";
}
result += words[0]; //Add in the first word
JOptionPane.showMessageDialog(null, result);
}

}

[XoL]
09-11-2012, 02:28 PM
Thank you both!
Sorry I was having a real hard time grasping this, (Don't know why, but now that I see it complete I got it... well we will see when that test comes around :p)

[XoL]
09-12-2012, 03:55 AM
Sorry for the bump, but what is the purpose of looping in this specific project? (I don't really see a reason to)

Bobarkinator
09-12-2012, 04:05 AM
Sorry for the bump, but what is the purpose of looping in this specific project? (I don't really see a reason to)


import javax.swing.JOptionPane;

public class FrontToBack {

public static void main(String[] args) {
String sentence = JOptionPane.showInputDialog("Enter the words: ");
int index = sentence.indexOf(" ");
String firstWord = sentence.substring(0, index); //Grab the first word
String remaining = sentence.substring(index + 1, sentence.length()); //Grab the rest of the sentence, minus the space we left at the beginning
JOptionPane.showMessageDialog(null, remaining + " " + firstWord);
}
}

You don't have to, I just don't like manipulating strings as much.

[XoL]
09-12-2012, 08:56 PM
import javax.swing.JOptionPane;

public class FrontToBack {

public static void main(String[] args) {
String sentence = JOptionPane.showInputDialog("Enter the words: ");
int index = sentence.indexOf(" ");
String firstWord = sentence.substring(0, index); //Grab the first word
String remaining = sentence.substring(index + 1, sentence.length()); //Grab the rest of the sentence, minus the space we left at the beginning
JOptionPane.showMessageDialog(null, remaining + " " + firstWord);
}
}

You don't have to, I just don't like manipulating strings as much.

Ah I see thank you!