SRL Archive - Java Help and Tutorials

Java Help needed!


[XoL]

2012-09-10 21:38:28

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.


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

2012-09-11 08:44:01


import javax.swing.JOptionPane;

public class FirstToBack2

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

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

2012-09-11 10:08:09

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]

2012-09-11 10:28:51

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]

2012-09-11 23:55:56

Sorry for the bump, but what is the purpose of looping in this specific project? (I don't really see a reason to)

Bobarkinator

2012-09-12 00:05:55

From: R4nd0m
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]

2012-09-12 16:56:07

From: Bobarkinator
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!